Free auditStart free
GuideNext.js · Analytics

Next.js Google Analytics

Add Google Analytics 4 to a Next.js App Router site with @next/third-parties, send events, avoid duplicate pageviews, and verify hits in Realtime.

3 min
read
4
sources
Oct 30, 2026
updated
Scaplet Editorial Team
author
In one line

In the Next.js App Router, install @next/third-parties, import GoogleAnalytics from @next/third-parties/google, and render <GoogleAnalytics gaId="G-XYZ" /> in your root layout. It loads the Google tag after hydration. Google Analytics tracks client-side route changes itself, so don't add manual pageview code unless you also turn the default off.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode

At a glance

4 facts from the sources below
@next/third-parties
Package
GoogleAnalytics
Component
gaId (G- ID)
Required prop
After hydration, by default
Loads
01Check it

Install and render the component

app/layout.tsx
import { GoogleAnalytics } from '@next/third-parties/google'

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>{children}</body>
      <GoogleAnalytics gaId="G-XYZ" />
    </html>
  )
}

Replace G-XYZ with your measurement ID. Google says a measurement ID is the unique identifier for a web data stream and has the format 'G-' followed by letters and numbers. Find it in Admin, under Data collection and modification, in Data streams, by selecting the Web tab and the stream. To load Google Analytics for a single route instead of the whole site, include the component in that page file.

Install
npm install @next/third-parties@latest next@latest
02Props for GoogleAnalytics

Component options

Prop

gaId

Required
Yes
What it does
Your measurement ID, usually starting with G-.
Prop

dataLayerName

Required
No
What it does
Name of the data layer. Defaults to dataLayer.
Prop

debugMode

Required
No
What it does
Enables Google Analytics debug mode.
Prop

nonce

Required
No
What it does
A nonce, for sites with a Content Security Policy.
03Worth knowing

Send events and avoid duplicate pageviews

The component must be present

sendGAEvent sends events through the dataLayer object and only works if <GoogleAnalytics /> is in a parent layout, page, or component, or in the same file.

Pageviews need no code

Google Analytics automatically tracks pageviews when the browser history state changes, so client-side navigations between Next.js routes send pageview data without configuration.

Check Enhanced measurement

Verify the Enhanced Measurement property is enabled in your Admin panel and that the "Page changes based on browser history events" checkbox is selected.

Manual pageviews duplicate data

If you send pageview events yourself, disable the default pageview measurement first.

A client component that sends an event
'use client'

import { sendGAEvent } from '@next/third-parties/google'

export function EventButton() {
  return (
    <button
      onClick={() => sendGAEvent('event', 'buttonClicked', { value: 'xyz' })}
    >
      Send Event
    </button>
  )
}
04How it works

Check it worked

  1. 01

    Deploy a build, not just dev

    Run the site with a production build, or open a preview deployment, so you test what visitors get.

  2. 02

    Watch Realtime

    In Google Analytics, sign in, select Reports from the left menu, and click Realtime. Open your site in another window, click between two internal links, and look for the page titles in Realtime.

  3. 03

    Turn on debug mode while testing

    Pass debugMode to the component, or enable debug mode for yourself with Google Tag Assistant at tagassistant.google.com. Then open Admin, under Data display, click DebugView to see events from your device.

  4. 04

    Remove debug mode afterward

    Google says to disable debug mode you exclude the debug_mode parameter, and that setting it to false doesn't disable it. Delete the prop rather than passing false.

05Worth knowing

Pitfalls

Router

The documentation this page follows is the App Router guide. If your project uses the Pages Router, confirm the setup in the Next.js Pages Router docs rather than copying this layout file.

Placement

The Next.js example places the component inside <html>, after <body>.

CSP

If your Content Security Policy uses nonces, pass the nonce prop.

Search data

Google Analytics doesn't replace crawl and indexing reports; see the Next.js SEO guides for the rest.

Sources · 4

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • nextjs.org
  • google.com
  1. 01Third Party Libraries | Next.js Documentationnextjs.org · checked 2026-11-01
  2. 02Measurement ID | Analytics Helpsupport.google.com · checked 2026-11-01
  3. 03Realtime report | Analytics Helpsupport.google.com · checked 2026-11-01
  4. 04Monitor events in DebugView | Analytics Helpsupport.google.com · checked 2026-11-01
Questions

Asked a lot.

2 questions people ask next, answered from the sources on this page.

01How do I add Google Analytics to Next.js?

Install @next/third-parties, then add <GoogleAnalytics gaId="G-XYZ" /> from @next/third-parties/google to your root layout, using your own measurement ID.

02Do I need to track pageviews manually in Next.js?

No. Google Analytics automatically tracks pageviews when the browser history state changes, so client-side navigations are measured. Disable the default if you send pageviews yourself.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane