Use code PERFMATTERS for an extra 10% off!
  1. Home
  2. Docs
  3. General
  4. How to enable Local Analytics (host Google Analytics on your WordPress site)

How to enable Local Analytics (host Google Analytics on your WordPress site)

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.

Step 1

Click into the Perfmatters plugin settings.

Perfmatters plugin settings
Perfmatters plugin settings

Step 2

Click on the “Analytics” menu.

Perfmatters Analytics
Perfmatters Analytics

Step 3

Input your Google Analytics tracking ID. This is also referred to as a measurement ID. Then select “Enable Local Analytics.”

Enable Google local analytics
Enable Google local analytics

Step 4

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.

Tracking ID

Input your Google Analytics tracking ID, also referred to as a measurement ID. If you don’t know your measurement 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.

Performance tip: Take advantage of our Delay JavaScript feature to prevent Google Analytics from loading any requests until user interaction.

Script type

Perfmatters has two different script types for hosting analytics locally on your WordPress site. They are loaded via the async method for best performance:

Google Analytics 4

gtagv4.js is 51.5 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.

  • IPs are automatically anonymized.
  • Track events and outbound clicks without any additional code.

Google Analytics 4 Minimal

Minimal v4 analytics (analytics-minimal-v4.js) is a smaller and optimized open source script that is only 2.2 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 gtagv4.js, such as engagement time.

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.

Note: Both script types 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. However, it happens after the page loads. So it won’t impact performance.

Track logged-in admins

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.

Use MonsterInsights

Support: gtagv4.js

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 (gtagv4.js) locally while still being able to take advantage of the awesome MonsterInsights features.

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, your measurement 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: gtagv4.js, analytics-minimal-v4.js

Enable support for analytics tracking on AMP sites. This is not a local script, but a native AMP script.

GDPR

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).

Troubleshooting analytics

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 toggle off the option to “Place Google Analytics 4 code.” You can find this under “Settings → Connected Services → Analytics → Edit.”

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.”

Was this article helpful?

Related Articles