Skip to content
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

Closed
1 task done
Vadorequest opened this issue Mar 29, 2018 · 4 comments
Closed
1 task done

Comments

@Vadorequest
Copy link
Contributor

Vadorequest commented Mar 29, 2018

  • I have searched the issues of this repository and believe that this is not a duplicate.

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.

image

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

Tech Version
next 5.0.1-canary.16
node 6.10.3
browser Chrome
@timneutkens
Copy link
Member

#4036 will probably help you, either way it's not an issue with the Next.js core.

@Vadorequest
Copy link
Contributor Author

@timneutkens I'm aware of the PR but it doesn't aim to fix this at all.
The PR is about Google Analytics, while this issue is with Google Tag Manager, which is a different product (event though it can load GA)
Also, the PR is to fix the "pageview" issue with SPA, it's a different feature that the "events".

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.
The goal is to find a workaround, or, even better, a solution.

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.

@timneutkens
Copy link
Member

Feel free to ask your questions on https://spectrum.chat

@Vadorequest
Copy link
Contributor Author

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;
};

GTM.initialize comes from https://github.com/Vadorequest/react-gtm but you don't need it, you just need to make sure that you load the GTM script in the DOM after you delete the window.google_tag_manager variable, to force re-binding DOM events.

See SO for detailed explanation: https://stackoverflow.com/questions/49557199/google-tag-manager-tags-type-event-stop-working-when-browsing-on-a-spa

@lock lock bot locked as resolved and limited conversation to collaborators Mar 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants