# Next.js analytics: your options and the App Router gotchas

The CrispStats team · Published 2026-11-23 · Updated 2026-11-29

**TL;DR:** 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.

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](/integrations/nextjs) is the shorter read.

## What does Next.js include out of the box?

Performance metrics, not visitor analytics. The [Next.js analytics guide](https://nextjs.org/docs/app/guides/analytics) 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?

Vendor facts as of 23 November 2026, from each vendor's own documentation.

| Option | How it is added | Cookies | Cost model | Best for |
| --- | --- | --- | --- | --- |
| Vercel Web Analytics | The `@vercel/analytics` component in the root layout ([quickstart](https://vercel.com/docs/analytics/quickstart)) | None; visitors are a request hash that resets daily | Free on Hobby up to 50k events a month; $0.03 per 1k events on Pro ([pricing](https://vercel.com/docs/analytics/limits-and-pricing)) | Apps deployed on Vercel that want traffic next to deployments |
| Google Analytics 4 | `<GoogleAnalytics gaId="G-..." />` from `@next/third-parties/google` ([docs](https://nextjs.org/docs/app/guides/third-party-libraries)) | `_ga` and `_ga_<id>`, two years by default ([Google](https://support.google.com/analytics/answer/11397207)) | Free | Teams that buy Google Ads and want the data in Google's tools |
| Privacy-first script tools | A `next/script` element in `app/layout.tsx` ([docs](https://nextjs.org/docs/app/api-reference/components/script)) | Usually none | By pageviews or events; most have a free tier or trial | Sites that want simple numbers without a consent banner question for analytics |
| Build your own | `useReportWebVitals` plus your own endpoint and database | Your choice | Your hosting and time | Performance data only, or a team that wants to own the pipeline |

### 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](https://vercel.com/docs/analytics/limits-and-pricing): 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](/alternatives/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](https://support.google.com/analytics/answer/9216061) 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](/tools/analytics-cost-calculator) does the sums, and the [checklist for choosing a Google Analytics alternative](/blog/google-analytics-alternative-checklist) 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](https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation-client):

```ts
// 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](https://react.dev/reference/react/StrictMode) 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](https://nextjs.org/docs/app/api-reference/functions/use-search-params) 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](https://nextjs.org/docs/app/api-reference/components/script). `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](https://nextjs.org/docs/app/guides/content-security-policy) 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:

```tsx
'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](/blog/core-web-vitals-plain-english) explains the percentile rule, and [how to improve Core Web Vitals](/blog/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:

Vercel and Google facts as of 23 November 2026 from their own pages.

| 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.

## The mistake: a second pageview on every navigation

In the App Router, a click on Link swaps the page in the browser. A tool that only counts full document loads records one pageview for the whole visit, so every route after the first is missing. The paired mistake is an extra send: a usePathname hook or a useEffect that fires a pageview on top of a script that already listens for history changes. Every navigation is then counted twice, and the site looks twice as deep as it is.

Settle it from the tool's docs. When the tool tracks client-side navigation, add nothing else. When you have to send the pageview yourself, send one per navigation, for example from onRouterTransitionStart in instrumentation-client.ts, and stop there. Strict Mode runs effects twice under next dev, so judge the count on a production build or a deployed preview.

## Add the cookieless script in the root layout

The script tag is `<script defer src="https://in.crispstats.com/d.js" data-site="YOUR_SITE_KEY"></script>`. In the App Router, load that same script with next/script from the root layout, inside the body, after the page content. The [Next.js install guide](/integrations/nextjs) and the product docs use this element:

```tsx
import Script from "next/script";

// Inside <body> in app/layout.tsx, after {children}.
<Script
  src="https://in.crispstats.com/d.js"
  data-site="YOUR_SITE_KEY"
  data-domains="example.com"
  strategy="afterInteractive"
/>
```

Replace the key with your public key, and replace example.com with your production hostname. data-domains keeps other hosts, including preview deployments, out of the property. Use afterInteractive, the strategy the App Router section above recommends for analytics. The script follows Link, router.push and the back button, and it ignores localhost. Add no usePathname effect beside it.

A Webflow site takes the same script in Site settings, Custom code, Head code, and then you publish. That install is the [Webflow analytics guide](/blog/webflow-analytics-guide). A marketing site on Webflow and an app on Next.js can share an account as two sites, each with the install its host expects.

CrispStats sets no cookies by default. An IP address is used to compute a daily rotating visitor hash and then discarded, and raw user agents are not stored. The core script is about 2.2 KB gzipped.

## 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](/features/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](/integrations/nextjs) has the exact element to paste.

## Frequently asked questions

### Does Next.js have built-in analytics?

Next.js has built-in performance reporting through the useReportWebVitals hook and an instrumentation-client file for setting up tools, but no visitor analytics. To count visitors, sources and conversions you add a tool such as Vercel Web Analytics, Google Analytics or a privacy-first script.

### Does Vercel Web Analytics work outside Vercel?

No. Vercel's documentation describes Web Analytics as part of the Vercel platform for projects deployed there. If you host a Next.js app elsewhere, use a tool that loads with a script tag.

### How do I track route changes in the Next.js App Router?

Most current analytics tools detect App Router navigations by listening to the browser's History API, so check your tool's docs before adding code. If it does not, send one pageview per navigation, for example from onRouterTransitionStart in instrumentation-client.ts. Never do both, or every navigation is counted twice.

### Where should the analytics script go in the App Router?

In the root layout, app/layout.tsx, using the next/script component with the afterInteractive strategy. The layout is a server component, which is fine: next/script loads the script in the browser.

### Why do my analytics events fire twice in development?

React Strict Mode runs effects an extra time in development. An event sent from useEffect fires twice under next dev and once in production. Check event counts on a production build or a deployed preview.

### How do I add a cookieless analytics script to the Next.js App Router?

Put one next/script element in app/layout.tsx, inside body, after the page content, with strategy afterInteractive. Set src to the tracker script and data-site to your public key. The script counts route changes itself. Add no usePathname effect, and expect no visits from localhost.

### Can the same analytics script run on Next.js and on Webflow?

Yes, as two installs of the same cookieless script. Next.js loads it with next/script in the root layout. Webflow pastes the tag into Site settings, Custom code, Head code, and you publish. Each host keeps its own snippet location.
