Why is tracking code generated for my site differe

2019-08-23 05:42发布

问题:

Looking at the GA developers page at https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide it says the tracking code is:

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

But when I start an account for my site GA gives me the tracking code:

(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-XXXXX-1', 'auto');
  ga('send', 'pageview');

Why are these completely different?

Thanks

回答1:

The first code snippet is for the old/traditional Google Analytics tracking script.

The second one uses the newer universal Google Analytics tracking script.

The reason why they are different is probably because Google is trying to push the newer tracking script, but haven't updated all of their documentation.

The analytics.js JavaScript library is built on Universal Analytics technology. If you’re using a tracking code snippet on your website that references analytics.js, you’re using Universal Analytics.

If you’re using any other library, like ga.js, you’re using a Classic Analytics tracking technology, and should upgrade your tracking code (if you also have a Universal Analytics property).

Reference