An unofficial blog that watches Google's attempts to move your operating system online since 2005. Not affiliated with Google.

Send your tips to gostips@gmail.com.

December 1, 2009

Optimized Google Analytics Code

A lot of web sites use Google Analytics to analyze traffic data, so the performance of Google's tracking code is very important. Google recommends to place the code just before closing the <body> tag to ensure "that the tracking code is executed as the last element of the DOM". Otherwise, the script would block the other elements from loading and visitors would have until the script is executed to see the rest of the page. Unfortunately, you can't place the script at the bottom of the page if you some advanced features like tracking events.

To improve the script's performance, Google decided to load it asynchronously. "Unlike a traditional installation, asynchronous tracking optimizes how browsers load ga.js so its impact on user experience is minimized. It also allows you to put your Analytics snippet higher in the page without delaying subsequent content from rendering."

Here's the new code for basic tracking, which uses some ideas from Steve Souders:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();

</script>
Google Analytics Blog says that the new snippet is a beta feature and changing the code is optional.
The new tracking snippet offers the following benefits:

* Faster tracking code load times for your web pages due to improved browser execution
* Enhanced data collection and accuracy
* Elimination of tracking errors from dependencies when the JavaScript hasn't fully loaded

4 comments:

  1. [quote]Unfortunately, you can't place the script at the bottom of the page if you some advanced features[quote]

    Too late to try 'I accidentally'

    :)

    ReplyDelete
  2. Thanx a lot for providing me great knowledge about the new features of google like-tracking events and more.I also want to share some information to you related to coupons and discounts-I have visited a site that is proving great discounts on network devices and electronic items by On Sale promo code.You can also visit that site for more knowledge.

    ReplyDelete
  3. "...if you some advanced features like tracking events.", "...visitors would have until the script is executed...", ...
    even if I'm german I notice how great the english in this article is...

    ReplyDelete
  4. I tested that "beta" javascript snippet on Currate.com blog site - it does not work as it seems to lock up. I changed it back to original Google Analytics snippet.
    Meanwhile, keep up with your excellent articles. :)
    Robert
    Currate.com Developer/Publisher

    ReplyDelete

Note: Only a member of this blog may post a comment.