Speed up your blog with self-hosted Google Analytics. The Blogging Musician @ adamharkus.com

Speed up your blog with self-hosted Google Analytics

Speed up your blog with self-hosted Google Analytics. The Blogging Musician @ adamharkus.com
Speed up your blog with self-hosted Google Analytics. The Blogging Musician @ adamharkus.com

One of the best ways of driving traffic to your blog is to increase its PageSpeed score. Find out how self-hosting your Google Analytics script can help.

Why does Google Analytics slow your site down?

ANY external call, including the call to https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID” will slow your site down, so it pays to minimise these as much as possible.

Here’s how the tracking script usually looks on your site. Placed between the <head> tags.

<!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'GA_MEASUREMENT_ID'); </script>

How do I stop Google Analytics from slowing my site down?

You have two options:

  1. Remove the tracking code and don’t use it. This is, of course, the fastest and simplest method, but not a great idea if you’re interested in tracking your site stats!
  2. Implement a self-hosted .js file (see below). This will allow you to continue tracking stats while mitigating the speed overhead.

How do I self-host Google Analytics?

  1. Go to https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID” and copy the contents of the page.
  2. In your hosting dashboard, go to your file manager and create a new file, analtytics.js for example. If you can’t do it yourself, you’re hosting provider can help you here.
  3. Paste the contents of 1. into the new file and save.
  4. Alter the contents of your tracking script as below. Note this is for an analytics.js file placed in the root of your site/blog.
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://adamharkus.b-cdn.net/analytics.js','ga');

ga('create', 'GA_MEASUREMENT_ID', 'auto');
ga('send', 'pageview');
</script>

Your site/blog will now access the analytics.js file directly, rather than having to call an external address.

What are the speed increases?

Previous GTMetrix scores were at the low 90% mark!

Can I self-host Google Adsense too?

Unfortunately not. This is both against Google’s T&C’s, and may also cause your blog to stop displaying ads, so don’t try it!

More Blog articles @ The Blogging Musician.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top