Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking a download button click with Analytics using events

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".

like image 307
lorenzo-s Avatar asked Dec 12 '11 21:12

lorenzo-s


People also ask

Can Google Analytics track a button click?

You can use Google Tag Manager to track button clicks into Google Analytics without needing to modify the code on your website. I'm going to walk you through tracking clicks on a button used in a form, but you can use the same technique for tracking buttons in your navigation, banners, content, and more.

Can Google Analytics track file downloads?

You can set Google Analytics up to track downloads automatically. Depending on your website's setup and your resources, there are a number of options for tracking file downloads.

Can you track PDF clicks in Google Analytics?

In short, yes. Prior to Google Analytics 4, you could not automatically track downloads of your content. However, setting up pdf tracking via Universal Analytics and Google Tag Manager was simple enough. Now, with Google Analytics 4, you can turn on pdf tracking as part of your reports, with zero code changes required.


1 Answers

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.

like image 142
Timothy Jones Avatar answered Sep 24 '22 23:09

Timothy Jones