7

I'm tracking the Download button click on a website featuring a project of mine with this code:

function trackDownload(link) {
    try {
        _gaq.push(['_trackEvent', 'Downloads', 'Click', 'Setup executable']);
        setTimeout('document.location = "' + link.href + '"', 100);
    } catch (err) {}
    return false;
}

And the button is something as:

<a href="files/setup.exe" onclick="return trackDownload(this);">Download</a>

So, when a user clicks it, an event is pushed to Analytics and then the user is redirected to the file.

This is applicable also on external link tracking, no differences.

And now my question. Can I be sure that the Analytics event is "processed" before the user is redirect? If not, that redirection cause the event to be lost? Currently events are being tracked, but I cannot be sure that all of them are.

I read I can also try something a little bit different, pushing the redirection function into Analytics queue:

_gaq.push(function() { document.location = link.href; });

But it's not clear if this works or if it's just equivalent to the previous one. In fact, here it's said that "calls to _gaq.push [...] executes commands as they are pushed".

1 Answer 1

7
+50

You are correct in that you can push functions onto the analytics queue. Since functions or tracking events are executed/evaluated in the order that you pushed them on to the array, you should be able to do this:

function trackDownload(link) {
    _gaq.push(['_trackEvent', 'Downloads', 'Click', 'Setup executable']);
    _gaq.push(function() { document.location = link.href });
    return false;
}

Note that the try/catch isn't necessary since push() isn't documented to throw anything (and I'd recommend removing it since empty catch blocks can mask other problems).

You ask:

But it's not clear if this works or if it's just equivalent to the previous one.

In your first example (push, setTimeout), the event will be lost if Analytics hasn't finished loading when you do the redirect (because at that time, _gaq is just an array). In the version with the push(function..., then the event will be recorded before the redirect regardless of whether Analytics has finished loading at the time the user hits the download button. For this reason, I would recommend using push(function....

Be warned that the push(function... version will wait for analytics to finish loading before the redirect happens (which sounds like what you want anyway), but you may want to add a way to handle the case where analytics doesn't load.

4
  • Maybe I can push redirection in the _gaq as I posted, and then repeat it in the catch block and in a more delayed setTimeout (one or two seconds). So if something go wrong with _gaq, I can be sure user will be redirected sooner or later.
    – lorenzo-s
    Commented Dec 20, 2011 at 7:49
  • I agree that a delayed setTimeout is the way to handle the no analytics case. However, I think one or two seconds is probably a bit quick. Also, I just noticed that the catch block isn't needed - I'll update my answer. Commented Dec 20, 2011 at 14:03
  • In fact, I do not remember why I put that try..catch block :) About the timeout: I can set a longer one (5 secs) and a spinner on the button when clicked. So the faster user won't have the feeling that nothing is happening...
    – lorenzo-s
    Commented Dec 20, 2011 at 14:09
  • I like it :) In my opinion, the timeout should be long enough to give analytics reasonable time to load, but not long enough that the user goes away. 5 seconds sounds fine (but I'm not aware of studies). Make sure you disable the button on click as well - otherwise you might get multiple analytics events. Commented Dec 20, 2011 at 14:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.