# How to Add Analytics to Your Next.js App Without Cookies

A developer guide to integrating privacy-first, cookie-free analytics into your Next.js application. Works with App Router and Pages Router.

By Jasper Koers · Published 2025-07-29

## Key takeaways

- ClearAnalytics is added to a Next.js app with one Script component from next/script, in app/layout.tsx for the App Router or pages/_app.tsx for the Pages Router.
- Loading the ClearAnalytics script with strategy="afterInteractive" means it loads after the page is interactive and never blocks rendering.
- ClearAnalytics tracks every client-side route change in a Next.js app as a pageview without extra configuration.
- Custom events such as signups are tracked in ClearAnalytics with window.ca?.('event', { name: 'signup' }).
- On a production Next.js site, the 0.8KB gzipped ClearAnalytics script left the Lighthouse score, First Contentful Paint and Total Blocking Time unchanged.

Adding analytics to a Next.js application should be simple. With privacy-first analytics, it is. No cookie consent logic, no tag manager, no GDPR headaches. Just a single script tag and you are tracking.

## Why Cookie-Free Analytics Fits Next.js

Next.js developers care about performance. A sub-1KB analytics script with no cookies means zero impact on your Lighthouse score and no consent banner blocking your UI.

Unlike Google Analytics, which requires cookie consent flows, privacy-first analytics works without any client-side state management for consent.

## Setup with App Router

With the Next.js App Router, add the tracking script in your root layout:

```tsx
// app/layout.tsx
import Script from 'next/script'

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <Script
          src="https://track.clearanalytics.eu/ca.js"
          data-site-id="your-site-id"
          strategy="afterInteractive"
        />
      </body>
    </html>
  )
}
```

That is it. The script loads after the page is interactive, so it never blocks rendering.

## Setup with Pages Router

For the Pages Router, add it in `_app.tsx` or `_document.tsx`:

```tsx
// pages/_app.tsx
import Script from 'next/script'

export default function App({ Component, pageProps }) {
  return (
    <>
      <Component {...pageProps} />
      <Script
        src="https://track.clearanalytics.eu/ca.js"
        data-site-id="your-site-id"
        strategy="afterInteractive"
      />
    </>
  )
}
```

## SPA Navigation Tracking

ClearAnalytics automatically detects client-side navigation in single-page applications. Every route change in Next.js is tracked as a pageview without any additional configuration.

## Vercel Deployment Considerations

When deploying on Vercel, your analytics script loads from our EU-based CDN. No data passes through Vercel's infrastructure. This keeps your analytics data fully EU-sovereign even when your Next.js app runs on Vercel's global edge network.

## Adding Goal Tracking

To track custom events like button clicks or form submissions, use the global tracking function:

```tsx
// Track a signup conversion
function handleSignup() {
  window.ca?.('event', { name: 'signup' })
}
```

## Performance Impact

We tested the impact on a production Next.js site:

- Lighthouse Performance score: no change
- First Contentful Paint: +0ms
- Total Blocking Time: +0ms
- Script size: 0.8KB gzipped

Compare this to Google Analytics (45KB+) or even Plausible (1.5KB). Every kilobyte matters for Core Web Vitals.

## Frequently asked questions

### How do I add cookie-free analytics to the Next.js App Router?

Add a Script component from next/script to your root layout in app/layout.tsx, pointing at the ClearAnalytics script with your site ID and strategy="afterInteractive". No consent logic or tag manager is needed.

### Does ClearAnalytics track route changes in a Next.js single-page app?

Yes. ClearAnalytics detects client-side navigation automatically, so every route change counts as a pageview.

### Does analytics data pass through Vercel when my Next.js app runs there?

No. The ClearAnalytics script loads from an EU-based CDN, so no analytics data passes through Vercel's infrastructure.

### Will an analytics script slow down my Next.js site?

Not measurably with ClearAnalytics: on a production Next.js site the script added 0ms to First Contentful Paint and Total Blocking Time. It is 0.8KB gzipped, against 45KB+ for Google Analytics.

Source: https://clearanalytics.eu/blog/how-to-add-analytics-to-your-nextjs-app-without-cookies
