Google scripts are one of the things that slow down your website. Yes, I was also surprised to see Google PageSpeed Insights and GTmetrix complain about the Google Analytics script.
Why Google Analytics is Slow?
The very fast CDN is used to load the Google analytics script. So why does Google Analytics move so slowly? Here are the explanations:
Google Tag Manager + Analytics = 73KB
Yes, the new Google Analytics script is built into gtag.js, which is about 73KB. It’s 20KB, even if you only use the Google Analytics script.
For speed, every Kilo Byte matters! The reason why 73KB and 20KB are what they are is that they are too big. They have too many features that we don’t use 99% of the time.
Third-Party HTTP Requests & DNS Lookups
This is how the latest Google Analytics script looks:
First, a.js file is downloaded from www.googletagmanager.com. Another script will be downloaded from www.google-analytics.com by the downloaded one.
As you can see, we’ve already sent two HTTP requests from two different domains. The browser must make two more DNS lookups and TCP connections. That doesn’t sound good at all.
If you have used Google PageSpeed Insights to look at your Google Analytics site, you may have seen the warning “Serve static assets with an effective cache policy”.
“Leverage browser caching” means the same thing in GTmetrix.
The cache time (TTL) for the Google Analytics script file (analytics.js) is only 2 hours. So you’re not using the browser’s caching feature.
How to Optimize Google Analytics?
We have to ensure that we don’t send any additional DNS/HTTP requests and that the browser doesn’t show a warning for caching.
Solution? Host Google Analytics on your own!
Why Google Analytics should be Self Hosted?
Google Support says the js file shouldn’t be hosted on your site. We’ll miss the updates, which is the only reason. But there are ways to deal with it (as mentioned below). But here are some reasons why you should host Google Analytics yourself:
Advantages of Self Hosting Google Analytics
- No extra DNS lookups.
- Don’t let people block privacy extensions.
How to Self-host Google Analytics
Using Flying Analytics
- Analytics.js and Conversion.js are combined to make Gtag.js (66KB). Most of the time, Conversion.js is used for AdWords.
- Analytics.js (44KB) is Google Analytics’ standard script with all its features.
- Minimal Analytics (1.4KB) is a “minimal” version of Analytics.js that only has the most basic features. Some advanced features and “Site Speed” are not available. But basic features like real-time users, page views, users, location, device, traffic sources, and so on will work.
Minimal Analytics should be enough for 90% of users.
Google and Facebook often slow down websites. No matter how fast their CDN is, a request from a third party to another domain will slow down your site if you don’t have a good cache policy.
After doing these things to your WordPress site, you should see the changes.
Leave a comment if you have any questions or thoughts. I read all of them and answered each one within 8 hours!