Search code examples
javascriptgoogle-analyticsoutbound

When tracking outbound links with Google Analytics, why delay the outbound click instead of pushing a function into the queue?


The official suggestion for tracking outbound links with (the asynchronous version of) Google Analytics is to push an tracking event into the queue, like:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
setTimeout('document.location = "http://foo.bar"', 100);

Would it not be better to push an anonymous function into the GA queue, like:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
gaq.push(function() { document.location = 'http://foo.bar/'; });

In the setTimeout version, there's no guarantee that the event will be processed before the redirect occurs, whereas in the second version, it would only redirect after the event is processed—right?


Solution

  • The problem with doing your suggestion is that it wouldn't have time to do the request before the page changes.

    The browser won't wait for those 2 events to be complete before navigating the user onwards. If you are familiar with jQuery, it would be similar to adding a click event handler to a link, adding an ajax request to that handler, but not putting an event.preventDefault() in there. In other words, the ajax request would not be handled as the user has already gone to the next page.

    edit as you mentioned in the comments, this is irrelevant if you apply return false to the links as well.

    If you can actually push a function like you illustrated in your example, I really don't see why it wouldn't work better then, with the exception that the first request is timing out for some reason, making the user wait far beyond the 100ms they usually would.

    What about the users that have google blocked? There are lot of addons/programs etc which can completely block out google analytics, adsense etc. will those users have normal user experience?