Hosting Google Analytics locally can help speed up your site by reducing extra DNS lookups and resolving the “leverage browser caching” issue from their script.
Ironically Google’s own script throws a warning about caching, but this is because they have their HTTP caching header expiration set really short. When you host it yourself, your own CDN or server’s HTTP caching headers will automatically be applied. In other words, you have full control over the caching of the script.
Hosting Google Analytics locally and serving the script from your own CDN or server also lets you take advantage of a single HTTP/2 connection.
Note: This is not officially supported by Google, but we have been doing this for years without any issues.
Enable Local Analytics
To enable Local Analytics, follow the steps below.
Click into the Perfmatters plugin settings.
Click on the “Analytics” submenu.
Scroll down and click “Save Changes.” Make sure to also clear your site’s cache.
Our plugin uses a CRON job that runs once daily to grab the latest copy of the latest Google Analytics script from their servers.
Important: Don’t use this feature with other Google Analytics plugins, otherwise you might load the script twice which could skew your data.
Input your Google Analytics tracking ID. If you don’t know your tracking ID you can find it here.
Tracking code position
Load your analytics script in the header (default) or footer of your site.
Google Analytics advises you load the script in the header of your site. But this is simply to prevent any issues if someone aborts loading the page (in rare cases it might not fire). If you have a fast loading site, loading GA in the footer is completely fine and won’t impact your page view counts. In fact, we load GA in the footer on all of our sites.
Perfmatters has six different script types for hosting analytics locally on your WordPress site. They are loaded via the
async method for best performance:
analytics.js is 18.4 KB and is the standard Google Analytics script with all features included. This is the default option. It’s hosted locally and will be cached in your browser and on your CDN.
It doesn’t collect demographics or interests data.
gtagv4.js is 51.5 KB and is the latest version of Google Analytics. This allows you to track events and outbound clicks without any additional code and uses Google’s brand new analytic’s interface. (How to find your measurement ID)
gtag.js is 39.3 KB and is a slightly older version of Google Analytics. The one downfall to this method is that it also loads a 19.5 KB
analytics.js file. But both of these scripts will be locally hosted. This method is the most compatible with third-party solutions and is still the most widely used approach. (How to find your tracking ID)
You can use your CDN URL when referencing
analytics.js from inside
gtag.js. Example: https://cdn.example.com
Minimal analytics (
analytics-minimal.js) is a smaller and optimized open source script that is only 1.5 KB. Reporting for page views, users, locations, devices, traffic sources, and real-time will all work fine. However, it doesn’t have all of the features of
analytics.js. One of those would be site speed monitoring. It also doesn’t collect demographics or interests data.
For a lot of WordPress users, minimal analytics is a great fast option because we’ve found that many don’t use a lot of the extra data that Google Analytics provides, they simply want to see things like visits and traffic sources.
This file is hosted locally and will be cached in your browser and on your CDN.
Minimal inline is the same code from
analytics-minimal.js, it’s simply loaded inline on your site. This will reduce an HTTP request for the file, but also increase the total size of your HTML page.
Minimal v4 analytics (
analytics-minimal-v4.js) is a smaller and optimized open source script that is only 1.6 KB. Reporting for page views, users, locations, devices, traffic sources, and real-time will all work fine. However, it doesn’t have all of the features of
Note: Both minimal analytics options will generate a request to
https://www.google-analytics.com/collect. This is simply a way to send Google the reporting data. Unfortunately, you can’t host that part locally, and it’s required with this method. However, both minimal options will still be much faster due to the smaller script file size.
Disable Display Features
Disable remarketing and advertising which generates a 2nd HTTP request to DoubleClick.
Support: analytics.js, Minimal, Minimal Inline, and gtag.js
Hide your IP to comply with privacy restrictions in some countries.
In Google Analytics 4 (gtag.js v4), IP addresses are automatically anonymized.
Track logged-in admins
Support: analytics.js, Minimal, Minimal Inline, gtag.js v4, and gtag.js
Include logged-in WordPress admins in your Google Analytics reports.
Important: If you leave this off, you won’t see the analytics script when you’re logged in as an administrator on your site. But you can confirm your script is running by checking for it when you’re logged out or in incognito mode.
Adjusted bounce rate
Set a timeout limit in seconds to better evaluate the quality of your traffic. (1-100) We personally use 30 seconds. Read more about why you might want to use adjusted bounce rate.
Google Analytics 4 has discontinued bounce rate and moved to engagement rate.
MonsterInsights is a popular plugin used by over 2 million users to add Google Analytics to WordPress. You can now use MonsterInsights along with Perfmatters. It enables you to host the Google Analytics script (
analytics.js) locally while still being able to take advantage of the awesome MonsterInsights features like GA event tracking.
It’s important to note that when the MonsterInsights option is enabled, all additional Google Analytics settings are then handled by their plugin, not Perfmatters. For example, excluding admin users from being tracked, anonymize IP addresses, your tracking ID, all need to be set in MonsterInsights.
You still need to toggle on the “Enable Local Analytics” option and input your tracking ID to use this integration. When MonsterInsights is enabled, Google Analytics in Perfmatters is automatically disabled, so you don’t need to worry about the script loading twice.
Enable AMP support
Support: analytics.js, Minimal, Minimal Inline, and gtag.js
Enable support for analytics tracking on AMP sites. This is not a local script, but a native AMP script.
As announced by Google, Universal Analytics will be going away on July 1, 2023, and will stop processing any new data. After that, only Google Analytics 4 will be available. However, the old and new versions are very different, including the UI. Therefore, Google recommends running both the old and new versions side by side until you’re comfortable making the complete switch. This allows you to retain historical data in both profiles.
You can run both Google Analytics 4 and Universal Analytics simultaneously in Perfmatters. To do this, ensure you have first selected gtag.js as the “Script Type.” Then toggle on “Dual Tracking.”
Then input your Google Analytics 4 Measurement ID into the “Measurement ID” field.
Google Analytics 4 relies on first-party cookies, which keeps them compliant with new privacy laws like GDPR and the California Consumer Privacy Act. And IP addresses are automatically anonymized.
If you’re looking for a good cookie solution (opt-in consent) that works with Perfmatters Analytics, we recommend the Real Cookie Banner. The pro version of their plugin has an integration with Perfmatters (gtag.js, gtag.js v4). Note: This doesn’t work with our Delay All JS option.
If you want a Google Analytics alternative, we highly recommend Fathom Analytics. It’s privacy-focused, GDPR compliant, and the script is lightweight (under 3 KB).
When you are hosting your analytics locally, Google’s Tag Assistant Chrome extension won’t pick up analytics running on your site. This is completely normal, as they are only looking for it on their servers.
An easy way to confirm that Google Analytics is working is to open up a browser in Incognito Mode and visit your site. Look at the “Realtime” view in Google Analytics, and you should see yourself show up.
Site Kit by Google
You can use Site Kit by Google alongside the Local Analytics implementation in Perfmatters. Just make sure you go into the advanced settings in Site Kit and toggle off the option to “Place Universal Analytics code.”
Not detecting custom events
Our plugin uses a CRON job that runs once daily to grab the latest copy of the latest Google Analytics script from their servers. If you don’t see a newly created event showing up, it could be that the file is cached. Make sure to clear all cache layers on your site (CDN, page cache, etc.).
Script not loading
If you’re using another optimization plugin, you might need to exclude the Google Analytics script and optimizations being applied to it. For example, if you’re using Autoptimize, make sure to exclude the script and turn off the feature “Minify excluded CSS and JS.”