Faster Google Analytics with asynchronous tracking code

new asynchronous tracking codeGoogle Analytics is a great web analytics program, but sometimes it can slow down your site. The good news is, that Google has now released a new tracking code that loads asynchronously which means  optimized loading of the ga.js tracking file, and the possibility to insert the tracking code higher in the HTML without interfering with the user experience

The new asynchronous tracking code can be found here and has the following structure:

<script type="text/javascript">
  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);
  })();
</script>

To get the new code working, you should do the following:

  1. Remove the old tracking code from your page
  2. Insert the new tag in the <head> section of your HTML, just before the </head> tag
  3. Replace “UA-XXXXX-X” with your web property id

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

Trackbacks for this post

  1. Faster Google Analytics | Tips4PHP

Comments are now closed for this article.