Press enter to see results or esc to cancel.

How to Leverage Browser Caching for Google Analytics in WordPress

When you run speed tests on a website using Google Page Speed insights it is very likely that you will get a “leverage browser caching” warning. Leveraging browser caching is valuable because it tells your viewer’s web browser to store static data (images, CSS etc.) for a defined period and uses these locally stored resources each time the page is visited so that it does not have to download all those assets again, making the website load faster.

Leverage Browser Caching Error

In Page Speed Insights, the warning message might come from Google’s analytics.js script which might seem strange coming from Google itself but this happens because Google sets a low cache time on the script. The cache time is set low so that webmasters can receive timely push updates as they are rolled out.

How To Fix The Error

The warning can be frustrating to fix if you are just encountering it for the first time but we will show you two methods that you can use to fix this warning. After these fixes, you may also observe a slight performance improvement.

Method #1 – Use a WordPress Plugin – Complete Analytics Optimization Suite (CAOS)

Analytics Optimization Plugin

CAOS is a free plugin available to download from the WordPress Plugins directory that can help you fix the “leverage browser caching” warning. The plugin inserts your Analytics tracking code into the header or footer of your website, stores the analytics.js file on your local server and keeps it updated using wp_cron().

With Complete Analytics Optimization Suite, you can also set your site’s adjusted bounce rate. This is the timeout limit that allows you to evaluate the quality of your traffic.

CAOS has another great feature: it allows you to anonymize the IP addresses of your visitors. Anonymizing IP addresses protects the identity of your visitors and is in line with the data protection laws of many countries where the anonymity of user IP addresses is a basic requirement for legal compliance. This is an optional feature and can be left disabled by choice.

By installing this plugin, you can easily get a higher score on PageSpeed Insights or the new think with Google service and your website will load faster because browsers won’t have to download the file from Google’s server over and over again.

Method #2 – Use a Google Analytics Client

You can also use a Google Analytics JS client called ga-lite. This is a small cache-able subset that uses Google’s Measurement Protocol REST API and was created by Jesse Luoto. The API is supported by Google which means that you don’t have to worry about Google updates or compliance.

Just add the below code to the end of your <body> tag and that’s all you need to fix the warning.

<script src="script URL" async></script>
 var galite = galite || {};
 galite.UA = 'UA-123456'; // Add your analytics code

Find the full code here.

The above code simply adds the library to your website and starts sending page views to Google Analytics’ public REST API for tracking.