网站上有多个Google Analytics(分析)代码

时间:2018-11-19 20:44:03

标签: google-analytics analytics analytics.js

我正在努力更新一个似乎运行了多个实例和版本的Google Analytics(分析)的网站。我需要帮助来确定实际发生的情况以及应该保留的版本。

首先将它们装入头部:

 <script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
    <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"></script>

然后,下面的这些代码也将在头部运行。

  var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
    _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);
    })();



 window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-6100216-1');

然后,在体内加载并调用gtag。

  <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-1"></script>

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-xxxxxx-1');

我已经纠正了过去开发人员犯的一千个错误。

2 个答案:

答案 0 :(得分:0)

天哪,这是一些工作!从总体上讲,您具有GA实现的所有 THREE(3)方法:ga.js,analytics.js,gtag.js。当前推荐的方法是gtag.js。

ga.js(旧版)

这是应该将与ga.js相关联的所有内容分组(as per documentation)的方式:

  1. 这是ga.js库的加载行: <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"></script>

  2. 这是启动ga.js的代码:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
    _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);
    })();
    

analytics.js

根据documentation,正常的异步实现如下所示:

<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

但是无论出于什么原因,看起来您只是在加载库,而没有将其余的都初始化。因此,对于analytics.js,您只需加载该库,而不会对其进行任何其他操作。

gtag.js

再次as per documentation,gtag实现代码如下:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_TRACKING_ID');
</script>

在这种情况下,页面使用了两次,如果UA编号相同,则会导致重复计数。

第一次

window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-6100216-1');

第二次

window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-xxxxxx-1');

好消息是代码中没有任何内容表明您正在跟踪任何特殊或自定义的内容。我建议您查看gtag.js的实现方法,并删除其余的分析代码。请注意,这是假设该网站的其余页面上也没有任何内容。随时发布有关该网站的更多信息,我可以看看。

答案 1 :(得分:0)

正如XTOTHEL所解释的,您有多个代码和重复计数,而他的方法指南非常出色。

我只能在此主题上添加的内容是,您应该考虑转移到Google跟踪代码管理器并将GA放入其中,而不是自己嵌入任何Google Analytics(分析)代码。