leverage browser cache for google analytics

Leverage Browser Cache for Google Analytics

You probably here because some website speed test is showing you warning about leverage browser cache for Google Analytics. Right?

Leverage browser caching warning in GTMetrix

It’s kinda annoying for some people when they face such issues especially from company like Google, which actively promotes webdevelopers or site owners to make sure their webiste is fast.

However,

Google Analytics is pretty fast by default but it’s just not cacheable longer than an hour, means it has to be loaded on every page instead of just once when a visitor first visits the website.

The reason for that is

Google wants to be able to update analytics and related scripts anytime quickly that’s why they don’t support long-term Browser cache.

On the other hand,

If you are trying to get a perfect score on website speed test tools such as Google page Insights, GTmetrix, etc. You will always get leverage browser cache warning for google analytics and google fonts.

Unless,

You host google analytics locally’

What Does It Really Mean To Host Analytics Locally

Almost all websites (typical) use Google Analytics these days to track number of visitors and other related data.

When someone visits your website, scripts for analytics have to be download from google servers each and every time which can’t be cached by the browser.

Because you have no control over it and google won’t make it cacheable any time soon.

In simple words, when you download those scripts and serve it from your server and even better if you can afford CDN. You can control the cache and get over latency issue.

The term caching means temporarily storing data–

Web browsers consistently save data from websites like images, files, and pages on visitor hard drive and don’t need to download the same resource twice. As a consequence, websites appear faster on visitors screen.

Benefits For Hosting Analytics On Your Server

The Biggest advantage is speed. Because for some reason Google doesn’t allow analytics to be cached by browsers.

The good thing about self-hosted analytics is

  • Caches extremely well.
  • Scripts can be updated every day via Cron-Job.
  • Support for all browsers and devices.
  • No reliance on third parties.

How To Leverage Browser Cache For Google Analytics By Hosting Locally

The first thing you need to do is install and activate the CAOS plugin.

caos plugin

CAOS for Google Analytics allows you to host analytics.js locally and keep it updated using WordPress’ built-in Cron-schedule automatically!

After you install the plugin, Go to “plugin settings” or settings
» Optimize Analytics

Here, you will see two kind of settings which means basic and Advance settings

Basic Settings

First you need to add “Google Analytics Tracking ID”. Which you can find by going to your google analytics account.

  • From left sidebar click on “Admin”
  • You will see 3 columns, in the middle column first link should say “property settings” click on that.
  • After that, You will see your tracking Id which looks like UA-1987649-1.

You can keep “Allow tracking” option to default unless you show cookie notice and want to be compatible with GDPR laws. In that case just follow the tutorial available in the plugin.

Lastly, for the position of tracking code you should choose footer otherwise instead of header otherwise Analytics script will become render blocking.

Advance Settings

You can pretty much leave advance settings as it is, unless you know what you are doing.

You might have noticed in advance settings you can enable support for Monsterinsights Analytics plugin.

If you do that your analytics will still be hosted locally but and you can use features of Mosterinsght as well.

Other Plugins

There are some other plugins to host google analytics locally include but not limited to:

but I found CAOS best.

Though I personally use Prefmatters(paid).

Not just because of Google Analytics but because of other features to speed up website speed and it just happen to offer to optimize analytics as well.

CAOS is the only standalone plugin allows you to host google analytics locally. Pretty much all other plugins offer 10’s of different option which might be just clutter for you if you don’t need all those features.

But, it’s worth checking them as well.

If you have any questions, then please leave them in the comments. If you have used other methods to convert Levrage Browser cache for Google Analytics in WordPress, then share it with us in the comments below.

Leave a Reply

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