DataSag guides · Updated 27 October 2026
Verify DataSag analytics on Next.js App Router
A script tag alone does not confirm that analytics works. Verify the published document, the request to DataSag and the matching visit in the correct project. This guide applies to Next.js App Router.
Start with the Next.js App Router installation
Import Script from next/script in your root layout. Render the DataSag Script once inside body so it persists across child routes. Keep the existing html, body, providers and children in place. Use strategy="afterInteractive" and replace YOUR_PUBLIC_KEY with the public key copied from your DataSag project.
A Script in a page component or nested layout may disappear or be reintroduced as the route tree changes. Put the single shared installation in the root layout. Browser-only event calls belong in client components, never in a Server Component render.
Use the public project key from the DataSag project you are inspecting. A test on a preview domain does not confirm that the production domain has the same code or settings.
Verify DataSag analytics in Next.js App Router
- Inspect app/layout.tsx rather than an individual page. The root body should contain one next/script installation with the public key. Keep existing providers and children intact, then build and deploy that version.
- Open a nested route directly in the published app. In Elements and Network, confirm that the Script component produced a real script element and a successful tracking request for that pathname.
- Follow a Next.js internal Link to another pathname and use Back. The root layout should remain mounted while the tracker records the navigation. Check the request path instead of expecting the script to download again.
A direct nested entry and a client-side Link transition both produce their expected pageview requests from one root installation.
Complete the collection checks
- Open the published website in a normal browser window with the Network panel recording. Use a browser profile where you can control extensions and your consent choice.
- Filter network requests for tracker.js. Confirm it returns JavaScript successfully and that the rendered script has the project's data-datasag-key attribute. The example YOUR_PUBLIC_KEY must be replaced.
- Filter for /api/track and reload once. Inspect a request with eventName null or absent. Its projectKey and path should match the chosen project and the page you opened.
- Open that DataSag project, choose a date range containing today and inspect the matching path. Use the configured report timezone when checking dates.
- Navigate to a second path, then use Back. Compare pageview requests with the route changes rather than counting every request as a pageview.
document.querySelectorAll('script[data-datasag-key]').length
// Expected: 1
document.querySelector('script[data-datasag-key]')?.getAttribute('data-datasag-key')
// Expected: your project's public key
typeof window.datasag?.track
// Expected after the script runs: 'function'What a successful check looks like
One rendered tracker installation, a successful request for the intended page and a corresponding entry in the intended project. Scroll and engagement requests are separate event types.
Record the public path you tested, the browser state, the approximate time and the observed request. Check one direct page load and one later navigation so the result covers more than the homepage.
Common mistake
A successful tracker.js download only proves that a file loaded. It does not validate the project key, consent state or tracking endpoint. Test a real published path before declaring setup complete.
Installation snippet for Next.js App Router
The shared installation belongs in app/layout.tsx, inside the root body. Use this as the installation reference while checking the procedure above.
import Script from "next/script";
// Inside the existing root layout body, alongside {children}:
<Script
id="datasag"
src="https://www.datasag.com/tracker.js"
data-datasag-key="YOUR_PUBLIC_KEY"
strategy="afterInteractive"
/>Related Next.js App Router guides
- Fix DataSag analytics not working
- Fix duplicate analytics pageviews
- Track custom events
- Track UTM campaigns
- Measure AI referral traffic
- Configure CSP for DataSag analytics
- Exclude your own analytics visits
- Load analytics without blocking rendering
Full Next.js App Router setup guide · Search and AI visibility
References
Official Next.js App Router documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.