Setup and data
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.
The CrispStats team · · 9 min read
In short
A Next.js app has three common routes to traffic analytics: Vercel Web Analytics if you deploy on Vercel, Google Analytics through the @next/third-parties package, or a privacy-first script loaded with next/script in the root layout. Next.js itself only ships performance reporting (useReportWebVitals), not visitor analytics. Whichever you pick, the App Router gotchas are the same: client-side navigations must be counted as pageviews exactly once, tracking code belongs in client components, and preview and development traffic has to be kept out.
On this page · 6 sections
Sections
Adding analytics to a static site is one script tag. A Next.js app is different in three ways: most navigation happens in the browser without a full page load, half your components run only on the server, and every pull request gets its own preview URL. Each of these can quietly double, drop or pollute your numbers.
This guide covers the options and the traps, using the Next.js documentation for version 16.4 and the vendors’ own pages, read on 23 November 2026. If you have already chosen CrispStats, the Next.js install guide is the shorter read.
What does Next.js include out of the box?
Performance metrics, not visitor analytics. The Next.js analytics guide describes two built-in pieces: the useReportWebVitals hook, which hands you TTFB, FCP, LCP, CLS and INP for each page view, and an instrumentation-client.ts file that runs before your front-end code, for setting up analytics or error tracking. Neither one counts visitors, sources or conversions. For that you add a tool.
Which analytics options work with Next.js?
Vercel Web Analytics: what to know
It is the lowest-effort choice on Vercel: one component, and no account beyond the Vercel one you already have. The limits come from Vercel’s pricing page: it works only for projects deployed on Vercel; on Hobby, collection pauses once 50k events and a 3-day grace period are used up; custom events need Pro; UTM parameters need the Web Analytics Plus add-on at $10 a month; and full Core Web Vitals are a separate product, Speed Insights. If you might move the app to another host, the data does not move with it; the Vercel Analytics alternatives page compares tools that work on any host.
Google Analytics 4: what to know
The @next/third-parties package (which Next.js still marks as experimental) loads the Google tag for you and gives you sendGAEvent for events. GA4 can count client-side navigations as pageviews: the page views option in Enhanced measurement has an advanced setting that sends one on browser history events (pushState, popState and replaceState). Check that it is on before adding any route-change code of your own. Because it sets cookies, sites with visitors in opt-in regions such as the EU and UK usually load it behind a consent banner and lose the visitors who decline.
Privacy-first script tools: what to know
Tools in this group, including ours, are a single script tag that sets no cookies by default and counts visitors with a hashed or anonymous identifier. Most detect App Router navigations themselves by listening to the History API, so you add nothing else. They vary a lot on everything beyond pageviews: some have funnels, revenue, web vitals or error tracking, others are deliberately minimal. Compare at your own volume; the analytics cost calculator does the sums, and the checklist for choosing a Google Analytics alternative lists the questions to ask.
The App Router gotchas that skew your numbers
These apply whichever tool you choose. Each one is a real way to get wrong numbers that look plausible.
1. Counting navigations zero times, or twice
In the App Router, clicking a <Link> swaps the page in the browser; no new HTML document loads. A tool that only counts full page loads sees one pageview per visit. The opposite mistake is common too: adding a usePathname effect that sends a pageview on every route change to a tool that already tracks history changes, which counts every navigation twice.
Check your tool’s docs for one sentence: does it track client-side navigation automatically? If yes, add nothing. If no, send exactly one pageview per route change. Next.js 16 also offers a framework hook for this, onRouterTransitionStart in instrumentation-client.ts, documented in the instrumentation-client reference:
// instrumentation-client.ts, at the project root or in src/export function onRouterTransitionStart( url: string, navigationType: 'push' | 'replace' | 'traverse',) { // Runs when an App Router navigation starts console.log(url, navigationType)}2. Duplicate events in development
In development, React’s Strict Mode runs every effect an extra time to surface bugs. A pageview or event sent from useEffect therefore fires twice under next dev and once in production. Do not “fix” the duplicate by adding guards that change production behavior; test event counts on a production build or a deployed preview instead.
3. Reading search params in a tracking component
If your tracking component calls useSearchParams to read UTM parameters, the Next.js docs explain the cost: on a prerendered route, the client component tree up to the closest Suspense boundary is rendered on the client instead. Put the tracking component inside its own <Suspense> so the rest of the page still prerenders, or read window.location.search inside an effect, which avoids the hook entirely.
4. Calling the tracker from a server component
Server components never run in the browser, so window and the tracker’s global do not exist there. The script element itself can sit in app/layout.tsx, which is a server component, because next/script handles the client side. Events from clicks have to come from a component marked 'use client'. Server-side events, such as a completed payment, go through the tool’s server API from a route handler or server action.
5. Script strategy
next/script has three stable loading strategies. afterInteractive is the default and the right one for analytics: it is injected on the client after some hydration has happened, so it does not hold up the server-rendered HTML. lazyOnload waits for browser idle time, which can be late enough to miss short visits. beforeInteractive is for scripts the page cannot work without; analytics is not one of them.
6. Content Security Policy
If you send a CSP header, the analytics host has to be allowed in script-src and connect-src. If you use nonces, the Next.js CSP guide notes that a fresh nonce per request means the page must be rendered dynamically, which gives up static rendering. Allowing the analytics host by name is often simpler than a nonce for a single external script.
7. Preview deployments and localhost
Every pull request on Vercel or Netlify gets a preview URL, and your team clicks through them. Unless the tool is told which hostnames count, those visits land in your production numbers. Restrict collection to your production domain, and make sure localhost is ignored too.
8. Ad blockers
Many blockers stop well-known analytics hosts, whatever the tool. Serving the script and the collection endpoint from your own domain recovers some of those visits. In Next.js that means a route handler that forwards the request. Read your tool’s guidance before using a rewrites rule in next.config.ts for this: a rewrite passes the visitor’s request, including its headers, to the other host.
Measuring web vitals yourself with useReportWebVitals
If you only want performance data, the built-in hook is enough. It must run in a client component, so the Next.js docs put it in its own small file and render it from the root layout:
'use client'import { useReportWebVitals } from 'next/web-vitals'export function WebVitals() { useReportWebVitals((metric) => { // metric.name is LCP, INP, CLS, FCP or TTFB navigator.sendBeacon('/api/vitals', JSON.stringify(metric)) }) return null}What the hook does not do is the rest of the job: storing every event, computing the 75th percentile per page and device, and showing it. Core Web Vitals are judged at the 75th percentile of real visits, so averages from this hook will look better than what Google reports. Core Web Vitals in plain English explains the percentile rule, and how to improve Core Web Vitals covers the fixes.
Which option fits which Next.js app? A worked example
Say a Next.js marketing site and app get 100,000 pageviews a month, with no custom events yet. Here is what each route costs and asks of you, from the facts above:
| Option | Monthly cost at 100,000 pageviews | What else it needs |
|---|---|---|
| Vercel Web Analytics | About $3 in usage on Pro (100 × $0.03), on top of the $20 Pro plan | Hosting on Vercel; the $10 Plus add-on for UTM reports |
| GA4 | $0 | A consent banner for opt-in regions, and the data you lose from visitors who decline |
| CrispStats | $5 on the Starter plan, which covers 250,000 events | One script element; works on any host |
Cost is rarely the deciding factor at this size. The deciding factor is usually where you host, whether you need consent for the tool, and which questions you will ask next: campaigns, funnels, revenue or performance.
How CrispStats works with Next.js
CrispStats is the script-tag route: one next/script element with strategy="afterInteractive" in the root layout, no npm package needed. It tracks App Router and Pages Router navigations through the History API, so you add no usePathname effect. It ignores localhost and, with data-domains set, only counts the hostnames you list, which keeps preview deployments out. The core script is about 2.2 KB gzipped and sets no cookies by default.
The same tag can also collect web vitals at the 75th percentile, JavaScript errors and outbound clicks, each switched on with one attribute, and revenue can be tied to the first source of each visitor through Stripe and other payment providers. It works on any host, Vercel included. What it is not: it does not trace server-side exceptions or read source maps, so pair it with an error tracker if you need stack traces from route handlers. Every account starts with a 14-day free trial with every feature and no card, and the Next.js install guide has the exact element to paste.