gaId
- Required
- Yes
- What it does
- Your measurement ID, usually starting with
G-.
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
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.
npm install @next/third-parties@latest next@latestgaIdG-.dataLayerNamedataLayer.debugModenonce 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.
Google Analytics automatically tracks pageviews when the browser history state changes, so client-side navigations between Next.js routes send pageview data without configuration.
Verify the Enhanced Measurement property is enabled in your Admin panel and that the "Page changes based on browser history events" checkbox is selected.
If you send pageview events yourself, disable the default pageview measurement first.
'use client'
import { sendGAEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<button
onClick={() => sendGAEvent('event', 'buttonClicked', { value: 'xyz' })}
>
Send Event
</button>
)
}Run the site with a production build, or open a preview deployment, so you test what visitors get.
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.
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.
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.
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.
The Next.js example places the component inside <html>, after <body>.
If your Content Security Policy uses nonces, pass the nonce prop.
Google Analytics doesn't replace crawl and indexing reports; see the Next.js SEO guides for the rest.
2 questions people ask next, answered from the sources on this page.
Install @next/third-parties, then add <GoogleAnalytics gaId="G-XYZ" /> from @next/third-parties/google to your root layout, using your own measurement ID.
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.
Run the free audit, then approve the fixes. Every change can be undone.