How to Host Google Analytics Locally?

Introduction

If you use Google Analytics on your blog, you will find the Leverage browser caching suggestion in your GTmetrix report. 

What does this exactly mean?

Whenever your site loads, there’s a Javascript file called analytics.js(about 18kb) that loads with your website every single time. It’s responsible for tracking user demographics and other useful statistics in real-time. 

This adds more seconds to your site’s load time. As we know, higher load-time leads to poor user experience.

The solution? You can host Google analytics locally.

It is done by caching the .js file locally in the user’s browse. Instead of loading the file via google server, it loads the file from the user’s device. By the end of this tutorial, you would have made your slow site into a lightning-fast site.

There are a variety of plugins to achieve this. I recommend four of them. (2 free and 2 premium plugins)

Advantages of hosting Google analytics locally

  1. Minimize DNS lookups
  2. Fix leverage browser caching warning in GTMetrix
  3. Boost your website performance
  4. Google loves you more

Drawbacks of hosting Google analytics locally(no plugins used)

Though there are plugins to achieve this, people sometimes opt into the non-plugin way. For which there are a few disadvantages to be considered. 

  1. It’s difficult to update the .js file regularly as google updates its .js files periodically.
  2. Creating a CRON job to do this automatically is not for not techies. Though you do this by scheduling a cron job in your CPanel account, not everybody is comfortable to get their hands dirty by messing with codes.

Note: Using a plugin resolves all these disadvantages

Backing up your analytics data

It’s ideal to backup your Google Analytics data in case anything goes wrong. Though the chances are between slim to nothing, it’s still a good safety measure.

Step 1 – Sign-in to your Google Analytics dashboard

Step 2 – To your left, you can find a column called reports.

Step 3 – Click on the report you wish to export. (in this case, it’s the audience overview) Note: Select the desired date range as the report’s data is similar to what you view on the screen.

Step 4 – Hit export and click your desired format to download your backup. The file would automatically be downloaded to your local disk.

Now that we’ve backed up our analytics data, let’s make your site faster.

Plugin 1 – CAOS  – Complete Analytics Optimization Suite (Free)

CAOS is one of the free yet highest rated plugins in the market.

Step 1 – Install and activate the plugin via your plugin dashboard.

Step 2 – Open CAOS settings.

Step 3 – Open a new tab and navigate to your Google Analytics dashboard.

Step 4 – In the search bar type admin and hit enter.

Step 5 – In the property menu, select the desired property(your website)

Step 6 – Click on tracking info > tracking code

Step 7 – Copy the tracking ID.

Step 8 – Paste in the Google Analytics Tracking ID. 

Choose “Always” if your blog collects only anonymous data or it doesn’t use any cookies. 

Choose “when cookie is set” or “when cookie has a value” if you wish to configure your website with a cookie notice.

.

Step 9 – Configuring Snippet Type

Leave it as default for accurate statistics. 

Choose asynchronous for a little boost in performance. This eliminates render-blocking resources which increases website speed time. 

Choose Minimal Analytics If you only need pageview stats. This boosts your website performance significantly at the cost of other stats.

Step 10 – Anonymizing IP

Turn on “anonymize IP” to prevent IP Tracking. . This is required by law in some countries.

Though you cannot view user’s IP addresses in your reports, google collects and strips them before processing the report. 

Step 11 – Positioning the Tracking code

Position the code in your footer if you have a fast-hosting. Else leave the setting unchanged.

Finally, save changes.

Plugin 2 – Swift performance lite (Free)

Swift performance lite is a simplified version of CAOS with additional features like lazy loading, CSS, JS optimization

Install the plugin and click bypass Google analytics and  paste your tracking ID 

Now click anonymize IP to prevent IP tracking.

Plugin 3 –  WP Rocket (Paid)

It’s a single plugin that packs more features than one could ask. 

Most importantly it’s easy for newbies to set it up without going through the hassle of reading guides for setting up the plugin.

Under Google analytics turn ON, and you’re done.

In addition to this, the plugin offers a plethora of features like Page caching, hosting Google Analytics locally, Gzip compression, Defer JS loading, eliminates render-blocking resources, Optimizes google fonts, Database Optimization and a lot more. 

Out of the four featured plugins, this stands out because of its simplicity and elegance.

Plugin 4 – Perfmatters (Paid)

Like WP Rocket, Perfmatters is yet another great plugin which offers many features for half the price of WP Rocket. 

Once you install the plugin, paste your tracking ID, set the tracking position as a footer (only if you have a fast hosting provider), choose your script type and anonymize IP.Disable display features as it generates a second HTTP/2 request and leave the other settings unchanged.

Conclusion

To summarize, you’ve optimized your WordPress site speed by hosting google analytics locally. You’ve also resolved the leverage browser caching issue by minimizing the extra DNS lookups. 

Feel free to ask questions if you have any doubts in the comments.