Frameworks
Next.js analytics with CrispStats
One next/script element in app/layout.tsx adds CrispStats to a Next.js app: paste it, then deploy. Navigation between routes is tracked for you, so you need no usePathname effect and no npm package.
Updated
Set up CrispStats on Next.js
- 01
Step 1: Copy the snippet with your key
In the dashboard, open your site’s Settings, then Tracking code, and choose the Next.js tab. It shows the element below with your public key (it starts with
cs_pk_) and your domain filled in. - 02
Step 2: Add the Script element to the root layout
Open
app/layout.tsx(orsrc/app/layout.tsx). ImportScriptand add the element inside<body>, after{children}. Change nothing else: metadata, fonts and providers stay as they are. On the Pages Router, add the same element topages/_app.tsx, next to<Component {...pageProps} />.Replace the highlighted values with your own.
app/layout.tsximport Script from "next/script";// Inside <body> in app/layout.tsx, after {children}.<Scriptsrc="https:/ / in. crispstats. com/ d. js"data-site="cs_pk_your_site_key"data-domains="example.com"strategy= "afterInteractive"/> - 03
Step 3: Deploy and open your production site
Deploy as you normally do, then open the production URL in a normal browser window.
data-domainskeeps preview deployments and copies of the site on other hosts out of your numbers; list more hostnames with commas. - 04
Step 4: Send a custom event (optional)
Call the global from a client component. The optional call (
?.) keeps the button working when an ad blocker stops the script. For typed calls and a queue that catches calls made before the script loads, add the two small files from the Next.js guide.app/pricing/upgrade-button.tsx"use client";export function UpgradeButton() {return (<button onClick={() => window.crispstats? . ( "upgrade clicked", { from: "pricing" })}>Upgrade</button>);}
Check that it works
Open your production site in a normal window. Your visit shows on the Realtime screen within a few seconds, and Settings, Tracking code shows Receiving events. Visits to next dev on localhost:3000 are not counted, so test on a deployed URL, or turn on debug mode to see each event in the console without sending it (test locally).
CrispStats on Next.js
What you get
- Pageviews for every route change:
<Link>,router.pushand the back button, read through the History API and the Navigation API. - Sources and channels for each route, so you see whether search, a launch post or an AI assistant such as Perplexity sent people to your docs or your pricing page.
- A core script of about 2.2 KB gzipped, loaded after the page is interactive, so it never delays your first paint.
- Optional web vitals (LCP, INP, CLS at p75), JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.
- Revenue by source once you pass
crispstats.id()to your checkout: see Stripe revenue attribution.
- Pageviews for every route change:
Notes and limits
- Use
strategy="afterInteractive".lazyOnloadloads the script so late that short visits are missed, andbeforeInteractiveis not needed. - Do not give the element an
id. Browsers expose an element’s id as a global, soid="crispstats"would make the global the script element until it loads. - In JSX, write flag attributes with an empty string:
data-outbound="", not a baredata-outbound, which renders asdata-outbound="true". - To count visitors whose ad blocker stops the collector host, proxy it through a route handler on your own domain, never a rewrite in
next.config.ts, which would forward your visitors’ cookies. See proxy through your domain. - AI crawlers such as GPTBot do not run JavaScript. Report them from
proxy.ts(middleware.tsbefore Next.js 16), which sends only the user agent and the path. - The proxy route handler runs as it is on Vercel, and on Cloudflare through OpenNext or vinext.
- Use
The docs page covers the optional steps this guide leaves out.
Setup questions. Straight answers.
What people ask before they connect it.
No. CrispStats is one next/script element in your root layout. There is nothing to install, and route changes in the App Router and the Pages Router are tracked without extra code.
Yes. The Script element can sit in a server component such as app/layout.tsx, and next/script handles the client side. Custom events are sent from client components.
The script ignores localhost and automated browsers on purpose, so development and test runs do not pollute your numbers. Turn on debug mode in development to see each event in the browser console instead.
Yes. Add the same Script element to pages/_app.tsx, next to the page component. Route changes made by next/router are read from the History API, exactly as in the App Router.
Each route change is one pageview event, and preview deployments cost nothing because data-domains keeps them out. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.
Not answered here? Write to hello@crispstats.com
Go further
Related
- Developers: web analytics that stays out of your code
A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.
- Web vitals from real visits
LCP, INP and CLS measured on real visits, by page and device.
- Error tracking in your analytics
JavaScript errors grouped with the page and browser they hit.
- CrispStats vs Vercel Analytics
Vercel Web Analytics is built into Vercel and free up to 50k events.
Guides
- Next.js analytics: your options and the App Router gotchas
The ways to add analytics to a Next.js app, from Vercel Web Analytics to GA4 and privacy-first scripts, and the App Router mistakes that skew your numbers.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
Your traffic, crisp and clear
One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.