Begun in June 2021, in the course of June and July Google's core update referred to as "Page Experience" is underway, aimed at making Core Web Vitals ranking factors.
Increasing loading time and speed of your site is essential: in a previous article, dedicated to how to speed up WordPressWe have listed some strategies for improving loading times.
During page loading, aspects that can slow down the loading of the page include code, CSS and Javascript, including the Google Analytics (preferably installed via Google Tag Manager).
Just the Google Analytics script can slow down the loading of your site, increasing the number of external resources your website has to load.
The Google script also throws a warning about caching, due to the fact that the HTTP cache header expiration is set very short.
One solution is to host your gtag.js script locally on your server: in this case the HTTP cache headers of your CDN or server are automatically applied, with full control over the caching of the script.
Hosting Google Analytics locally and serving the script from your CDN or server also allows you to leverage a single HTTP/2 connection.
By default, Google Analytics does in fact have a cache time of two hours. By hosting the script locally, you can have full control over caching the file, further reducing the loading time.
Hosting gtag.js locally with MonsterInsights
MonsterInsights, the popular plugin used by over 2 million users to add Google Analytics to WordPress allows you to host the Google Analytics script (gtag.js and analytics.js) locally.
Monsterinsights includes an update to the way our main monitoring script is loaded on your website, the Performance Addon which loads the script so that MonsterInsights is fast to keep the loading time as short as possible.
The Performance Addon feature automatically fetches the latest gtag.js file from Google every 24 hours to make sure it's up to date.
The Performance Addon update makes the MonsterInisghts script load as fast as possible in order to help achieve optimal Core Web Vitals scores.