How to Integrate Google Analytics in Next.js (GA4, the Right Way)

By Malik Kashanโ€ขOctober 2026โ€ข5 min read

Add GA4 to Next.js App Router with @next/third-parties, fix Content-Security-Policy blocking, and keep your PageSpeed score intact.

The Clean Way: @next/third-parties

Don't paste raw gtag.js script tags into your layout โ€” you'll manage script loading, consent and CSP yourself. Install the official package: npm install @next/third-parties google.

In app/layout.jsx: import { GoogleAnalytics } from '@next/third-parties/google' and render <GoogleAnalytics gaId="G-XXXXXXXXXX" /> inside the html element. That's it โ€” the component handles script injection, the gtag stub and page-view tracking across client navigations automatically.

Getting Your Measurement ID

In the Google Analytics dashboard: Admin โ†’ Data Streams โ†’ create a Web stream for your site. The Measurement ID (format G-XXXXXXXXXX) appears at the top of the stream details. Copy exactly โ€” a wrong ID fails silently, with no errors anywhere.

Put the ID in an environment variable (NEXT_PUBLIC_GA_ID) rather than hardcoding it, so staging and production can use different properties.

Fixing 'Script Blocked by Content-Security-Policy'

If you set security headers in next.config.mjs, a strict CSP will silently block GA. The console shows 'Loading the script violates the following Content-Security-Policy directive: script-src'. The fix is allowing the analytics domains explicitly.

Add to script-src: https://www.googletagmanager.com (loads the script) and https://va.vercel-scripts.com if you also run Vercel Analytics. Add to connect-src: https://www.google-analytics.com and your regional endpoint (region1.google-analytics.com) โ€” this is where beacons are sent. Add the same domains to img-src for tracking pixels. Changing CSP requires a full redeploy.

Verifying It Actually Works

Open your site, then in the GA dashboard check Reports โ†’ Realtime โ€” your own visit should appear within a minute. Also check the browser Network tab for a request to google-analytics.com with status 200, and the console for CSP violations.

For deeper verification, install the Google Analytics Debugger extension โ€” it logs every hit your browser sends, including events and their parameters.

Keeping Performance Intact

The @next/third-parties component loads GA with proper async handling, so it doesn't block rendering. Still, keep your site lean: analytics is one more script, and every script costs a little. Don't stack three analytics tools when one will do.

Test your PageSpeed score after adding analytics โ€” if LCP or INP regressed, the culprit is usually script loading order, and facades or delayed loading can fix it.

Conclusion

Five minutes of setup, one component, three CSP domains โ€” and you have production-grade analytics on a Next.js site. Verify in Realtime reports, and keep an eye on performance after any analytics change.

I set up analytics, CSP and performance monitoring as standard on every client build. Want your site properly instrumented? Let's talk.

Want help with your project?

I build fast, modern and SEO-ready websites. Let's talk about what you need โ€” I usually respond within a few hours.

Get in Touch โ†’
Malik Kashan Official Portfolio Branding Logo

Malik Kashan

Frontend & WordPress Developer ยท Lahore, Pakistan

I build fast, SEO-ready websites for startups and businesses worldwide using Next.js, React and WordPress.

Hire Me for Your Project