Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Tag Manager causes full page reload in SPA - React

When I add trigger in GTM on a React <a /> or <Link /> element it causes full page reload on click instead of re-rendering just part of application.

When I remove google tracking, everything works smoothly. Is there a way, how to configure GTM to not affect behaviour of application?

like image 804
jirikolarik Avatar asked Apr 21 '16 12:04

jirikolarik


People also ask

How do I get rid of Google Tag Manager debugger?

In the top-right corner click three dots and then go to More tools > Developer Tools. Then go to Application > Cookies > [Your domain]: And in the search field, enter _TAG_ASSISTANT. Once you find that cookie, click it and hit DELETE on your keyboard.

How do you track user behavior on an SPA since it is missing other web pages?

Multiple ways of tracking SPA + How to read this guide Built-in tracking of Google Analytics 4 (this is the quickest one) working with a History Change listener (within GTM) asking a developer to push pageview data to the Data Layer every time a visitor navigates from one page to another.


2 Answers

If strange things happens with Google Tag Manager link click tracking the first thing you should do is to go to your click trigger setup and see if the "wait for tags" and "check validation" checkboxes are enabled (as they are by default) and uncheck them.

They are not necessary in an SPA and (as evidenced) potentially harmful; "wait for tags" adds a delay so that other tags have time to fire before a the link directs a user away from the page (which does not happen within SPAs in any case), "check validation" tests if the link target is a valid URI (which within an SPA it probably isn't by GTMs standards - for example links starting with a hash are not valid etc.).

like image 189
Eike Pierstorff Avatar answered Oct 19 '22 19:10

Eike Pierstorff


You can create separate function and use context router to change route ex:

myFunction(url) {
 this.context.router.push({ 
      pathname: url
    });
}
<Link onClick={this.myFunction.bind(this,url)} ></Link> 

attach this function to onClick of Link tag.

like image 29
Pram Avatar answered Oct 19 '22 19:10

Pram