Local Google Analytics in Wordpress

Page content

Host Google Analytics Locally in Wordpress

Google analytics is crucial to measuring your website performance. Bloggers and website owners mostly depend on Google analytics code to measure website visits and other user data. However, the code can slow down your website a bit. Hosting Google analytics locally can help you speed up your website. BY hosting Google analytics locally, you can avoid extra DNS lookups as well as fix the leverage browser caching issue caused by the GA4 script.

There are quite a few plugins to apply Google analytics code to your website/blog. However, most of them do not have the feature to host Google analytics locally. There are still some plugins including free and premium that can help you host Google analytics locally and speed up your website. The most popular plugin used for applying GA4 code to websites is the MonsterInsights plugin. However, with this plugin too you cannot host Google analytics locally. This is where plugins like Perfmatters and CAOS come to our rescue.


Perfmatters is a premium wordpress performance management plugin that allows you to disable several useless scripts in Wordpress and speed up your Wordpress website. However, it also includes several additional features like CDN, local google analytics, and prefetch scripts.

With perfmatters you can host the GA4 script locally and load it inside the header or footer according to your preference.

To enable local analytics using perfmatters plugin, you will need to install the plugin and then go to settings. Go to perfmatters settings and then click on the analytics menu. Now, you need to enter only the tracking id inside the box where it says Tracking id.

After that, you can select the position where you want to apply the script. Google suggests loading the code inside the header. However, for a fast website, loading it inside the footer is also fine. Toggle the switch to enable local analytics on.

perfmatters analytics settings

After having saved the changes, do not forget to clear the website cache. You will see that there are two options for loading GA4 script locally. One is the normal script, while the other is the minified script. If you want you can use the minified version, which is much smaller in size to the original version for extra speed.

If you do not need a lot of the extra data and only the basic data, you must enable the minified version in Perfmatters for a faster experience. Both the scripts are loaded by the plugin via the async method for faster performance.


The CAOS plugin is a free plugin for hosting GA4 script locally. If you do not want a premium plugin, CAOS is your best option for hosting google analytics locally. It has more than 20,000 active installations and has received a 4.9 out of five ratings.

Using the plugin is absolutely easy and you can install it just as you install any other plugin. Go to the wordpress plugin repository and search for CAOS. Install and activate the plugin. Now, go to settings in your wordpress dashboard and from there click on optimize google analytics.

CAOS settings

You will find yourself in the basic settings section of the CAOS dashboard. Here you are required to enter the tracking ID (only the tracking id) inside the box. You can select to load the code into the header or the footer. Save the settings and your basic setup is complete.

With the pro version of this plugin you can ensure higher compliance with the GDPR regulations. Go to the advanced settings if you want to load Google analytics using your cdn url. In this section, you can enter the cdn url for your website to serve Google analytics code using cdn. If you are also using another plugin to insert GA4 code like MonsterInsights, you can check the compatibility mode option.

Flying Analytics:

A third popular plugin for hosting google analytics script locally and for faster website performance is the flying analytics plugin. Created by WP Speed Matters, which have also created Flying Press and several more speed optimization plugins for wordpress, Flying analytics is also rated 4.9 out of 5.

Installing and using this plugin is also very easy and apart from inserting your tracking id, you will not need to do anything else to host GA4 script locally in wordpress.

Install the plugin like you install any plugin. Now, go to the plugin settings and insert the tracking id inside the box. Save the changes and you are done.

flying analytics

You have four options to choose from for the script which include gtag.js V4, Gtag.js, analytics.js and minimal analytics.js. Gtag.js V4 is the largest in size at 91kb and minimal analytics.js is the smallest in size at just 1.5 kb. Depending on the level of performance you require, you can decide which one to use.

WP Rocket:

WP Rocket is another premium Wordpress caching, speed and performance plugin that you can use to host Google analytics locally in Wordpress.

Since WP Rocket is a premium plugin, you will need to purchase a premium plan before you can install it on your website. However, if you have it already, you just need to use the google analytics add on by WP Rocket. Activate the add on and insert your GA 4 tracking id. That’s all.