google-analyticsanalyticsanalytics.js

Multiple Google Analytics code on website


I'm working to update a website that appears to have multiple instances and version of Google Analytics running. I need help identifying what is actually happening and what version I should keep.

First these are loaded in the head:

 <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>

Then, these following code is run in the head as well.

  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');

Then, in the body, gtag is loaded and called.

  <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');

I've already corrected a thousand mistakes past developers have made.


Solution

  • Holy moly, that's some work! On a high level, essentially you have all THREE(3) methods of GA implementation: ga.js, analytics.js, gtag.js. The current recommended method is gtag.js.

    ga.js (legacy)

    This is how everything associated with ga.js should be grouped (as per documentation):

    1. This is the line where the ga.js library is loaded: <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"></script>

    2. This is the piece of code that initiates 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

    As per documentation, the normal async implementation look like so:

    <!-- 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 -->
    

    but for whatever reason, it looks like you're only loading the library, but didn't initalize it with the rest. So for analytics.js you're only loading the library and not doing anything else with it.

    gtag.js

    Again, as per documentation, the gtag implementation code looks like:

    <!-- 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>
    

    In this case, the page is utilizing it twice, if the UA numbers are the same, it would result in double counting.

    first time

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

    second time

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

    The good news is that nothing in the code indicates that you are tracking anything special or customized. I would suggest you look at the gtag.js implementation method and remove the rest of the analytics code. Note, this is assuming there is nothing on the rest of the pages of the site as well. Feel free to post more information about the site and I can take a look.