简体   繁体   中英

setTimeout for Google Analytics tracking

ADDED after posting. When a visitor clicks on an exit link on a site and that exit link is tagged in an analytics tool (in this case Google Analytics) sometimes the event data are not passed in time to the analytics tool by the time the browser hits the new site. A common solution is to add a delay to the link in order to allow the analytics tool time to pick up the data. Since asking my question below I experimented with how much of a delay to add. Initially I was told 5 milliseconds. In the end I had to go up to 500 milliseconds

I had set up some custom html in Google Tag Manager in order to track events.

The original code I was using is here:

    <script type="text/javascript"> 


$(document).ready(function(){ 

    $('.app-cta a').click(function(){ 
        dataLayer.push({ 'event':'GAevent', 'eventCategory':'App', 'eventAction':'Click to App Store', 'eventLabel':'iOS' }) 
    }); 

});

</script>

This was failing to capture the event Categories, Actions and Label within Google analytics. I was however picking these up in Httpfox . A colleague suggested adding a 5 millisecond delay . I'm not sure of the logic but was told this is a common solution to my problem.

Using Google and W3 Schools I think I've figured out how to add the 5 Millisecond delay:

    <script type="text/javascript"> 

setTimeout(function(){
$(document).ready(function(){ 

    $('.app-cta a').click(function(){ 
        dataLayer.push({ 'event':'GAevent', 'eventCategory':'App', 'eventAction':'Click to App Store', 'eventLabel':'iOS' }) 
    }); 

});
},5);

</script>

This does not appear to have worked. Google Analytics is not picking up the event data but Httpfox still is. Since I'm not experienced with Javascript I wondered if the second code block was even correct - have I correctly added the setTimeout method?

It seems to me that you mixed up Google Analytics event tracking and Google Tag Manager events. For tracking GA event in comparison with GTM event you should:

  1. Create HTML tag in GTM like this:

     <script type="text/javascript"> _trackEvent('event':'GAevent', 'eventCategory':'App', 'eventAction':'Click to App Store', 'eventLabel':'iOS' }); </script> 
  2. Create for this tag rule "{{event}} contains GAevent_track"
  3. Save tag and display it on pages, where you want to track links. Publish the new version
  4. Make links you want to track like this : <a href="#" name="button_event" onclick="dataLayer.push({'event': 'GAevent'});" Event tracking</a> <a href="#" name="button_event" onclick="dataLayer.push({'event': 'GAevent'});" Event tracking</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM