Return to Snippet

Revision: 37684
at May 25, 2011 02:21 by jeremytarpley


Updated Code
<html>

<head>
  <script type="text/javascript">//<![CDATA[
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount','UA-xxxxxxx-x']);
	_gaq.push(['_trackPageview'],['_trackPageLoadTime']);
	(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>
</head>

<body>

  <p>Page Content</p>

  <script type="text/javascript"> (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>
</body>
</html>

Revision: 37683
at December 15, 2010 06:46 by jeremytarpley


Initial Code
<script type="text/javascript">
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	try{
	    var pageTracker = _gat._getTracker("UA-254185-2");
	    pageTracker._trackPageview();
	} catch(err) {}
    </script>

Initial URL


Initial Description
Asynchronous Google analytics tracking

based entirely on examples from code.google.com and yoast.com
http://yoast.com/wordpress/google-analytics/
http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html

notes from code.google.com
If you prefer to put the Analytics snippet at the bottom of the page, you should know that you don't have to put the whole snippet at the bottom. You can still keep most of the benefits of asynchronous loading by splitting the snippet in half—keep the first half at the top of the page and move the rest to the bottom. Because the first part of the tracking snippet has little to no effect on page rendering, you can leave that part at the top and put the part of the snippet that inserts ga.js at the bottom. 

Both pieces of code need to be wrapped in their own script tags, but only the last six lines of the original asynchronous snippet need to be moved to the bottom. All the lines that push methods onto _gaq can stay at the top.

Initial Title
Smart way to add the tracking code for Google analytics

Initial Tags


Initial Language
JavaScript