Google Analytics(分析)hitCallback事件跟踪无法正常工作

时间:2014-03-30 10:55:21

标签: google-analytics

我正在尝试使用事件跟踪和hitcallback来跟踪被点击的链接。我的分析代码看起来像这样......

<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>

我想跟踪的链接看起来像这样......

<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/的hitcallback示例,但它仍无效。

我错过了什么吗?

1 个答案:

答案 0 :(得分:8)

问题是您尝试使用通用分析语法(analytics.js)进行点击跟踪,但您使用的GA库是较早的_gaq(ga.js)语法。

_gaq样式确实允许您指定回调函数,尽管它没有记录。基本上,在进行_set调用之前,您必须_trackEvent回调函数。

您的链接应如下所示:

<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>