Why is tracking code generated for my site different than what is in the documentation?

一笑奈何 提交于 2019-12-25 01:49:03

问题


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



来源:https://stackoverflow.com/questions/28617001/why-is-tracking-code-generated-for-my-site-different-than-what-is-in-the-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!