Google pushes website owners to improve the speed. However, ironically all Google products will drag site’s page speed score. This is due to the fact that you have to call third-party Google domains to use their Fonts, AdSense, Analytics, etc. In our earlier article, we have explained how to disable Google Fonts and here we will explain how to host Google Analytics locally on your server to improve page loading speed.
Checking Diagnostics in Google PageSpeed Insights Tool
Why to Host Google Analytics Locally?
There are many advantages of hosting the script locally on your server:
- You do not need call external server and use your own server. This will reduce number of HTTP calls from your site.
- Sometimes Analytics script will load slowly which you can avoid by hosting locally.
- You can set your own HTTP caching header instead of using Google’s to avoid leverage browser caching issues.
How to Host Google Analytics Locally in WordPress?
Whatsoever is the reason, you can easily host Google Analytics script locally in WordPress using Perfmatters plugin. This is most recommended performance optimization plugin for removing unnecessary bloats from your site and improve page loading speed. However, this is a premium only plugin which you have to purchase before using it on your site.
Install Perfmatters Plugin
First go to the developer’s website and purchase Perfmatters plugin for $24.95. You can get 10% discount when purchasing and the plugin so the net price will be nominal for anyone. After purchasing, download the plugin zip file and upload on your WordPress by navigating to “Plugins > Add New” section in your WordPress dashboard.
Finally, make sure to activate the license under “License” tab by navigating to “Settings > Perfmatters” section.
Setup Local Google Analytics Hosting
Follow the below instructions to host Google Analytics locally in WordPress:
- Go to “Settings > Perfmatters” menu and navigate to “Options > Analytics” tab.
- First turn on “Enable Local Analytics” option.
- Enter your tracking ID from Google Analytics account. Remember, this will vary depending upon the type of account you have created with Google Analytics. For the old Universal Analytics, you will see the tracking ID starting with UA- while the GA4 setup will have the measurement ID starting with G-.
- Leave the tracking code position as header which is the default option. If required, you can also change the position to footer for delayed loading.
- Next, select the script type depending upon your tracking ID. Make sure to enter correct tracking or measurement ID and select proper script type from the dropdown.
- The script type dropdown has five options – analytics.js, gtagv4.js, gtag.js, minimal and minimal inline.
- If you do not know the script type, you can check your Google Analytics integration code in your page source or from your Analytics account. The script should contain a word “gtag.js” or “gtagv4.js” or “analytics.js”. You can check and select the script type accordingly from the dropdown.
- The minimal and minimal inline options work with analytics.js to send minimal information like page views, users, locations, etc. using an open source script from minimalanalytics.com. Hence, this setup will call an additional script for integrating Google Analytics and we do not recommend selecting these options.
- Below is how it should look like when you use gtag.js script.
Tracking ID, tracking code position and script type are sufficient to finish your local Analytics hosting setup. However, you can use additional available options to optimize the setup further. Remember, available additional options will change based on the script type you select.
- Disable Display Features – this will disable remarketing and advertising feature in Google Analytics and save one HTTP request.
- Anonymize IP – you can use this to comply with GDPR and other privacy laws by anonymizing user’s IP.
- Track Logged In Admins – enable this option to include logged in users also in Analytics reporting.
- Adjusted Bounce Rate – enter time in seconds to evaluate adjusted bounce rate.
- CDN URL – use URL CDN URL if you are serving script file from CDN.
- Use MonsterInsights – manage Analytics integration with MonsterInsights if you are using that plugin while hosting Google Analytics locally with Perfmatters.
- Enable AMP Support – use this to enable support for AMP pages on your site.
After finishing your additional options setup, click “Save Changes” button to finish the task.
Remove Existing Google Analytics Setup
Since you have setup Google Analytics locally using Perfmatters plugin, you can remove any of your existing setup. For example, we use header hook in GeneratePress theme which we simply change to draft status to disable the Analytics integration.
If you are using separate plugin, make sure to disable the plugin. Similarly, remove the Analytics tracking code from the settings page if you use theme settings.
Before you check again Google PageSpeed Insights, make sure you do the following two things:
- Clear your cache by clicking on “Purge Cache” option available in your caching plugin. Also, clear the server level cache if you have any and your browser’s cache (this will log you out, so first save all your changes or use another browser). Your source code should show the Google Analytics script from Perfmatters plugin like “/perfmatters/analytics.js” or “/perfmatters/gtag.js” as shown below:
Test in Google PageSpeed Insights Tool
Now, check any page in your site in Google PageSpeed Insights. You will see the Analytics script issues are disappeared.