Load Google Analytics faster on WordPress!
If you’ve ever been addicted to speed-testing your site on Pingdom/GTmetrix, then you know exactly what I’m talking about. That GA JS request lags your site costing you an extra 100ms-2seconds of load time. Below are my favorite methods to prevent the Google Analytics JS from slowing down your site.
Why Speed Up GA Script Load?
How important is it to speed up the GA script? Most of us are only thinking about it because we saw it on a page speed test and want faster times and higher scores. The reality is…that file is probably on every browser and heavily cached. Your device might load it but it probably doesn’t call it every time. Nonetheless, you’re welcome to discover your own tactics of speeding it up for function or aesthetic purposes. Enjoy!
METHOD #1 – Using a cache plugin (fastest)
- This is definitely the fastest way. Simply load it with your cache plugin which may or may not combine it with all your other JS scripts, depending on what settings are available.
- Not all cache plugins handle GA caching. I use Swift Performance Lite. (Here’s the premium version with more features). Just make sure you don’t forget to re-enable GA if you decide to disable caching.
METHOD #2 – Using a GA plugin (2nd fastest)
- Complete Analytics Optimization Suite (CAOS) – nice plugin that saves the latest GA.js file on your server and loads it locally. The file is synchronized with the latest GA from Google servers and updated periodically. You save one request and speed up page load times. Works well, very easy-to-use and lean.
METHOD #3 – Locally-loaded GA script (2nd fastest)
- I don’t recommend this since it’s a pain in the ass to setup and requires a lot of technical knowledge to call the script manually from your theme and also to figure out a way to keep the file updated using cron. Should there be any mistakes, you might miss out on some of the proper GA tracking.
- You can search Google for various methods of accomplishing this. Again, I don’t recommend it.
METHOD #4 – Using your theme (regular speed, safest)
- This is the safest and lean way to load Google Analytics, since it isn’t reliant on caching or other plugins. Most WordPress themes have a place where you can enter your scripts and even choose header or footer position. The speed is the same as the default way since you’re loading the full script which makes all the usual requests (2?). I prefer to load GA in the footer instead of the the header.
- For those without a place to load scripts in your theme, simply use one of the Google Analytics plugins mentioned above.
- Another option if you don’t want CAOS plugin is to the GA Google Analytics plugin. It’s also lean.
METHOD #5 – Bloated GA plugin (slow)
- This method is only listed for comparison purposes. I hate the big bloated plugins that show you fancy charts from your WordPress dashboard. I also don’t recommend to select all the unnecessary options like Display Advertising, etc and etc. 99% of you don’t need them.