【问题标题】:Track outbound links using Universal analytics使用通用分析跟踪出站链接
【发布时间】:2014-03-03 17:37:47
【问题描述】:

我刚刚向 th[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')

【问题讨论】:

    标签: javascript google-analytics


    【解决方案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);
      }
    });
    

    【讨论】:

    • 这似乎很好地解决了问题,并且应该是公认的答案,假设将 jQuery 作为要求是可以接受的。
    【解决方案2】:

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

    Event Tracking - Web Tracking (analytics.js)

    【讨论】:

      【解决方案3】:

      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 混合在一起。

      【讨论】:

      • 是的,我只是复制了您列出的功能,它似乎有点奇怪。我在这个 jsfiddle jsfiddle.net/nicomiceli/CRSBc 中重写了它,只是取消注释 ga 代码并注释掉 console.log - 我为调试器添加了它。 (只需确保您在网站上有 jquery。)
      【解决方案4】:

      您不想使用.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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 1970-01-01
        相关资源
        最近更新 更多