-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Tag Manager - Tags (type: event) stop working when browsing #4077
Comments
#4036 will probably help you, either way it's not an issue with the Next.js core. |
@timneutkens I'm aware of the PR but it doesn't aim to fix this at all. Can we reopen this issue? It's definitely not related to Next.js core (AFAICT), but I believe other people will run into this as well. It's likely caused by the way GTM works, which isn't SPA-friendly. For instance, I was wondering if removing the GTM script tag from the DOM and add it again at every page load would somehow force GTM to reload upon route changes, I wonder if I can trigger this behaviour in the router directly. |
Feel free to ask your questions on https://spectrum.chat |
I found a workaround for anyone interested, here is the implementation for Next.js: Router.onRouteChangeComplete = () => {
// XXX Destroy all data of GTM, which will make it refresh all bindings (events) the next time GTM.initialize is called
// This is a hack to make GTM works with SPA, because otherwise events don't trigger because DOM events are removed when Next.js load dynamic parts of the DOM
delete window.google_tag_manager;
};
See SO for detailed explanation: https://stackoverflow.com/questions/49557199/google-tag-manager-tags-type-event-stop-working-when-browsing-on-a-spa |
Expected Behavior
GTM tags should work the same way on every page, whether the user arrived at this page or browsed to it.
Current Behavior
GTM tags that use the
event
type stop working properly as soon as a user browse the website. By "browse", I mean using client-side navigation, which doesn't refresh the page but only part of it (kinda the point of SPA)Steps to Reproduce (for bugs)
You can reproduce on my online staging environment:
Context
I believe that somehow GTM binds some DOM events upon loading that get cleaned when the DOM changes due to Next.js redirection, which reloads the DOM, partially.
Those GTM tags (type: event) I'm talking about are basically triggers based on HTML class/id in the DOM. When I use the GTM Preview mode (debug), I can see that there is no gtm.linkClick fired when clicking the logo, if I got redirected from another page using a frontend-redirection. But if I refresh the page and click, it works just fine.
The simplest workaround I see is to disable client-side redirection but forcing a page refresh, but that's not a nice workaround and kills the point of building an SPA in the first place.
Maybe there is a way to force refresh the GTM script (need to clean it from the DOM and add it again because it won't trigger if already loaded), or to use a feature within GTM that binds everything again? I don't know.
Your Environment
The text was updated successfully, but these errors were encountered: