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

时间:2022-01-13 15:21:06

I wish to use event tracking to record clicks on a specific type of link to another website. I am using jQuery, and the code I currently have is:

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

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

However, after seeing referrer information from the other site, I don't belive this is accurately tracking the clicks, probably because _gaq.push is asynchronous, and the request has not been received before the browser navigates to the url, and terminates any javascript running on the current page.

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

Is there any way to detect that the _gaq.push function has completed successfully, so I can use event.preventDefault() and document.location to navigate to the link after the event has been recorded?

有没有办法检测_gaq.push函数是否已成功完成,所以我可以使用event.preventDefault()和document.location在事件被记录后导航到链接?

3 个解决方案

#1


9  

How about solution from this 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


12  

Use Google Analytics hitCallback

You can set a custom callback on the tracker object by using:

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

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

I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit

我在这里更深入地描述了它的代码:点击表单提交后在Google Analytics中跟踪事件

#3


1  

I've faced similar issue with the external link tracking. My salvation was using OnMouseDown event instead of OnClick.

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

#1


9  

How about solution from this 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


12  

Use Google Analytics hitCallback

You can set a custom callback on the tracker object by using:

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

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

I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit

我在这里更深入地描述了它的代码:点击表单提交后在Google Analytics中跟踪事件

#3


1  

I've faced similar issue with the external link tracking. My salvation was using OnMouseDown event instead of OnClick.

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

相关文章