DEV Community

loading...

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

Collapse
cullylarson profile image
Cully Larson

Does this register a new page visit on all navigation changes?

Collapse
ebereplenty profile image
NJOKU SAMSON EBERE Author

I wouldn’t be too sure but I think it does because from the report on my dashboard, it shows visits to individual pages

Collapse
cullylarson profile image
Cully Larson

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
NJOKU SAMSON EBERE Author

Thank you for this update.

I will check it out