Do you want to Host Google Analytics Locally for your website? Then here it is.
By reducing DNS lookups and resolving the “leverage browser caching” problem from the script, hosting Google Analytics locally can help your site load faster.
Google’s script issues a caching warning, because by default their HTTP caching header expiration is set to a very short time frame. Your own CDN or server’s HTTP caching headers will be used if you host or serve Google Analytics locally. In other words, you have complete control over how the script is cached.
You can use a single HTTP/2 connection by hosting Google Analytics locally and serving the script from your own CDN or server. This will reduce the additional DNS lookups and speed up your website.
Here is how you can host Google Analytics Locally using Flying Analytics WordPress Plugin in 3 easy steps.
How to Host Google Analytics Locally using Flying Analytics Plugin?
1. Install and Activate Flying Analytics WordPress Plugin.
Login to your WordPress Admin dashboard. Click on Plugins and Add New. Search for Flying Analytics and click on the Install Now button. Then
2. Copy Google Analytics 4 Measurement ID
Go to your Google Analytics account>Admin>Data Streams>Select a data stream.
Here you will find your Google Analytics property Measurement ID. Copy it.
3. Add the Google Analytics Tracking ID in the Plugin
Next, go to WP Admin>Settings>Flying Analytics.
Now add the Measurement ID in the Google Analytics Tracking ID box and select the “Gtag.js v4” JavaScript method. Finally, Save Changes.
If you’re using any caching plugin then make sure to purge all caches after your hit the save changes button. Also, keep Disable for logged-in admins option is checked to hide your page visits in the Google Analytics stats.
Note: Google does not officially support this, but we have been using it successfully for years.
FAQs
Gtag.js vs Analytics.js vs Minimal Analytics?
Gtag (Global Site Tag) is a combination of Analytics.js and Conversion.js. Conversion.js is usually used for AdWords.
Analytics.js is the standard script for Google Analytics, which includes all the features.
Minimal Analytics is a “minimal” version of Analytics.js with only basic features. ‘Site Speed’ and some advanced features are not available. But basic features like realtime users, page views, users, location, device, traffic sources etc will work.
For 90% of users, Minimal Analytics should be enough.
Minimal Analytics.js vs Minimal Analytics Inlined?
Minimal Analytics.js injects a JS file at the footer (deferred). This takes advantage of browser caching and CDN. But results in an extra request.
Inlined version injects the script directly in HTML. So no extra request is required. But increases HTML size, no caching or CDN.
Minimal Analytics.js is generally recommended. But if you want better scores in GTmetrix, use inlined version.
Why I’m still seeing a request to https://www.google-analytics.com/collect?
Only the JS files are hosted locally. The data collected needs to be sent to Google for reporting. This can’t be avoided.
Conclusion
It is one of the easiest way to locally host Google Analytics on your own web server or via CDN. If you’re using any caching plugin then make sure to purge all caches after your complete step 3 above.
Like this article? Do share it with your friends on social media.
RELATED: