我对这里描述的代码进行了轻微修改,以便跟踪外部链接的点击。这是我的代码:
function recordOutboundLink(link, label) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', 'Outbound Links', label, link.href ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
然后我像这样将它添加到我的href标签中:
<a href="http://example.com/user/5" onclick="recordOutboundLink(this, 'example.com');return false"></a>
我认为它不起作用,但很难检查它是否有效。如何检查它是否起作用?
编辑:使用chrome的Google Analytics调试器,我能够看到发生了什么。它显示:
Account ID : UA-XXXXX-X
之所以奇怪,是因为正常的页面跟踪按预期工作。这是我设置 Google Analytics 代码的方式:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'my-id']);
_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);
})();