Setting up Google Analytics in Next.js 13 - Not tracking visits

I’m having trouble getting Google Analytics to track page views in my Next.js 13 application. I tried implementing it using the standard setup but the dashboard shows no data at all. When I check the browser developer tools, I can see that the GA script is loading properly and there are no console errors. The tracking code appears to be in place but somehow the events aren’t being sent to Google Analytics. Has anyone else run into this issue with Next.js 13? I’m wondering if there’s something specific about the new app directory structure that might be causing problems with analytics tracking.

Check if your gtag config is actually imported in layout.js. I wasted hours on this same problem - turns out my gtag wasn’t firing on route changes. Next.js 13 does client-side routing, so you’ve got to manually trigger pageview events when people navigate around. I made a custom hook that watches pathname changes and fires the pageview to GA. Also double-check your data streams in the GA4 interface - sometimes the property gets created but the stream settings block events from recording.

had the same prob a while ago! i forgot to turn on enhanced measurement in GA4 settings. also, make sure your measurement ID is the new one starting with G- and not the old UA- one. took me some time to catch that, lol.

Hit this exact nightmare last month during a project migration. The problem was gtag timing - Next.js 13 hydration was beating GA initialization.

Fixed it by moving the GA script to its own component with dynamic imports and ssr: false. Forces GA to load client-side after hydration wraps up.

Also check your Content Security Policy. CSP can silently kill GA requests without any console errors. Wasted a whole day on this before I realized our CSP was blocking the analytics calls.

This video covers most common GA tracking issues:

One more tip - flip on debug mode temporarily. Add debug_mode: true to your gtag config. Shows you exactly what events are firing in real time through the GA debugger.

Sounds like a client-side rendering issue with Next.js 13. I had the same problem when GA tried to load before the component mounted. Wrap your GA setup in a useEffect hook so it only runs client-side. Also make sure you’re manually firing pageview events for route changes - Next.js navigation works differently than regular sites. The app directory has different lifecycle timing, which screws things up. I fixed it by adding a small delay and checking if the window object exists before initializing GA.