简体   繁体   English

如何跟踪出站链接的点击

[英]How to track clicks on outbound links

[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/ 我在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 . 我刚刚直接从http://support.google.com/analytics/bin/answer.py?hl=zh_CN&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;" 但是,实际上似乎没有报告我添加了onClick="recordOutboundLink(this, 'Outbound Links', 'Postdoc advert');return false;"的链接的任何点击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? 我究竟做错了什么?

PS The closest related online complaint seems to be http://productforums.google.com/forum/#!topic/analytics/4oPBJEoZ8s4 which just claims the code is broken. 附言:与之最相关的在线投诉似乎是http://productforums.google.com/forum/#!topic/analytics/4oPBJEoZ8s4 ,该投诉仅声称代码已损坏。

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. 我正在使用jQuery将onclick处理程序添加到具有“引荐”类的任何链接中,但是我希望将其直接添加到HTML中也可以正常工作。

  $(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 . 请参阅_gaq.push文档

More precisely: 更确切地说:

  1. The var myTracker declaration is unused, so you can just delete that line. var myTracker声明未使用,因此您只需删除该行。 Variables declared within the scope of recordOutboundLink aren't visible when other functions, such as _gaq.push , are running, so it can't be relevant. 当其他函数(如_gaq.push )正在运行时,在recordOutboundLink范围内声明的变量不可见,因此它不相关。
  2. You should simply use '_trackEvent' instead of 'myTracker._trackEvent' . 您应该只使用'_trackEvent'而不是'myTracker._trackEvent'

您也可以尝试使用此自动外部链接脚本

Set a longer timeout 2 seconds maybe, as it takes a certain amout of time for the _gaq.push to actually push to the server, and 100 milliseconds isnt long enough for it to send (the push gets cancelled as soon as the document.location changes). 可能将超时时间设置为2秒,这是因为_gaq.push实际需要一定的时间才能实际推送到服务器,并且100毫秒的时间不足以发送给服务器(一旦document.location,推送就会被取消变化)。 Unless _gaq.push uses a blocking call (doesnt execute the next line till the push is complete), but i dont think that is the case i think most of that uses asynchronous requests. 除非_gaq.push使用阻塞调用(在推送完成之前不执行下一行),但是我不认为我认为大多数情况下都使用异步请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM