DEV Community


Discussion on: Google Analytics installation - React and Next.js

cullylarson profile image
Cully Larson • Edited

I just tested and it does not register pageviews on all navigation changes, just the initial load. You may have a "history change trigger" set up in Google Tag Manager. That will register a pageview on all history changes. But without that, just adding the GA code to <Head> will not register pageviews on navigation. This is because nextjs doesn't reload the page, it just does a prop update and lets React re-render as it needs to. To get pageviews on navigation changes, you need to do something like listen for the routeChangeComplete nextjs router event and push a pageview when it triggers.

Also, to be clear, nextjs will do a refresh if you use a plain <a> for links, instead of using next/link. So this issue only applies to next/link clicks or pushing onto the router.

Thread Thread
ebereplenty profile image

Thank you for this update.

I will check it out