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.
Here’s how to speed up that annoying Google Analytics javascript on WordPress:
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.
Other methods of loading GA Analytics
- GA Lite using JSdelivr – smaller, lightweight, locally-loaded or CDN-loaded cacheable GA script. More explanation about it here as well.
Regev
Hey Johnny,
Google Analytics has been giving me nightmares the past few weeks. Completely messed up – errors and everything inside their site. I thought of just getting rid of it, since I barely use it (just to see the *overall* traffic every once in a while) – I use Google Search Console to see how much search traffic, new external links, which keywords and what SERP placements, etc and improve accordingly. Do you still use GA? what are your thoughts?
Johnny
I use GA without any issues and do love the convenience. But that’s because I have real traffic on my sites and a ton of it from different places. Do I really really need it? Probably not, could run my business well even without. I have a good pulse on things and can guess my traffic but I think people should use it at least until they’ve created 1 successful site.
Reg
What do you do with GA data though except for seeing how much traffic you get? Improving UX within the site to reduce bounce rates, etc?
Johnny
Hahaha, thanks to your good question…I made a whole post just for you. 🙂
https://wpjohnny.com/metrics-track-google-analytics/
Regev
I LOVE YOU JOHNNY!!!
Johnny
Hahaha, I love you too man!
John @ S4
We use CAOS a lot. It’s great and never fails, but seems to be becoming less lean and simple with every new release adding a little something extra.
Johnny
What’s not lean about it? CAOS mainly locally loads your GA, which makes most of the difference. Whether extra 1kb or so for the GA script, I think is not a big deal.
John @ S4
Don’t get me wrong I still love CAOS and use it everywhere.
I just want very little, maybe less than most people, from GA and therefore from a GA plugin, I chose CAOS originally because it did so little, but now it has stealth mode lite, google optimize integration, enhanced link attribution, and big adverts asking me to upgrade to super stealth mode.
I’m sure it’s all good stuff, I just personally preferred it as simple as simple could be.
Johnny
Hmmm….then try Flying Analytics by Gijo, maybe that’s more of your type.
John @ S4
Thanks, never tried it, but I’ll take a look.
Martin
The plugin affected my analytics in the past e.g. broken bounce rate. I had to switch to manual implementation.
Martin
METHOD #3 – Locally-loaded GA script (2nd fastest)==> Try this guide and you will change your mind https://themefreak.net/blog/how-to-cache-google-analytics-code/ (I jumped set up htaccess since LS Cache does it)
Johnny
Thanks for sharing. I prefer to rely on a plugin to do it but yes. That method works!
Martin
The same plugin broke my GA script e.g broken bounce rate, hence I switched to this method.
Ryan
Any thoughts on Site Kit from Google?