Speed Up Your Google Analytics Async Code

August 18th, 2010 → 7:00 am @ Rob Kingston // 5 Comments

Updated: I originally thought it was possible to increase the speed of the GA Async code, but it turns out I misread another post.

Believe it or not, you can optimise your Google Analytics asynchronous code. Whilst reading some links on Del.icio.us, I stumbled across Mathias Bynens’ post on optimising the GA asynchronous snippet.

Feel free to check it out.

For those only wanting real results

Most of the optimisations are not worth it for your average Joe, but one of them really caught my eye and should for yours too (that is of course if your site runs both SSL and non SSL)…

There is a line of code that checks whether the document is being transferred over SSL or a non-encrypted connection. Removing this check speeds up the Google Analytics snippet by around about 235 times on my laptop (fairly decently spec’d I might add).

Test your computer here.

This could be the difference between catching those bouncing visits and not catching them at all.

What does it involve?

All you need to do is replace this string:

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

With the following string:

ga.src = 'http://www.google-analytics.com/ga.js';

So that it resembles this snippet:

<script type="text/javascript">
  </script>

There you have it, you should have the 210th fastest GA code in the west.


Did You like this post? Yes / No

You Might Like These Posts Too


If you found this post useful, please subscribe to my blog.

5 Comments → “Speed Up Your Google Analytics Async Code”


  1. MSRMNT | LukeStevens

    1 month ago

    RT @RobKingston: Just posted: Speed Up Your Google Analytics Async Code by 250ms http://goo.gl/fb/Qm1jz


  2. Kevin Naidu

    1 month ago

    RT @robkingston: Speed Up Your Google Analytics Async Code by 250ms http://bit.ly/cWyqlm


  3. Brian Kuhn

    1 month ago

    No offense, but this optimization is a farse. Yes, if you drop support for https and use a hard coded string, your code will run slightly faster. But that line is only executed once in the asynchronous snippet and in either case takes far less than a millisecond to execute. You are not saving 250ms with this optimization.

    Pay close attention to what that test does. View the source. It runs both lines of code 100000 times each. You’re saving 250ms off of 100000 executions. Divide 250ms by 100000 and ask yourself if you’re wasting your time ;)


  4. Rob Kingston

    1 month ago

    Thanks for the heads up Brian - I’ve checked it and you’re right. I knew at some level it was a bit too far fetched. Checking the test’s code I now see what it does…


  5. Wez

    4 weeks ago

    It’s a good point to check the speed of all JavaScript snippets.. I recently optimized some tracking code on the inter spire shopping cart which made a big differance on load times, which should have a good result for conversions. Used firebug for firefox.


Add Your Comment