Mat*_*tin 13 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) {}
});
Run Code Online (Sandbox Code Playgroud)
然而,从其他网站看到引荐信息后,我也不敢相信这是准确跟踪点击,可能是因为_gaq.push
是异步的,并请求的浏览器导航到URL之前尚未收到,并终止任何JavaScript的运行当前页面.
有没有办法检测到该_gaq.push
功能是否已成功完成,所以我可以在记录事件后使用event.preventDefault()
和document.location
导航到该链接?
flu*_*flu 12
hitCallback
您可以使用以下命令在跟踪器对象上设置自定义回调:
_gaq.push(['_set', 'hitCallback', function(){}]);
Run Code Online (Sandbox Code Playgroud)
我在这里更深入地描述了它的代码:在点击表单提交时跟踪Google Analytics中的事件
这个答案的解决方案怎么样?
// 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; });
Run Code Online (Sandbox Code Playgroud)