Trakg works seamlessly with both App Router and Pages Router in Next.js. It captures user interactions on any form built with standard HTML elements, and gives you real-time insights β€” with zero backend config.

πŸ“¦ Installation Overview

All you need is to embed the Trakg tracking script. We recommend using Next.js’s built-in <Script /> component for better performance. πŸ” Replace YOUR_ID_GIVEN with your actual Site ID found in your Trakg dashboard.

βš™οΈ App Router (Next.js 13+)

If you’re using the App Router (/app directory), you can add the script inside your layout.tsx or layout.js file β€” which wraps all routes.

Example: app/layout.tsx

import Script from "next/script";

export default function RootLayout({children}: {children: React.ReactNode}) {
	return (
		<html lang='en'>
			<body>
				{children}

				{/* βœ… Trakg Script */}
				<Script
					defer
					src='https://cdn.trakg.com/api/v1/tracker.min.js?id=YOUR_ID_GIVEN'
					strategy='afterInteractive'
				/>
			</body>
		</html>
	);
}

πŸ“ Pages Router

If you’re using the traditional Pages Router (/pages directory), include the Trakg script in your custom _document.js.

Example: `pages/_document.js

import {Html, Head, Main, NextScript} from "next/document";

export default function Document() {
	return (
		<Html lang='en'>
			<Head />
			<body>
				<Main />
				<NextScript />

				{/* βœ… Trakg Script */}
				<script
					defer
					src='https://cdn.trakg.com/api/v1/tracker.min.js?id=YOUR_ID_GIVEN'
				></script>
			</body>
		</Html>
	);
}

πŸ›‘ Best Practices

⚠️ You must use a standard <script defer /> here β€” since Next.js does not support <Script /> from next/script inside _document.js.
Recommendations
  • Always use the defer attribute β€” ensures the script loads after the HTML is parsed.
  • For App Router, prefer <Script strategy="afterInteractive" /> from next/script.
  • Keep the script at the end of <body> for non-blocking performance.
  • No need to modify your forms β€” Trakg supports native <form>, <input>, <textarea>, and <select> elements.
  • Works with any form library or custom UI inputs β€” as long as native HTML form elements are rendered in the DOM.

πŸš€ You’re Ready!

Once the script is added, Trakg will automatically:
  • πŸ” Track user interactions on your forms
  • πŸ“ Capture partially filled form data
  • βœ… Log completed submissions
  • πŸ“Š Display real-time analytics in your dashboard
  • πŸ” Help you recover abandoned leads
Need help? Contact Support or View Dashboard to explore your captured data.