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
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
- Using a scripting language, such as PHP, ASP or JSP, to dynamically cache the files upon requests, as mentioned by Joyce Babu in his post "Speed up Google Analytics using simple PHP Script"
- Creating a system automatic schedules job that regularly updates the files on the disk, as mentioned by Jeff Starr in his post "Optimizing Google Analytics Performance"
Being notified when the your scripts are outdated
- Creating, perhaps, a JUnit test that compares your local scripts with the on-line versions. If the test fails you know they need to be updated. These JUnit tests could be run by an continuous integration system.
- Follow Google's change log and even their RSS feed for changes.
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.