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

Canonical: https://scaplet.com/seo/nextjs/google-analytics

Guide Next.js Next.js · 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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fnextjs%2Fgoogle-analytics%20(%22Next.js%20Google%20Analytics%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fnextjs%2Fgoogle-analytics%20(%22Next.js%20Google%20Analytics%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fnextjs%2Fgoogle-analytics%20(%22Next.js%20Google%20Analytics%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fnextjs%2Fgoogle-analytics%20(%22Next.js%20Google%20Analytics%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

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

_01_Check 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](/glossary/data-streams) 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](/glossary/google-analytics-4) for a single route instead of the whole site, include the component in that page file.

**Already using Google Tag Manager?**Next.js recommends configuring Google Analytics through [Tag Manager](/glossary/google-tag-manager) in that case, rather than including Google Analytics as a separate component.

Install

```
npm install @next/third-parties@latest next@latest
```

_02_Props 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](/glossary/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.

_03_Worth 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> ) }
```

_04_How it works

## Check it worked

**Consent can hide your test events**Google says events are not visible in debug mode if you've implemented consent mode and users have not given consent for Analytics cookies. Accept analytics in your test browser before concluding the install is broken.

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.

_05_Worth 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](/seo/nextjs) 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. 01 [Third Party Libraries | Next.js Documentation](https://nextjs.org/docs/app/guides/third-party-libraries) nextjs.org · checked 2026-11-01
2. 02 [Measurement ID | Analytics Help](https://support.google.com/analytics/answer/12270356) support.google.com · checked 2026-11-01
3. 03 [Realtime report | Analytics Help](https://support.google.com/analytics/answer/9271392) support.google.com · checked 2026-11-01
4. 04 [Monitor events in DebugView | Analytics Help](https://support.google.com/analytics/answer/7201382) support.google.com · checked 2026-11-01

Questions

## Asked a lot.

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

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

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

## Keep learning

[All platform guides](/seo)

[Learn Next.js SEO Frameworks](/seo/nextjs) [Related Google Analytics 4 Measurement](/glossary/google-analytics-4) [Read next Squarespace Google Analytics CMS](/seo/squarespace/google-analytics) [Do Run a free site audit Free, about two minutes](/site-audit)

More in Next.js SEO
[Next.js 404 page](/seo/nextjs/404) [Next.js AI crawlers](/seo/nextjs/ai-crawlers) [Next.js canonical tags](/seo/nextjs/canonical-tags) [Next.js Google Analytics](/seo/nextjs/google-analytics) [Next.js image SEO](/seo/nextjs/image-seo) [Next.js lazy loading](/seo/nextjs/lazy-loading) [Next.js noindex](/seo/nextjs/noindex) [Next.js redirects](/seo/nextjs/redirects) [Next.js robots.txt](/seo/nextjs/robots-txt) [Next.js structured data](/seo/nextjs/structured-data) [Next.js title and meta description](/seo/nextjs/title-and-meta) [Next.js XML sitemap](/seo/nextjs/xml-sitemap)

Terms on this page
[Data streams](/glossary/data-streams) [Google Analytics 4](/glossary/google-analytics-4) [Google Tag Manager](/glossary/google-tag-manager) [Data layer](/glossary/data-layer)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
