如何跟踪点击出站链接 [英] How to track clicks on outbound links
问题描述
[在Google产品论坛上交叉发布http://productforfors.google.com/d/topic/analytics/ZrB14a-6gqI/discussion]
我在 http://www.cs.bris.ac.uk/Research/Algorithms/
< script type =text / javascript>
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>
< script type =text / javascript>
函数recordOutboundLink(link,category,action){
try {
var myTracker = _gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent',category,action]);
setTimeout('document.location =''+ link.href +'',100)
} catch(err){}
}
< / script>
我刚从 http://support.google.com/analytics/bin/answer.py?hl=zh_CN&answer=1136920 。
然而,实际上它没有报告任何点击链接,我添加了 onClick =recordOutboundLink(this,' Outbound Links','Postdoc advert'); return false;
,例如。我在网上看到了一些投诉,但我还没有找到可行的解决方案。
我做错了什么?
PS最接近的相关在线投诉似乎是 http://productforums.google.com/forum /#!topic / analytics / 4oPBJEoZ8s4 ,其中声称代码已损坏。
使用,这一直在为我工作。我使用jQuery将onclick处理程序添加到任何具有referral类的链接,但我希望直接将它添加到HTML中以便正常工作。
< pre $
$(function(){
$('。referral')。click(function(){
_gaq.push(['_ trackEvent','Referral ','Click',this.href]);
setTimeout('document.location =''+ this.href +'',100);
return false;
}) ;
});
编辑:我相信您调用跟踪器的语法按名称是错误的。由于在页面加载时设置跟踪时您没有使用指定的跟踪器,因此您不应稍后尝试命名。请参阅 _gaq.push的文档
更精确地说:
-
var myTracker
声明未使用,因此您可以删除该行。当> recordOutboundLink
范围内声明的变量在其他函数(例如_gaq.push
)正在运行时不可见,所以它是不相关的。 - 您应该简单地使用
'_ trackEvent'
而不是'myTracker。 _trackEvent'
。
[cross-posted on Google Products Forum http://productforums.google.com/d/topic/analytics/ZrB14a-6gqI/discussion ]
I am using the following code at http://www.cs.bris.ac.uk/Research/Algorithms/
<script type="text/javascript">
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>
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
</script>
which I just copied directly from http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920 .
However, it doesn't actually seem to report any clicks on the links where I have added onClick="recordOutboundLink(this, 'Outbound Links', 'Postdoc advert');return false;"
, for example. I have seen a number of complaints about this online but I haven't found a solution that works.
What am I doing wrong?
P.S. The closest related online complaint seems to be http://productforums.google.com/forum/#!topic/analytics/4oPBJEoZ8s4 which just claims the code is broken.
Here's what I'm using, which has been working for me. I'm using jQuery to add the onclick handler to any link with a class of "referral", but I'd expect adding it directly in the HTML to work as well.
$(function() {
$('.referral').click(function() {
_gaq.push(['_trackEvent', 'Referral', 'Click', this.href]);
setTimeout('document.location = "' + this.href + '"', 100);
return false;
});
});
edit: I believe your syntax for invoking a tracker by name is wrong. Since you aren't using a named tracker when you set up tracking at page load, you shouldn't try to name it later either. See the documentation for _gaq.push
.
More precisely:
- The
var myTracker
declaration is unused, so you can just delete that line. Variables declared within the scope ofrecordOutboundLink
aren't visible when other functions, such as_gaq.push
, are running, so it can't be relevant. - You should simply use
'_trackEvent'
instead of'myTracker._trackEvent'
.
这篇关于如何跟踪点击出站链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!