Hosting ga.js locally (google analytics tracking code)

Hosting ga.js locally has two main benefits - fewer DNS lookups for the client’s browser (you could even combine it with your site’s java script to reduce HTTP reqs as well), and you might be able to get it to your users faster than Google can. Whilst the second point isn’t likely true for many, it is true for those of us serving most of our content in Australia.

Google recommends you don’t do this, but doesn’t say you can’t.

A neat solution for ensuring your ga.js file stays up-to-date with the one that Google wants you to use is the following shell script, written by a colleague of mine at Service Central:

#! /bin/sh
cd /path/to/httpdocs/include/js
svn up -q ga.js
wget -q -O ga.js www.google-analytics.com/ga.js
svn ci -q -m "Auto update of ga.js" ga.js

Simply setup a cron to run this script as often as you wish to check (we run it daily) and the most current version of ga.js will be updated on your server and committed back into your SVN repos.

comments powered by Disqus