How to Host Google Analytics Locally in WordPress?

In this tutorial, learn to host Google Analytics locally in WordPress. Boost your site performance and get a better score on speed testing tools including Google PageSpeed Insights, GTMetrix, etc.

In this guide, you will also learn

  • Why host Google Analytics locally?
  • How to host Google Analytics locally?
  • Use the CAOS plugin.
  • Try Flying Analytics in WordPress.
  • WP Rocket to optimize Analytics code.
  • Use Perfmatters plugin.
host google analytics locally in WordPress

Do you need to Host Google Analytics Locally in WordPress?

Optimizing and getting a perfect speed score is a dream for everyone. Since Google announced speed as a ranking factor, we wanted to make our website even faster so that we can rank higher in Search Engine Result Pages.

Generally, we use the Google Analytics code to track our visitors. While testing our site in GTMetrix or PageSpeed Insights, we get a warning about Leverage browser Caching for Analytics.

Google Analytics Leverage Browsing Caching

In order to get a higher score in the speed testing tool, we can fix this by hosting google analytics locally in WordPress sites.

Advantages of Hosting Analytics Locally

  1. Fix Leverage Browser Caching Warning.
  2. Boost your website’s performance.
  3. Improve your score in GTMetrix, Pingdom, Google PageSpeed Insights.
  4. Maximize your conversion.
  5. Minimize Extra DNS Lookup.

Disadvantages of Hosting Analytics Locally

  • Syncing your GA with the latest version might be a major problem.
  • You need to know about Cron Job.
  • If you use a minimal analytics version then most of the data are not collected by Google Analytics.
  • If you are using Woocommerce then you might be a big problem.
  • You may not get data for Remarketing or Retargeting while running Google Ads.

Where to find Google Analytics Tracking ID?

If you haven’t signed up in Google Analytics then you can simply visit analytics.google.com to get Tracking ID and tracking script.

If you have signed up then follow the process to find the GA Tracking ID

Where to find Tracking ID
  1. Log in to your Analytics Account.
  2. Click on Admin Menu
  3. Visit Account Column and Select the Account.
  4. Go to Property Column and Select the Property.
  5. Under Property Section, Click on Tracking Info > Tracking Code.
  6. Now, you can access the Tracking ID as well as Tracking GA Script.

Steps to Host Google Analytics Locally

Though hosting analytics locally is not supported by Google but these methods have been working for a long time. Hosting Google Analytics within your server is easy with these WordPress plugins. We have listed a few methods, some of them support to serve the JavaScript files using CDN.

Analytics.js, Gtag.js, Ga.js, Minimal Analytics.js, Minimal Inlined Analytics.js are the GA JS to track the visitors and you have to use one of these available JS tags that suit your WordPress sites.

1. CAOS | Host Google Analytics Locally

Among all the available plugin, CAOS is my favorite plugin to host Google Analytics locally in WordPress sites. It is a complete analytics optimization suite for WordPress and I have been using CAOS plugin built by Daan van den Bergh. He has built another famous plugin called OMGF – a plugin to host Google Fonts locally.

Check out: Optimize Google Fonts & Host Locally in WordPress

CAOS - Host Google Analytics Locally Plugin

CAOS Basic Settings

CAOS Plugin Basic Settings
  • Google Analytics Tracking ID: Enter your Google Analytics Tracking ID
  • Allow Tracking: For GDPR Compliance, choose the tracking options between Always (default), When cookie is set, When cookie is NOT set, and When cookie has a value.
  • Snippet Type: Choose between default (Synchronous i.e. render-blocking) and Asynchronous.
  • Position of Tracking Code: Add tracking code by using one of the available options between Header, Footer. The manual option is also available to load the Analytics tracking-snippet.

CAOS Advanced Settings

CAOS Advanced Settings
  • Compatibility Mode: CAOS is compatible with Monster Insights, Exact Metrics, Analytify, and WooCommerce GA Integration plugins.
  • Stealth Mode: Bypass ad blockers and track users in analytics by enabling Stealth Mode.
  • Which JavaScript to Use?: Analytics.js, Gtag.js, Ga.js are the list of options available. Analytics.js is the recommended option.
  • Analytics Path: Save analytics.js inside wp-content (locally). Leave this default.
  • Serve from a CDN?: Use CDN to serve analytics. I am using BunnyCDN
  • Enable Preconnect: Enable the Preconnect option to connect early with Google Analytics.
  • Capture Outbound Links: Track external links when the user clicks to leave your site.
  • Cookie Expiry Period: Let the Google Analytics cookies expire after some period.
  • Adjust Bounce Rate: Maintain the bounce rate in seconds between 0 to 100.
  • Change the Enqueue Order: Change the order of the analytics script. Leave default as 0 to trigger before everything.
  • Disable all Display Features: Disable Remarketing features in Analytics used in Google Ads.
  • Anonymize the IP Address: Anonymize the IP Address to respect the privacy of visitors in some countries. Enable this option for GDPR Compliance.
  • Tracking logged in Administrators: Enable or Disable Tracking of Administrators login in Analytics.

2. Flying Analytics

Flying Analytics is yet another plugin that helps to optimize our website by hosting analytics in the server. Flying Analytics is built by Gijo Varghese, who is a speed optimization expert.

Flying Analytics to host locally
Flying Analytics simple analytics configuration

Go to Settings > Flying Analytics and set up everything including Analytics Tracking ID. I have been using Flying Analytics plugin in my other site to speed up the site performance.

Available Methods in Flying Analytics: In this Analytics plugin, there are 4 JavaScript options available to load Google Analytics on your site.

Flying Analytics FAQs - Host Google Analytics Locally in WordPress
  • Gtag.js: Gtag is a Global Site Tag that helps to connect all the Google Analytics services. It is a combination of Analytics.js and Conversion.js tag and Gtag.js is used for Google Ads Conversion Tracking. Gtag.js has a size of around 66 KB.
  • Analytics.js: Analytics.js is the standard script with all the Google Analytics features included in it. It mainly helps to measure the user interaction and send it to the Analytics server. The size of Analytics.js is 44 KB.
  • Minimal Analytics.js: With only 1.4 KB in size, Minimal Analytics.js helps to speed up our site by deferring the analytics code in the footer by default but there will be an extra request.
  • Minimal Analytics Inlined.js: Inlined.js has the same size as Minimal Analytics but the difference is it is injected directly to HTML.

3. WP Rocket

If you are looking to speed up your WordPress site then WP Rocket is the best plugin in the market. Personally, I am using the WP-Rocket plugin to boost the performance of my site.

WP Rocket Hosting Google Analytics locally in WordPress
WP Rocket Hosting Google Analytics locally in WordPress

With various features available in the WP Rocket plugin, they too have features to host google analytics locally. Google Tracking One-Click Add-ons is one of the top features that helps to fulfill the PageSpeed recommendation.

Just go to Settings > WP Rocket > Add-ons > Turn On Google Tracking Addons

Note: Google Tracking Addons doesn’t work if you have installed Google Analytics Code via Google Tag Manager.

4. Perfmatters

Perfmatters is a web performance plugin to boost your WordPress website. The plugin is built by Brian Jackson and his brother Brett Jackson. Brian Jackson was previously a CMO at Kinsta – Managed WordPress Hosting. Perfmatters plugin helps to boost the site speed in various ways and one of them is Hosting Google Analytics locally.

To host analytics locally using Perfmatters, you need to have a Tracking ID and simply use the available settings.

Go to Settings > Perfmatters > Google Analytics

Perfmatters plugin Google Analytics locally
Perfmatters plugin – hosting analytics locally

Features of Local Analytics in Perfmatters Plugin

  • Tracking ID: Browse your Google Analytics Dashboard. Go to Admin Menu > Select an account in ACCOUNT Column > Select property from PROPERTY Column > Click Tracking Info > Tracking Code
  • Tracking Code Position: There are two options available to load tracking code, one is at Header and other at the Footer. Google Analytics advise loading tracking code at the header. so, it’s your choice, in some cases, you might choose the footer section.
  • Disable Display Features: Disable all the advertising features including Remarketing and Advertising Reporting that generates extra HTTP request.
  • Anonymize IP: Alter the IP Address of visitors before sending the tracking data to Google Analytics to agree with the privacy in some countries.
  • Track Logged In Admins: Enable/Disable tracking of Admin login in Google Analytics.
  • Adjusted Bounce Rate: Set a time out limit from 0-100 to adjust the bounce rate in analytics.
  • Use MonsterInsights: Use Perfmatters plugin along with a famous Google Analytics plugin – MonsterInsights.

Summary

This is how you can host Google Analytics locally in WordPress. From the above article, you can use any method that is easier for you.

Self-hosting analytics is one of the best ways to increase the PageSpeed Score and reduce extra DNS lookup and resolve “leverage browser caching“.

If you have any other methods then please feel free to share below in the comment box.

Happy Optimizing

Share:

GET 100+ FREE SEO TOOLS

Quickly access over 100+ popular SEO/Marketing tools to Boost your website traffic.

Thank you for subscribing. Check your Email(Didn't receive? check Spam Folder)

Something went wrong.

I am obsessed with WordPress and write articles about WordPress Speed Optimization, SEO, Blogging. I mostly publish tutorials on GeneratePress and GenerateBlocks.

4 thoughts on “How to Host Google Analytics Locally in WordPress?”

  1. How can I design the sidebar like wplogout? Your all articles are super useful. I’m sure you’re the next useful blogger in the wordpress industry.

    Reply
  2. Thank you for your article. Finally, I use your recommendation for CAOS. I hope this method can make my web faster.

    Reply

Leave a Comment