Pros and Cons of Hosting Google Analytics Locally

Analytics and Tracking

Author: Mike Ciffone

Published: 03.06.2024 / Updated: 03.06.2024

Benefits:

Performance Improvements

Hosting Google Analytics locally can speed up your site by reducing DNS lookups, and potentially decrease the time it takes to download the script. You would see the most impact if your server is faster or geographically closer to the end-user than Google’s CDN. If shaving every millisecond of load time is critical, this can be worth it.

Caching Control

The Google hosted gtag.js file notoriously sets a short cache expiration header, which causes the “leverage browser caching” warning in Lighthouse/PageSpeed Insights reports. When hosting the script on our servers instead, we have complete control over the caching policy and can set longer lifetimes. Setting a long cache lifetime makes it so the browsers of repeat users don’t have to re-download it from Google’s servers.

Avoiding Third-party Requests

By hosting Google Analytics locally, you reduce the number of third-party requests made by your website. This can slightly improve loading times and reduce the website’s dependency on external services for critical functionality.

Drawbacks and Considerations:

Frequent Updates

Google updates gtag.js frequently with new features, bug fixes, and performance improvements. This is why you might see the “leverage browser caching” warning in a PageSpeed Insights report. The headers we set on our servers doesn’t apply to 3rd party requests. When hosting the script locally, you’re responsible for manually updating it to ensure you’re using the latest version. Failing to keep the script updated can lead to missing out on new features or bug fixes.

Possible Impact on Data Accuracy

There’s a potential risk that local hosting might affect the accuracy of analytics data. Google’s CDN is optimized for global delivery, ensuring that the script is served quickly from a location close to the user, which can affect tracking accuracy and real-time data processing.

When Is It a Good Idea?

High Traffic Sites

For websites with massive traffic, where reducing even a small amount of load time can significantly improve the user experience (UX).

Sites With Specific Compliance Needs

In environments where data governance and privacy regulations require strict control over script sources and data handling (like HIPPAA), hosting Google Analytics locally might be part of a broader compliance strategy.

Those Who Want Greater Technical Control

For teams that prefer having fine-grained control over all aspects of their site’s operation, including the hosting and caching of third-party scripts.

Conclusion

While there can be performance benefits to hosting Google Analytics locally, there are some downsides. Generally, I recommended to using the Google hosted version to ensure you’re always using the most current version of the scrip because simply optimizing how the script loads can usually solve the problem in most cases. For that check out my promise based method of loading Google Analytics asynchronously.

If you do decide to host Google Analytics locally, just make sure to establish a routine process for checking and updating the script. Even then, I would probably still recommend something like Cloudflare Zaraz.