Asynchronous Google Analytics
A few days ago, Google released what I consider to be their most important new product feature of the year. Google Analytics now supports asynchronous loading of its tracking code. This means that you can have all the awesomeness of Google Analytics with very minimal impact to your page load times. If you’re still not using Google Analytics on your site, go get it right now. I’ll wait.
All right. Now that you’ve got Google Analytics working on your site, replace the Javascript code tracking snippet with the following:
1 <script type="text/javascript"> 2 3 var _gaq = _gaq || []; 4 _gaq.push(['_setAccount', 'UA-XXXXX-X']); 5 _gaq.push(['_trackPageview']); 6 7 (function() { 8 var ga = document.createElement('script'); 9 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 10 ga.setAttribute('async', 'true'); 11 document.documentElement.firstChild.appendChild(ga); 12 })(); 13 14 </script>
Make sure to replace the ‘UA-XXXXX-X’ string on line 4 with your site’s Google Analytics ID.
If you’d like, you can even place this Javascript at the top of your pages, since it will no longer slow down your page load times. Google had previously suggested placing it at the end of your pages, but this is no longer necessary.
That’s it! You have a shiny new asynchronous visitor tracker, and your site will be super snappy.
More details about Google Analytics asynchronous tracking can be found here.
