What this lets you do
Track AI-referred traffic across a Next.js application, including client-side navigations that don’t trigger a full page reload.Before you start
- Your Traceten site key, from Sites → Install in the dashboard.
- A Next.js project (version 13 or later recommended).
- Understand which router you’re using: App Router (
app/) or Pages Router (pages/).
App Router (Next.js 13+)
The App Router uses a root layout file (app/layout.tsx) that wraps every route. This is where the snippet goes.
1
Open app/layout.tsx
Open your root layout file at
app/layout.tsx (or app/layout.jsx).2
Add the snippet to the <head>
Use Next.js’s built-in Copy the whole tag from Sites → Install for your site so
<Script> component with strategy="afterInteractive". This is equivalent to adding an async script tag and ensures the snippet loads after the page is interactive.data-site (your site key) is already yours. data-cookie-domain is your own domain, which keeps one visitor as one visitor across your subdomains. It’s off by default, so it only appears on the tag once you’ve confirmed a value under Sites → Settings → Cookies. See Subdomains and cross-domain.strategy="afterInteractive" loads the script after hydration without blocking the page. Do not use strategy="beforeInteractive", because the snippet is designed to run after the page loads.3
Deploy and verify
Run
next build and deploy, or start the dev server with next dev to test locally. Then check the Verify step on your site’s install page.Pages Router
If you’re using the Pages Router (pages/ directory), the snippet goes in _document.tsx.
1
Open or create pages/_document.tsx
If the file doesn’t exist, create it at
pages/_document.tsx.2
Add the snippet in the Head component
data-site (your site key) is already yours. data-cookie-domain is your own domain, which keeps one visitor as one visitor across your subdomains. It’s off by default, so it only appears on the tag once you’ve confirmed a value under Sites → Settings → Cookies. See Subdomains and cross-domain.3
Deploy and verify
Restart your dev server or deploy. Check the Verify step on your site’s install page.
Single-page application navigation
Route changes are tracked automatically. You do not need to add anything. The snippet watcheslocation and fires a pageview whenever the path or query
string changes, so App Router and Pages Router navigations are both picked up
without any code from you. Each detected navigation closes the measurement
window for the route being left, reports it, and opens a fresh one (scroll
depth, time on page, active time) for the new route.
Two cases still need a manual call:
- Hash-only routing. Changes to the fragment (
/docs#install→/docs#usage) are deliberately ignored, because most sites use the hash for in-page anchors rather than for navigation. If your router navigates by hash alone, callwindow.traceten("pageview")yourself after each change. - A route that changes neither path nor query string. Rare, but if you re-render a genuinely different page at the same URL, the snippet cannot see it.
window.traceten exposes.
Environment-specific site keys
Use different site keys for production and staging:.env.local (staging):
data-cookie-domain can stay hardcoded across both environments. A staging host under the same domain (staging.example.com) is covered by it, and a preview host on a different domain (*.vercel.app) does not match, so the snippet ignores it there and writes host-only cookies. If your staging site is a separate Traceten site on its own domain, use the value its own install page shows.
Do not commit real site keys to version control.

