最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tracking a download button click with Analytics using events - Stack Overflow

programmeradmin2浏览0评论

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 mands as they are pushed".

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 mands as they are pushed".

Share asked Dec 12, 2011 at 21:34 lorenzo-slorenzo-s 17k15 gold badges58 silver badges88 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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 remend 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 remend 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.

发布评论

评论列表(0)

  1. 暂无评论