Hazards of hosting the Google Analytics scripts yourself and how to avoid them

If you are running a popular website with a lot of visitors and use Google Analytics to track them, you might already have heard that you could host the Google Analytics scripts yourself to increase your site's response time. Even I have written a post concerning the impact on loading external scripts and how it impacts your own response time, thus also the user experience of the site.

Concerning Google Analytics, I am not going to argue if that's a good or a bad idea to host the scripts yourself. As a web developer, you have to take that decision yourself; do you want to sacrifice the response time to have a more reliable tracking or is it more important with a faster response time by removing any external dependencies.

The hazards of hosting the scripts yourself

If you decide to host the files yourself, you introduce the risk that the scripts can become old and deprecated (due to Google upgrades the original scripts) and you won't track anything at all. However, there are some solutions to avoid this.

Two kind of solutions

The solutions can be divided into two groups. The idea behind the first group solutions is to automatically update your hosted scripts regularly. The second group of solutions is to be notified when the scripts are out of date and manually update them.

Both solutions have their benefits and disadvantages depending on how much time and effort you want to put down before and after the launch of the site, and how secure you want it.

The benefit of automatically updating the scripts is:

  • You do not need to put down time and effort after the site is launched, since the scripts will be automatically be updated a time after Google makes an update.

The disadvantage are:

  • Since you want to be nice towards Google, you don't check for updates every five minutes, but perhaps once a week. However, that means if Google updates their scripts, it will temporary make your scripts outdated and possible break the tracking until you automatically update the local scripts.

The benefits of being notified are:

  • Usually less time spent in implementation, compared to creating a whole solution that updates the local scripts
  • More secure, since you can verify that you are updating the scripts to the next version. With an automatic update, you introduce the risk for a sneaky man in the middle attack which in fact could make your site start hosting malicious JavaScript without you even knowing of it.

The disadvantages are:

  • You need to put down time in maintaining the scripts manually, after the site is launched. If you are responsible of several sites, this could easily consume large amounts of time.

Different ways to automatically update the scripts

Being notified when the your scripts are outdated

Using Google's asynchronous solution

It is also possible to forget this whole post and implement Google's latest tracking code, which is instead asynchronous based. According to Google it's faster, more accurate and more resistant against errors.