Google Analytics异步:事件跟踪回调?

时间:2011-11-16 05:16:48

标签: javascript jquery google-analytics event-tracking

我希望使用事件跟踪来记录指向另一个网站的特定链接类型的点击。我正在使用jQuery,我目前的代码是:

$('a.website').click(function(event) {
    var href = $(this).attr('href');
    try {
        _gaq.push(['_trackEvent', 'website', 'click', href]);
    } catch(err) {}
});

但是,在看到来自其他网站的引荐来源信息之后,我不相信这是准确跟踪点击次数,可能是因为_gaq.push是异步的,并且在浏览器导航到网址之前尚未收到请求,并终止当前页面上运行的任何javascript。

有没有办法检测_gaq.push功能是否已成功完成,因此我可以使用event.preventDefault()document.location导航到之后的链接事件已被记录?

3 个答案:

答案 0 :(得分:12)

使用Google Analytics hitCallback

您可以使用以下命令在跟踪器对象上设置自定义回调:

_gaq.push(['_set', 'hitCallback', function(){}]);

我在这里更深入地描述了它的代码:Track event in Google Analytics upon clicking form submit

答案 1 :(得分:9)

这个answer的解决方案怎么样?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });

答案 2 :(得分:1)

我遇到了与外部链接跟踪类似的问题。我的救赎是使用OnMouseDown事件而不是OnClick