最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding a delay to link clicks for Google Analytics - Stack Overflow

programmeradmin3浏览0评论

I've been fiddling with various snippets of Javascript for the purposes of Google Analytics event tracking. I'm using Google Tag Manager. This question is also similar to some questions I posted recently so apologies to the smaller pool of users who follow the google analytics tag and are seeing the same thing.

Currently I'm working with this snippet:

    <script type="text/javascript"> 

$(document).ready(function(){ 

    $('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);

});
</script>

Within httpfox the event parameters (App, Click, iOS) are all showing. But not in Google Analytics.

I'm told that it is fairly mon practice to add a delay to the link click of anywhere between 5 and 500 milliseconds. This is because, I'm told, that sometimes the browser hits the new site before it's had time to pass the analytics parameters.

There may be alternative means of correcting this but for my own curiosity of learning how to use Javascript for analytics, how would I integrate setTimeout to the above code?

I tried this:

<script type="text/javascript"> 

$(document).ready(function(){ 

 setTimeout(function(){

    $('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);

});
},500);
</script>

But surely this delays the Google Analytics tag from passing the data, rather than delaying the click? I checked and it did not solve my problem.

I've been fiddling with various snippets of Javascript for the purposes of Google Analytics event tracking. I'm using Google Tag Manager. This question is also similar to some questions I posted recently so apologies to the smaller pool of users who follow the google analytics tag and are seeing the same thing.

Currently I'm working with this snippet:

    <script type="text/javascript"> 

$(document).ready(function(){ 

    $('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);

});
</script>

Within httpfox the event parameters (App, Click, iOS) are all showing. But not in Google Analytics.

I'm told that it is fairly mon practice to add a delay to the link click of anywhere between 5 and 500 milliseconds. This is because, I'm told, that sometimes the browser hits the new site before it's had time to pass the analytics parameters.

There may be alternative means of correcting this but for my own curiosity of learning how to use Javascript for analytics, how would I integrate setTimeout to the above code?

I tried this:

<script type="text/javascript"> 

$(document).ready(function(){ 

 setTimeout(function(){

    $('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);

});
},500);
</script>

But surely this delays the Google Analytics tag from passing the data, rather than delaying the click? I checked and it did not solve my problem.

Share Improve this question asked Aug 12, 2013 at 14:56 Doug FirDoug Fir 21.4k54 gold badges192 silver badges341 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Use Google Analytics' hitCallback

You can set a custom callback on the tracker by using the code documented here.

You might end up with something like:

$( ".app-cta a" ).click(function(e) {
        var location = $(this).attr('href'); //get the link location         
        e.preventDefault(); //disable the link action

        _gaq.push(['_set','hitCallback',function() {
            window.location = location; //action link after callback
        }]);

        _gaq.push(['_trackEvent', 'App', 'Click', 'iOS'); //trigger Track Event

        return !window._gat; //fallback in case Google Analytics hasn't loaded.
});

I'm not extremely well-versed with Google Analytics but the code below will execute your _gaq.push() method and then redirect the page 500ms later.

$(document).ready(function() {
  $('.app-cta a').on('click', function (e) {
    var location = $(this).attr('href');

    e.preventDefault();

    _gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);

    setTimeout(function () {
      window.location = location;
    }, 500);
  });
});
Something like that should work
     $(function () {
    $('.app-cta a').click(function (e) {
        e.preventDefault();
        var $this = $(this),
            timer = 500,
            target = $this.attr('target'),
            href = $this.attr('href');
        _gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
        setTimeout(function () {

            if (target) {
                window.open(href, target);
            } else {
                window.location = href;
            }

        }, timer);


    });
});
发布评论

评论列表(0)

  1. 暂无评论