DataSag guides · Updated 27 October 2026
Install DataSag on Next.js App Router
Add DataSag to Next.js App Router through app/layout.tsx, inside the root body. Create a project first, then replace the example public key with the key from your dashboard.
Where to put the tracking script
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.
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"
/>The project tracking key is public and belongs in this snippet. Account credentials, provider API keys and database secrets do not. Keep your existing consent choices and only load analytics when your site's policy permits it.
Verify the published installation
- Publish the change to the domain visitors actually use. Open a nested page as well as the homepage.
- In browser developer tools, find one tracker.js script with the correct data-datasag-key value.
- Filter Network requests for /api/track. Confirm that a pageview payload names the path you opened and receives a successful response.
- Open the same DataSag project and choose a date range containing your test. Confirm the page appears before measuring campaigns or conversions.
Avoid this Next.js App Router setup mistake
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.
Keep pageviews separate from scroll, click and engagement events. A page can generate several tracking requests while still representing one visit to that path.
Events and conversions
Call the event in the successful client-side action handler. In TypeScript, add an optional Window.datasag type or access the API from a typed helper; do not silence all type errors.
<a href="/signup" data-datasag-goal="signup_click">Create account</a>This attribute measures a click. To record a completed signup, call window.datasag.track in the application's confirmed success handler. Current plan details determine which event and campaign reports are available.
Setup and troubleshooting recipes
- Verify DataSag analytics on Next.js App Router
Check the rendered script, public project key, browser requests and first pageview before relying on your analytics.
- Fix DataSag analytics not working on Next.js App Router
Trace missing website visits through script placement, project keys, exclusions, consent, network delivery and report filters.
- Fix duplicate analytics pageviews on Next.js App Router
Separate legitimate engagement events from duplicate tracker installations and repeated pageview dispatches.
- Track custom events on Next.js App Router
Add click goals or completed-action events with DataSag's data attribute and browser JavaScript API.
- Track UTM campaigns on your website
Create consistent tagged links and verify that campaign parameters reach your website and DataSag.
- Measure AI referral traffic on your website
Check visits with observable AI referrers and understand the difference between traffic, citations and LLM visibility.
- Configure CSP for DataSag analytics on Next.js App Router
Diagnose blocked scripts, beacon requests and image fallbacks without disabling your site's Content Security Policy.
- Exclude your own analytics visits on your website
Use DataSag's browser exclusion switch to keep your testing activity out of future tracked visits.
- Load analytics without blocking rendering on Next.js App Router
Choose a non-blocking script placement and verify actual loading behavior instead of relying on a fixed speed claim.
Platform reference
Official Next.js App Router documentation explains the platform integration point. DataSag-specific behavior here follows the current tracker and project setup.
Need a different stack? Browse every installation guide.