Reputation: 1
What is the differnce between the below two code in tracking the webpage.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', 'right.com']);
_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);
})();
</script>
And
<script language="javascript" src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>
<script type="text/javascript">var pageTracker = _gat._getTracker("UA-XXXXXXXX-X"); pageTracker._initData(); pageTracker._trackPageview();</script>
Upvotes: 0
Views: 389
Reputation: 32780
1) The first was was deprecated a few month ago, the second one has been deprecated many years ago (you should use neither of them)
2) The second version is very old, synchronously executed code. The downside was that loading the GA code synchronously meant that page load might under some circumstances be blocked until the ga.js file finished loading (which in turn meant that users sometimes saw an empty screen until the file had finished loading). Older browsers where not got with loading multiple files simultaneously, especially if the files content might affect the way the page was rendered.
3) The first example (asychronous code) stores all instructions for the analytics code in an javascript array. Since this uses native javascript code (the push method) this will work before the ga file has been loaded. Once the code is in place (it is injected into the pageheader via the short bootstrap script below the _gaq.push calls) it can read the array and process the instructions contained therein.
Again I'd like to point out that this information is of purely historical value since both versions of the tracking code are deprecated. You should use the new Universal Analytics code only.
Upvotes: 2