使用通用分析跟踪出站链接

时间:2014-03-03 17:37:47

标签: javascript google-analytics

我刚刚将以下“通用”分析代码添加到[e head] [1],但无法找到如何设置标准的通用出站跟踪事件:

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxx-Y', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->

这是建议:

var trackOutboundLink = function(url) {

      _gaq.push(['_trackEvent', 'Outbound', 'Click', this.href]);
      setTimeout('document.location = "' + this.href + '"', 100);
      return false;
}

这适用于通用出站事件跟踪吗?它是否跟踪所有事件以及它应放在何处 - 在头部或在关闭身体标签之前?

这是跟踪个别通用事件的正确语法吗?

onclick="trackOutboundLink('/WEBSITE/www.something.com')

4 个答案:

答案 0 :(得分:1)

来源:https://github.com/tomfuertes/jquery-universal-analytics/blob/master/src/jquery.universal-analytics.js

$(document).on('mousedown', 'a', function () {
  if ((this.protocol === 'http:' || this.protocol === 'https:') && this.hostname.indexOf(document.location.hostname) === -1) {
    ga('send', 'event', 'Outbound', this.hostname, this.pathname);
  }
});

答案 1 :(得分:0)

您需要阅读文档。 _gaq.push(['_trackEvent'])不再有效。你必须使用新功能; ga('send', 'event', 'button', 'click', 'nav buttons', 4);

Event Tracking - Web Tracking (analytics.js)

答案 2 :(得分:0)

Universal Analytics事件跟踪已更改。

这是new event tracking code

ga('send', 'event', 'category', 'action', 'opt_label', opt_value); // value is a number

所以重写你的代码看起来像这样:

var trackOutboundLink = function(url) {

      ga('send', 'event','Outbound', 'Click', this.href);
      setTimeout('document.location = "' + this.href + '"', 100);
      return false;
}

然后,您可以将其附加到onclick=的链接或创建一个javascript事件侦听器。我经常做事件监听器,因为我尝试远离混合html和JS。

答案 3 :(得分:0)

您不想使用.on('MouseDown'),因为它无法准确跟踪。它仍会错过较慢连接上的点击事件,如果用户将鼠标拖下来然后拖走,则会报告虚假点击。

NikZilla provides an answer here使用.on('click')使用jquery和Universal Analytics hitCallback function。我已将其与跟踪所有出站链接as referenced in another answer的功能相结合。

<script>
$(document).on('click', 'a', function (e) {
  if ((this.protocol === 'http:' || this.protocol === 'https:') && this.hostname.indexOf(document.location.hostname) === -1) {
    var obj = $(this);
    e.preventDefault();
    ga('send', 'event', 'Outbound', this.hostname, this.pathname, 0, {
                        'hitCallback': function() {
                            obj.off(e);
                            obj[0].click();
                            }
                    });
  }

});

</script>
相关问题