At this point of time, performance optimization is one of the hot and trending topics in WordPress world. Earlier, we used to have SEO and caching plugins for improving page loading speed of WordPress sites. However, the meaning of optimization has been completely changed in recent days and the focus is to eliminate all unnecessary codes from the base installation instead of optimizing the final output. One of the major problems in this process is to reduce or eliminate the use of external codes like advertisements, fonts, Google Analytics and other third-party JavaScript libraries. In this article, we will explain how to host Google Analytics locally in WordPress and improve the speed.
Standard Google Analytics Setup in WordPress
The standard way of enabling Google Analytics for a website is to insert the tracking code in header section. There are multiple ways in WordPress to achieve this.
- Manually inserting the tracking code in header.php of your theme or child theme.
- Using a plugin for this purpose. You can use dedicated plugins like MonsterInsights or a generic plugin for inserting header and footer codes.
- Most commercial WordPress themes offer an option in theme panel to insert custom JavaScript code like Analytics.
- Popular themes like Astra, GeneratePress and Kadence offer hook element to insert the code in header using WPHead hook.
Speed Problems with Standard Setup
All the above methods of adding code in header section will cause multiple problems in Google PageSpeed Insights tool. Generally, you can find issues under “Reduce the impact of third-party code” with Analytics or Google Tag Manager script.

You can also find the Analytic script under “Reduce JavaScript execution time” section.

Since Analytics script is a static file, you will find a suggestion to “Serve static assets with an efficient cache policy” for improving browser caching.

Note: Google PageSpeed Insights interface changes continuously. The above screenshots are shown to emphasize the fact that inserting Analytics script from Google server will slow down your site.
Hosting Google Analytics Locally in WordPress
As you can see, Analytics and Tag Manager related scripts primarily affect TBT (Total Blocking Time). This will highly impact speed score as TBT has 30% weightage in the performance score calculation.

Unfortunately, you can’t fix the above problems with Analytics script loading from external Google server. The only solution is to find a way to host Google Analytics tracking code locally on your server and serve it as a static file. This will help to fix all issues and improve the speed score.
Perfmatters Plugin
Perfmatters is the popular performance optimization plugin to remove all unnecessary bloating codes from your WordPress site. you can host Google Fonts locally, disable unnecessary WooCommerce scripts on posts, remove feed/RSD links and host Google Analytics tracking code locally. This is a premium only plugin that you can purchase for a small price of $24.95. Most of the time, you can get 10 to 30% discount which will further reduce the price to make this plugin more attractive. You can also check out the coupons available for Perfmatters plugin to get lower price.
After purchasing the Perfmatters plugin, follow the below instructions to host Google Analytics locally in your site.
- First, make sure to activate the plugin with the purchase code so that you will get automatic updates of new features. You can add the license key under “Settings > Perfmatters > License” section.
- After that, navigate to “Settings > Perfmatters > Analytics” section.
- Turn on the button against “Enable Local Analytics” option.
Google Analytics Tracking Code Format
Before filling the details, go to your Google Analytics account and get the tracking code. Below is an example of GA4 version that has the tracking or measurement ID (G-SSHYxxxxxx is the measurement ID).
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SSHYxxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-SSHYxxxxxx');
</script>
Note that the plugin earlier used to offer additional options for Universal Analytics (GA3). This has been removed as Google no more supports GA3 version.
Add Google Analytics Code in Perfmatters
You will have two possibilities to install Analytics locally with Perfmatters:
- Google Analytics 4 (Default) – offers all features and reporting.
- Google Analytics 4 (Minimal) – open-source minimal script from third-party (not from Google) with limited features.
The available options will change based on the script type you choose. Below screen shows the options available when you select the default version.

When you select minimal script, you will see the below options.

Let us explain the details required for selecting or filling all the fields:
- Tracking ID – enter your measurement ID or tracking code.
- Tracking Code Position – header is the default option; you can also choose to move the script to footer section.
- Script Type – select which script you want to host locally – default or minimal GA4 version. We do not recommend using Minimal version as it needs a third-party external script for reporting the data.
- Track Logged in Admins – disable this option so that administrators pageviews are ignored. In this case, you should use different browser or other session in incognito mode while testing whether the site uses local Analytics code.
- Use MonsterInsights – enable to integrate with MonsterInsights plugin if you are using it on your site. This option is supported only when using default version.
- Enable AMP Support – use to track AMP pages.
Testing Local Analytics Setup and Speed
After filling up the details, save your changes and the next step is to test everything is working fine on your site. The easy way is to open the site in another browser or in incognito mode and check the source code. It should show something like below with Perfmatters code instead of the original Google Analytics code.
Go to your Google Analytics account and confirm you can see the correct real time users and the “Data Streams” section shows “Receiving traffic in past 48 hours”.

Finally, go to Google PageSpeed Insights tool and confirm there are no analytics scripts related warnings or errors in any of the sections.
Final Words
It is easy to host Google Analytics locally in WordPress sites using Perfmatters plugin. This will help to improve the site and you can also use other options available in the plugin to host Google Fonts locally, remove unused CSS and disable unused script files selectively on pages. Along with a good caching and SEO plugin, you can have a perfectly optimized site for better user experience.





