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

Solution 1 – Minimal Google Analytics

Minimal Google Analytics is a lightweight Google Analytics alternative that is only 1.5KB with minimal features.

A huge thanks and big shoutout to David for creating this script!

Here is the script:

minmal analytics

Since the script is too small, you can add it as inline in the HTML.

Install Minimal Google Analytics in WordPress

You can grab the above snippet and add it directly to your head section. There is also a WordPress plugin (unofficial) – Minimal Analytics WP plugin.

Pitfalls

The script is designed for sending page views only. It also supports sending events by ma.trackEvent('Category', 'Action', 'Label', 'Value').

If you’re using advanced features or any other plugins that use Google Analytics, it may not work.

For me, this is more than enough because I only track page views and website speed is super important for me!

Solution 2 – Self Host Google Analytics JavaScript

Another solution is to self-host Google Analytics. That is, we download the google-analytics.com/analytics.js file and serve it from your-domain.com/analytics.js.

CAOS WordPress Plugin

CAOS for Google Analytics will host analytics.js locally and schedule a cron job that will check for the latest analytics.js from Google.

WP Rocket WordPress Plugin

WP Rocket is an advanced caching plugin (the one I use). Similar to CAOS, WP Rocket can also self-host and serve Google Analytics files locally.

Goto WP Rocket add ons and turn on Google Tracking!

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like