Google Analytics 异步:事件跟踪回调?
我希望使用事件跟踪来记录对另一个网站的特定类型链接的点击。我正在使用 jQuery,我当前拥有的代码是:
$('a.website').click(function(event) {
var href = $(this).attr('href');
try {
_gaq.push(['_trackEvent', 'website', 'click', href]);
} catch(err) {}
});
但是,在看到来自其他站点的引用信息后,我不相信这能够准确跟踪点击,可能是因为 _gaq.push
是异步的,并且在浏览器导航到该 url 之前尚未收到请求,并终止当前页面上运行的任何 javascript。
有什么方法可以检测 _gaq.push
函数已成功完成,以便我可以使用 event.preventDefault()
和 document.location
在事件记录后导航到链接?
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:
$('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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Google Analytics
hitCallback
您可以使用以下方法在跟踪器对象上设置自定义回调:
我在这里更深入地描述了它的代码:点击表单提交后在 Google Analytics 中跟踪事件
Use Google Analytics
hitCallback
You can set a custom callback on the tracker object by using:
I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit
这个答案的解决方案怎么样?
How about solution from this answer?
我在外部链接跟踪方面也遇到过类似的问题。我的救赎是使用
OnMouseDown
事件而不是OnClick
。I've faced similar issue with the external link tracking. My salvation was using
OnMouseDown
event instead ofOnClick
.