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, click into the Perfmatters plugin in your dashboard and then on the “Google Analytics” tab. To work correctly, you will need to at least input your tracking ID. Then select “Enable Local Analytics” and save the 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 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 four different script types for hosting analytics locally on your WordPress site:
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.
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.
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 all will work fine. However, it doesn’t have all of the features of
analytics.js. One of those would be site speed monitoring.
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.
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, and Minimal Inline
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, and gtag.js v4
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 MonserInsights 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 MonserInsights.
You still need to toggle on the “Enable Local Analytics” option 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.
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.