javascriptgoogle-analytics

"Same web property ID is tracked twice." — Google Analytics


It's reported as a warning by the Tag Assistant, but it still bothers me.

And this is my analytics.js implementation:

<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-XXXXXXXX-X', 'auto');

var visited_pages = [];

visited_pages.push(document.location.pathname);

//ga('send', 'pageview');
</script>

<script>
function gaTrack(path) {
    setTimeout(function() {
        visited_pages.push(path);

        ga('set', { page: path });
        ga('send', 'pageview', path);
    }, 1);
}
</script>

It is in the <head></head> tag, as Tag Assistant wants.

I use the gaTrack wherever I do a history.pushState, for instance:

history.pushState(null, null, headnav.data('uri'));
gaTrack(headnav.data('uri'));

And it's all good, nothing is undefined or invalid, but still the warning gets raised in the Tag Assistant.

So my questions are (which may help someone in the future):

  1. Is my approach wrong?
  2. Is my analytics.js implementation and logic inside gaTrack function wrong?
  3. Is there something I could change inside Google Analytics web interface which could slove my issues here?

Solution

  • If you use google tag manager it will also include google analytics.

    thus the compatible way to include google analytics is

      <script>
        window['GoogleAnalyticsObject'] = 'ga';
        window['ga'] = window['ga'] || function() {
          (window['ga'].q = window['ga'].q || []).push(arguments)
        };
        ga('require','ecommerce');
        //etc
      </script>
    </head>
    <body>
      <!-- Google Tag Manager -->
      <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
      height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
      <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
      new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
      '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
      })(window,document,'script','dataLayer','GTM-XXXXXX');</script>
      <!-- End Google Tag Manager -->