next/script element in app/layout.tsx. Navigation between routes (<Link>, router.push, the back button) is tracked for you, so you need no usePathname effect. Everything else on this page is optional: types for the global, a first-party proxy, 404 tracking and AI crawler reports. There is no npm package to install.
You need your site’s public key, which starts with cs_pk_. Settings, Tracking code, Next.js shows every snippet on this page with your key and domain filled in.
Add the script
1
Add the Script element to the root layout
Open Replace the key and the domain with yours.
app/layout.tsx (or src/app/layout.tsx). Import Script at the top and add the element inside <body>, after {children}. Change nothing else in the file: your metadata, fonts and providers stay as they are.app/layout.tsx
data-domains keeps preview deployments and copies of your site on other hosts from being counted. List more hostnames with commas: data-domains="example.com,example.org".Pages Router: add the same import and element to pages/_app.tsx, next to <Component {...pageProps} />.2
Deploy and check
Deploy, then open your production site in a normal browser window. Visits to
next dev on localhost:3000 are not counted: see test locally. Your visit shows in Realtime within a few seconds, and Settings, Tracking code shows Receiving events.Why this element and these props
strategy="afterInteractive"loads the script right after the page becomes interactive, so it never delays your page. Do not uselazyOnload, which loads it so late that short visits are missed, orbeforeInteractive, which it does not need. Because the script starts after your app, it watches route changes through both the History API and the Navigation API; if route changes still go missing in some browsers, see routers that saved pushState early.- No
idprop.next/scriptneeds anidonly for inline scripts. Do not addid="crispstats": browsers expose an element’s id as a global, sowindow.crispstatswould be the<script>element until the script loads, and early calls throw. - Flags take an empty string. In JSX, write
data-outbound="", not a baredata-outbound, which renders asdata-outbound="true". See write flag attributes. - No
data-hash. Next.js routes with real paths, anddata-hashonly reads routes written after#, such as/#/pricing.
As a component
If you prefer a component, this file renders only the element. Render<CrispStats /> inside <body> in app/layout.tsx:
app/crispstats.tsx
next/script handles the client side.
Send events with types
The script installswindow.crispstats. Two small files make it typed and safe to call before the script loads. Both come from TypeScript, where they are explained.
1
Add the global types
Save this as
crispstats.d.ts at the project root, next to next-env.d.ts. Next.js projects include every .d.ts file there.crispstats.d.ts
2
Queue calls made before the script loads
Put this in
instrumentation-client.ts at the project root (or in src/). Next.js runs that file before your app becomes interactive, so the queue exists before any component calls crispstats. If the file already exists, add the code to it.instrumentation-client.ts
3
Call it from a client component
app/pricing/upgrade-button.tsx
?.) keeps the button working when an ad blocker stops the script and the stub is missing. For clicks you can also use data-cs-event attributes and no JavaScript: see declarative events.Proxy through your domain
To count visitors whose ad blocker stopsin.crispstats.com, serve the script and events from a path on your own site. In Next.js that is one route handler and a changed Script element. Nothing goes in next.config.ts or next.config.mjs: a rewrite there would forward your visitors’ cookies.
1
Add the route handler
Settings, Tracking code, Proxy through your own domain proposes a path for your site, such as It forwards only the headers listed in the comment, sets
/k3x9qe. Create app/k3x9qe/[...path]/route.ts with your path as the folder name:app/k3x9qe/[...path]/route.ts
x-forwarded-for to the visitor’s address, passes the visitor’s location on Vercel and Cloudflare, and returns only content-type, cache-control, etag and access-control-allow-origin. It never forwards cookie or authorization.2
Point the Script element at the path
app/layout.tsx
3
Check it
After you deploy, open
https://example.com/k3x9qe/check?k=cs_pk_xxxxxxxx on your site. Look for "proxied": true, "visitorAddressForwarded": true and "cookiesForwarded": false. See check it works.proxy.ts (or middleware.ts) runs on every path, leave the proxy path out of its matcher, as the AI crawler example below does.
Next.js on Cloudflare: OpenNext and vinext
The same route handler runs as is under OpenNext (@opennextjs/cloudflare) and vinext. On Cloudflare it reads the visitor’s address from cf-connecting-ip and the country from cf-ipcountry; turn on the Add visitor location headers managed transform in Cloudflare to pass the region and city too. To sign the requests, store the secret with npx wrangler secret put CRISPSTATS_PROXY_SECRET and redeploy.
vinext keeps its own reference to history.pushState, so the script sees its route changes through the Navigation API, in browsers that have it. If route changes are missing in other browsers, use manual pageviews.
Track 404 pages
Adddata-404="" to the Script element, then mark the not-found page. In the App Router, add the marker to app/not-found.tsx:
<meta> into <head>, and the script finds it anywhere in the document either way. Use the metadata version only in app/not-found.tsx or app/global-not-found.tsx, which support a metadata export. A notFound() call in a page renders the nearest not-found.tsx, so it is marked too. See 404 pages for when the check runs.
Test locally
The script ignoreslocalhost and automated browsers, so next dev and Playwright runs show nothing in the dashboard. Turn on debug mode in development to see every event in the console instead, without sending anything:
app/layout.tsx
NEXT_PUBLIC_CRISPSTATS_DEBUG=1 in .env.development.local and in your test runs, never in production. Localhost and testing shows a Playwright test that asserts on these lines.
Report AI crawlers from proxy.ts
AI crawlers such as GPTBot and ClaudeBot do not run JavaScript, so the script never sees them. Your Next.js proxy (called middleware before Next.js 16) can report them to the collector. It sends only the crawler’s User-Agent and the path: no IP address, no query string and no other header.proxy.ts
middleware.ts and the function middleware. If you already have a proxy, add the if block to it. Ordinary requests make no network call. The collector checks the User-Agent again and records only known AI crawlers. See AI crawlers and the /bot reference.
Next steps
Custom events
Track signups, clicks and other actions with properties.
Visitor id at checkout
Pass the visitor id to Stripe to attribute revenue.
Single-page apps
How navigation is seen, and manual pageviews.