Home › blog
Google’s snippet for Analytics is optimized for maximum compatibility, not for performance. Here’s how to make it faster.
Which dynamically determines whether the HTTP protocol for the requested page is either secure or standard. It then uses the appropriate protocol to reference the tracking code. So, if one page on your site is delivered over a standard HTTP protocol, the result is:
If another page is delivered over a secure connection, the result will be:
The problem with that you still have to do do the HTTP/HTTPS detection dynamically to determine the correct file to load, which is a bit too much for my taste (even if it has zero or no impact on performance).
Lets make it better
First I tried
http://ssl.google-analytics.com/ga.js which seems to redirect to
https://ssl.google-analytics.com/ga.js so that’s no good (forcing HTTPS)!
www.google-analytics.com/ga.js however seems to work on both (while inheriting the page’s protocol) taking that a step further, strip
www and it also works as expected (less typing = better).
(for sanity and paranoia’s sake, I downloaded both sources and compared them to make sure they are identical)
So from now on, I’ll probably stick to using the following:
Which is much shorter and more efficient.