File Under: Backend, Web Services

New Asynchronous Script Stops Google Analytics From Slowing Down Your Site

Google Analytics is one of the more popular ways to keep track of your site’s visitors. With the ability to track, compare and compile all sorts of statistics about who’s visiting your site, Google Analytics is hard to beat.

Unfortunately, all that statistical goodness has a price — the JavaScript Google Analytics uses to track your visitors can slow down your page load times. Traditionally, the delayed page load problem was solved by placing the Google Analytics JavaScript code at the bottom of your pages (just before the closing </body> tag).

However, Google is now offering a second possible solution — a new Google Analytics script that loads asynchronously.

The asynchronous script means browsers will load the ga.js script file separately from the rest of your page, minimizing the impact on page rendering.

The net result is that you can move your Analytics code back up to the head tags of your pages and it won’t slow down your pages. Or rather it will, but it won’t be noticeable to users. In our testing, the new script loaded in between 80ms-120ms, whereas the old script loaded in 40ms-60ms.

The new script is definitely slower, but because it loads separately from the rest of your code visitors will see all your content before the new script kicks in, even if it’s at the top of the page.

In our testing, the overall page load time for the new script in the head tags was almost exactly the same as for the old script at the bottom of your page.

So why would you want to switch to the new script? The primary reason is that some advanced features of Google Analytics, like JavaScript event tracking, require the script to be embedded in the page’s head tags. If you’ve been wanting to use any of the customization and advanced features that require Google Analytics’ code to reside in the page header, but haven’t wanted to sacrifice page load times to get it, well, now you have your solution.

The new script and some new embed code can be found on Google Code. Keep in mind that the new script is still a beta, which means you might want to hold off using it on production sites.

See Also: