Google Analytics: delay needed for tracking link clicks?

旧城冷巷雨未停 提交于 2020-01-03 19:37:43

问题


We're trying to track various click events on our pages to see how users are navigating our site.

It's possible to arrive at given page via different links (e.g. via a link in the top of the originating page vs one in the footer). For this reason it's not sufficient to merely track that the destination page loaded; we need to tag and track the click events.

The Google Analytics documentation recommends adding a 100ms delay for clicks on "outbound links", in order for the tracking code to complete before loading the link target. Is this because the _gaq.push(['_trackEvent', category , action]) code is asynchronous, and needs time to complete before the page is unloaded?

If so, wouldn't this also be required for "on site" links? I fail to see how this is different from a link to a new page on the same site; in both cases the current page is unloaded.

Edit: I've since discovered Google's hitCallback mechanism for firing your page load events via callback. This obviates the need to use a delay.


回答1:


Any tracking that is needing to be done just before a new page should include a slight ( < 200ms) delay. Offsite, onsite, form submit, etc. This allows the request to the analytics servers to be completed.

As far as internal link tracking, have you looked at the In-Page Analytics report & Enhanced Link Attribute plugin? It could help you out a bit without needing to do extra coding.




回答2:


Don't delay clicks. Even the 250ms delay wont guarantee a successful tracking. If the target url is inside your domain, just store the tracking info on the local.storage and check on every page if there's something on the storage and trigger the ga event for the clicked button on page load instead. You should also validate if there's local.storage available on the client, and if not you can then use, in those cases, the click delay.




回答3:


Here is the Jquery to create the delay:

$("a").click(function (e) {        
    e.preventDefault(); //cancel the link click
    var url = $(this).attr('href');//get the destination url of the link        
    _gaq.push(['_trackEvent', 'Links', 'Clicked', 'Buy']); //do your tracking
    //go to the original destination url after a delay
    setTimeout(function () { window.location.href = url; }, 250); 
});



回答4:


Yes, delay is needed to make sure GA request is finished before page is reloaded.

Here is the Vanilla JS code to implement the tracking and delay:

document.getElementById('ID').addEventListener('click', function(event) {
    event.preventDefault();
    _gaq.push([ '_trackEvent', 'category', 'action', 'label' ]);
    setTimeout( function() {
        document.location = event.target.href;
    }, 200);
});


来源:https://stackoverflow.com/questions/14611899/google-analytics-delay-needed-for-tracking-link-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!