Make Drupal load faster with Google analytics asynchronous tracking code

New Google analytics code loads asynchronously allowing your page to load faster. It is easy to migrate from old code snippet to asynchronous tracking code.

Asynchronous tracking is an improved way to track website visitors with Google Analytics. 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.

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

You can move to new code by remove old code to new by the following template (from Google)

<script type="text/javascript">

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

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

  • Replace UA-XXXXX-X with your web property ID
  • Insert the asynchronous snippet at the bottom of the <head> section of your pages, after any other scripts your page or template might use.

I don't know why "Average Time on Site" at my website reduces after using new tracking code!