Google Analytics hitCallback 事件跟踪不起作用 [英] Google Analytics hitCallback event tracking not working
问题描述
我正在尝试使用事件跟踪和 hitcallback 来跟踪被点击的链接.我的分析代码看起来像这样...
I am trying to track a link being clicked using event tracking and hitcallback. My analytics code looks like this...
<script type="text/javascript">//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
//]]></script>
我试图跟踪的链接看起来像这样......
And the link I am trying to track looks like this....
<a href='http://cool.com/' onclick="var href = this.href; ga('send','event','outbound','click', this.href, {'hitCallback': function(){document.location = href;}}); return false;">Cool</a>
我遵循了 http://www.swellpath.com/2013/12/universal-analytics-using-hitcallback/ 但它仍然无法正常工作.
I followed the hitcallback example from http://www.swellpath.com/2013/12/universal-analytics-using-hitcallback/ but its still not working.
我错过了什么吗?
推荐答案
问题是您正尝试使用 Universal Analytics 语法 (analytics.js) 进行点击跟踪,但您使用的 GA 库是较旧的 _gaq
(ga.js) 语法.
The problem is you are attempting to use Universal Analytics syntax (analytics.js) for your click tracking, but the GA library you are using is the older _gaq
(ga.js) syntax.
_gaq
样式确实允许您指定回调函数,尽管它没有记录.基本上你必须在调用 _trackEvent
之前_set
回调函数.
_gaq
style does let you specify a callback function, though it's undocumented. Basically you have to _set
the callback function before you make the _trackEvent
call.
您的链接应如下所示:
<a href='http://cool.com' onclick="var _this=this;_gaq.push(['_set','hitCallback',function(){document.location=_this.href;}]);_gaq.push(['_trackEvent','outbound','click',_this.href]);return false;">cool</a>
这篇关于Google Analytics hitCallback 事件跟踪不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!