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

2

6

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?

Angeliqueangelis answered 3/6, 2011 at 17:51 Comment(0)
M
2

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?

Mexico answered 6/6, 2011 at 18:13 Comment(5)
Is the default event not blocked by putting return false; in the anchor's onClick? (In GA's example, <a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">)Angeliqueangelis
@Angeliqueangelis it is yes, making my answer completely irrelevant. Thanks for pointing that outMexico
Not irrelevant: I'd omitted the extra onClick stuff in my question; good to point it out here. Probably better form to preventDefault on the event than to return false anyway.Angeliqueangelis
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.Mexico
Yeah, though I suppose I could add setTimeout('document.location = "http://foo.bar"', 100); as well to cover the cases in which GA does not process the event and function in its queue quickly enough and the cases in which GA has been blocked on the user end of things.Angeliqueangelis
W
0

The best way is to work with the hitCallback function supported by GA. hitCallback is a function that gets called as soon as the hit has been successfully sent.

In your case you could do something like this:

     // if after 300 ms, we still didn't get any action from hitCallback,
     // redirect manually
    setTimeout(function() {
      document.location = 'http://foo.bar/';
    }, 300);

    _gaq.push(['_set', 'hitCallback', function() {
      document.location = 'http://foo.bar/';
    }]);

    _gaq.push(['_trackEvent', 'outbound link ','click', 'http://foo.bar/']);

I made a gist about it here: https://gist.github.com/jonasva/aa20811003e7077360fcb1b297f0311d

Warner answered 10/10, 2016 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.