Self host Google Analytics in WordPress

I’ve said before that you should always aim to self host static assets to improve the performance and stability of your website.  I’ve also written about how to self host Google Fonts in WordPress, as this is one of the common use cases.  Well today I’d like to talk about another common use case, and that’s Google Analytics.

Google Analytics is one of those external files that always flags up on test sites such as GTMetrix and even Google’s own PageSpeed Insights, because it has a very short expiry time.  However, the file itself is not customised, it’s a generic library file and your unique tracking number is then included as a parameter in a local inline script.  This makes it perfect for self hosting.

If you’re doing this manually, you could just download the script file and then upload it to your website via FTP and modify the link to point to your local version.  That’s it, pretty straight forward.  However, if you’re WordPress, there’s an even easier way, and it’s a plugin called Complete Analytics Optimization Suite.

This plugin allows you to enter your tracking number and a few different settings, all of which get output into a local inline script.  It automatically downloads and hosts the Google file locally, and also uses a WordPress cron job to keep it regularly up to date, in case Google was to change it.

And that’s it really, simple as that; install, configure and forget.  My favourite kind of plugin really, just does what it should and nothing more.