When you start paying attention to the speed of your WordPress site, one of the most important things to look after is all the external requests that your site makes to all kinds of services.
A popular external request that many sites make is to Google Analytics.
And when you test your site in GTmetrix or Pingdom, you will notice the following message:
Leverage browser caching for the following cacheable resources: https://www.google-analytics.com/analytics.js
In this tutorial, I will tell you how to host Google Analytics locally in WordPress to Leverage Browser Caching in order to increase the speed and the score of your site in GTmetrix.
How To Host Google Analytics Locally In WordPress
The solution is very simple and it comes in the form of a plugin.
This plugin is called CAOS for Analytics and it's developed by Daan van den Bergh.
Daan wrote a 5 pages article back in 2016 titled How To Get A 100/100 Score On Pingdom & Google Pagespeed Insights. The premise of this article is obvious from its title.
The reason I mentioned it is to give you an idea about Daan. He's a developer who won't settle for less than 100 point score in Pingdom & Google Pagespeed Insights 👍
So, when Daan made a plugin that allows you to host Google Analytics locally in WordPress and it helps you with the Leverage Browser Caching notice, then you should absolutely try it because this guy knows what he's doing.
Let's take a look at it, shall we!
Go to Plugins > Add New
Search for CAOS for Analytics. Install & Activate it.
On the left sidebar, under settings, click on Optimize Analytics and you will see all these options that might look intimidating at first, but it's not.
Let's take a look at each one of them:
There are 2 types of settings, Basic Settings & Advanced Settings
Under basic settings you have the option to add your Google Analytics Code, choose when you want to allow tracking, configure the plugin to be completely GDPR Compliant with or without a cookie notice.
And last, in this section, you can pick where you want the tracking code to be placed, whether it's in the header, footer or if you want to insert it manually.
The first option in this section of the plugin is to Enable compatibility with Monster Insights. What this option allows you to do is if you have Monster Insights installed and configured, just enable this option and Monster Insights will start using the local analytics.js file that CAOS has created.
Enabling this option will disable all other options in CAOS since Monster Insights is your main Google Analytics plugin and CAOS will just serve as the plugin that creates the local analytics.js file.
Other options available here are set the number of days until cookies expire, adjusted bounce rate, Change enqueue order, Disable all display features functionality, Anonymize IP, Track logged in Administrators and last Remove script from wp-cron.
I love this plugin, it's the easiest way to host Google Analytics locally in WordPress and I recommend it to everyone who's looking to Leverage browser caching and improve their GTmetrix score.
I myself use it with Monster Insights because, in the latter, I use the Events Tracking feature that allow me to track clicks on outbound and affiliate links.