Adding Google Analytics 4 to a Next.js App Router project looks like a two-line job. Then the client asks why page views are a third of what they expected, or why the cookie banner doesn't actually stop tracking. Here's the setup I use and the issues I fix most often.
1. Keep the Measurement ID in an env variable
# .env.local NEXT_PUBLIC_GA4_ID=G-XXXXXXXXXX Pick one name and use it everywhere. A surprising number of bugs are just two different variable names in two files.
2. Load gtag once, in the root layout
// app/layout.tsx import Script from 'next/script'; import { Analytics } from './components/analytics'; const GA_ID = process.env.NEXT_PUBLIC_GA4_ID; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> {children} {GA_ID && ( <> <Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} strategy="afterInteractive" /> <Script id="ga4-init" strategy="afterInteractive">{` window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('consent', 'default', { analytics_storage: 'denied', ad_storage: 'denied' }); gtag('js', new Date()); gtag('config', '${GA_ID}', { send_page_view: false }); `}</Script> <Analytics /> </> )} </body> </html> ); } Note send_page_view: false — we'll send page views ourselves so the first load isn't counted twice.
3. Track client-side navigation
App Router navigation doesn't reload the page, so a static script only sees the first load. A small client component fixes that:
// app/components/analytics.tsx 'use client'; import { usePathname, useSearchParams } from 'next/navigation'; import { useEffect } from 'react'; export function Analytics() { const pathname = usePathname(); const searchParams = useSearchParams(); useEffect(() => { const url = pathname + (searchParams.toString() ? `?${searchParams}` : ''); window.gtag?.('event', 'page_view', { page_path: url, page_location: window.location.href }); }, [pathname, searchParams]); return null; } Because it uses useSearchParams, wrap it in a <Suspense> boundary if your pages are statically rendered, or Next.js will complain at build time.
4. Make the cookie banner actually do something
With Consent Mode v2, start with denied (as above) and update when the user accepts:
window.gtag('consent', 'update', { analytics_storage: 'granted' }); Store the choice and re-apply it on the next visit before any events fire.
5. Conversions that matter for a business site
Page views are nice; enquiries pay the bills. Fire explicit events for form submissions, WhatsApp clicks and phone clicks, and mark them as key events in GA4:
window.gtag('event', 'generate_lead', { method: 'contact_form' }); Common mistakes
- Testing in
next devand trusting the numbers — verify with a production build and GA4 DebugView - Double page views from leaving
send_page_viewon and also sending them manually - Forgetting search params, so filtered pages all look the same in reports
- Banner that hides visually but never updates consent
I wrote a longer walkthrough of the base setup here: GA4 with the Next.js App Router.
I'm Arun Tyagi, a freelance full-stack developer in Noida, India. I build Next.js and React applications with Laravel back ends.
