How to Load Google Analytics 10x Faster in WordPress

macbook pro on brown table 139387 1

One of the reasons for making your website slow is Google scripts itself! Yes, I was also shocked to see Google PageSpeed Insights and GTmetrix complaining about Google Analytics script.

Why Google Analytics is Slow?

The analytics script from Google is loaded from the super-fast CDN. Then why Google Analytics is slow? Here are the reasons:

Google Tag Manager + Analytics = 73KB

Yes, the new Google Analytics script is embedded inside gtag.js and totalling it’s about 73KB. Even if you use Google Analytics script alone it’s 20KB.

For speed, every Kilo Byte matters! The reason for 73KB and 20KB is because they’re bloated. They’ve too many features, 99% of the time we don’t need it.

Third-Party HTTP Requests & DNS Lookups

The latest Google Analytics script looks like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-119755794-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXX-XX');
</script>

The script first downloads a js file from www.googletagmanager.com. The downloaded script will again download another one from www.google-analytics.com.

As you can see we already made 2 HTTP requests, and to 2 separate domains. The browser needs to make two extra TCP connections and DNS lookups! Yes, that doesn’t sound good!

Browser Cache

If you’ve analyzed your site containing Google Analytics with Google PageSpeed Insights, you may have seen the warning “Serve static assets with an efficient cache policy”.

pagespeed insights warning

Similarly “Leverage browser caching” in GTmetrix.

browser caching google analytics

The Google Analytics script file (analytics.js) have a cache period (TTL) of only 2 hours. So you’re not taking advantage of browser caching.

How to Optimize Google Analytics?

We’ve to make sure we’re not making any additional DNS/HTTP requests and doesn’t show a warning for browser caching.

Solution? Self-host Google Analytics!

Why Google Analytics should be Self Hosted?

From Google Support it’s recommended that the js file shouldn’t be self-hosted. The only reason is we’ll miss the updates! However, there are solutions for it (as mentioned below). But here is why you should self-host Google Analytics:

Advantages of Self Hosting Google Analytics

  • No extra DNS lookups
  • Can be bundled with other JavaScript files
  • Prevent blocking from privacy extensions
  • GDPR complaint

How to Self-host Google Analytics

Using Flying Analytics

Flying Analytics WordPress plugins (built by myself) allows you to self-host the Google Analytics JavaScript file.

flying analytics
Flying Analytics

Flying Analytics comes with different JavaScript files for sending report to Google. They’re:

  • Gtag.js (66KB) is a combination of Analytics.js and Conversion.js. Conversion.js is usually used for AdWords.
  • Analytics.js (44KB) is the standard script for Google Analytics, which includes all the features.
  • Minimal Analytics (1.4KB) is a “minimal” version of Analytics.js with only basic features. ‘Site Speed’ and some advanced features are not available. But basic features like realtime users, page views, users, location, device, traffic sources etc will work.

For 90% of users, Minimal Analytics should be enough.

Using WP Rocket

If you’re using WP Rocket, go to add-ons and turn on Google Tracking. But you’ll not get other options like Minimal Analytics.

google analytics wp rocket

Conclusion

It’s common that Google and Facebook slow down the site. However fast their CDN is, if it’s a 3rd party request to another domain, without proper cache policy, it will slow down your site.

You should definitely see the improvements after doing these methods in your WordPress site.

Comment below if you’ve any queries or feedback. I read and reply to each of them within 12 hours!

You May Also Like