Guides

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 1 min read

On this page
  1. Why Cookie-Free Analytics Fits Next.js
  2. Setup with App Router
  3. Setup with Pages Router
  4. SPA Navigation Tracking
  5. Vercel Deployment Considerations
  6. Adding Goal Tracking
  7. Performance Impact

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.

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:

// 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:

// 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:

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

  • developers
  • nextjs
  • analytics
  • privacy

Back to blog Read as markdown

Enjoyed this article?

Try ClearAnalytics for free and get privacy-first analytics for your website.