Prevent Google Analytics from being blocked by AdBlocker
You may say what you want about Google Analytics, especially about how “they” are, apart from displaying data for you, using data gathered for “their” business purpose (forget about privacy). If you are a website owner and you are looking for a reasonable tracking method for your visitors, this is the solution that you will pick in the first place.
The problem with Google Analytics is that their tracking script weights a lot and that is slowing down your website.
Referred below code is based on Universal Analytics that will become obsolete by Google on 1st July 2023. Read A very Minimal Google Analytics 4 Snippet to use a similar approach with Google Analytics 4.
To solve this problem, but maintain basic use of your analytics, you can replace Google’s tracking method with a snippet, a portion of it, that will do exactly what you need. This snipped is called Minimal Google Analytics and was prepared by DavidKuennen and published in unminified form on GitHub Gist.
Thanks to replacing Google tracking code, which weighs 73kB, we go down to just 1.5kB with the snipped provided.
If you are concerned about site speed and your Core Web Vitals metrics, replacing an externally called tracking script with minimal analytics is the first thing that you shall do.
All “my” websites are currently using this snipped and I am getting all the analytics that I need from it.
Once visitors visit a page, the snipped sending a signal via
/collect URL to Google Analytics with specified tracking code
UA-XXXXXXXXX-X. Simple as that.
When I compared statistics from 2021 for my websites to what I got in 2020 I noticed a huge drop in traffic across all sites.
Despite huge improvement in page speeds thanks to Hugo and a lot of SEO aspects implemented through the year, I couldn’t figure out what was going on.
Recently, I noticed that this snippet was not reporting my visits to my website.
I thought that something was not right.
If my visit is not recorded (and I don’t have any exclusions for that on the Google Analytics side), how many visits am I losing every day?
I excluded fault in snipped on its own, as it is working well.
This makes me think. Something is blocking my snipped and preventing me from reporting visits back to Google Analytics.
Then I looked at what changed on my side.
I am using AdBlocker (AdBlock Plus particularly) along with the DuckDuckGo browser extension.
When dependent on the blocking list, AdBlock did not block Google Analytics by default, the DuckDuckGo extension is doing it out of the box to protect our privacy. I understand that I do, but still, I want to know who is visiting my website, and I want data to be accurate.
Analysing various blogs I noticed that over the years many people reported similar behaviour, that a significant percentage of visitors blocked Google Analytics by default.
It’s like Marko Saric in his post mentioned that 13% of my website visitors block Google Analytics, there is nothing more than agree.
This percentage is growing more and more.
On one of “my” websites with significant traffic, I saw a drop of 12.43% in the number of users in 2021 compared to 2020 and a 51.06% drop in the number of pages visited in the same period.
I want to do something with that.
I even looked and started a short trial of Plausible Analytics, a simple and privacy-friendly Google Analytics alternative.
There was one issue with it, sorry! I didn’t want to pay for it, especially on private websites that do not generate profit and traffic that is not worth adding extra cost to it.
As Plausible mentioned, they are not - yet - blocked by AdBlockers, like Google Analytics is by default.
We’re not blocked to the same extent as Google Analytics (neither Firefox nor Safari blocks Plausible) but we’re blocked by some block list maintainers who have taken the stance that they want to block every tracking script and don’t want to have the responsibility to judge what’s good and what’s bad.
The situation is fluid and they know, that at some point their solution will land on a block list, despite their efforts to change the word giving a real privacy-oriented alternative to what Google is offering.
We’re not blocked to the same extent as Google Analytics (neither Firefox nor Safari blocks Plausible) but we’re blocked by some block list maintainers who have taken the stance that they want to block every tracking script and don’t want to have the responsibility to judge what’s good and what’s bad**.
The problem is like Plausible mentioned, some eager people want to block everything, despite that they - Plausible - want to change the tracking industry for the better.
They knew that at some point they will need to fight with AdBlockers and they are ready for that providing a solution for various scenarios.
The way how AdBlockers are working, apart from the keyword-based approach, is that they analyse external links on our website. If they see a reference on my website to external
google-analytics.com, they will block it. This is what the DuckDuckGo extension (and browser) is doing by default.
AdBlockers however are not blocking “things” originating from the origin domain - our website address.
You may think in that case, that you can serve Google Analytics script from your domain that will solve the problem. Not necessarily.
Google is forbidding this practice and can penalise you for doing that by blocking your account. Also, we removed the Google script by snipped, hence there is nothing else to serve from our side, as all are already.
With one exemption.
Minimal analytics snipped is sending “ping” to
www.google-analytics.com/collect each time there is new traffic on my website.
This of course will be blocked, like in future
plausible.js may be.
Plausible presented a solution by masking their tracking script by proxying through the originating server.
I am hosting my website on Netlify so I looked at how they did Proxying Plausible through Netlify and figured out, that I can do that same with my Minimal Analytics snipped that I am using.
I can proxy my call to
_redirects rule and pretend that I am “collecting” traffic from my domain.
No reference to
google-analytics.com, so nothing is blocked.
So I put into my
_redirects file the following line:
/collect https://www.google-analytics.com/collect 200
www.google-analytics.com/collect with my domain. In that instance, it’s
As you will see, I will still collect traffic through snippets, but theoretically, I will pinpoint to my website. In the background, behind AdBlocker’s ability to trace, my
/collect call will be redirected to Google Analytics.
Of course, it’s worth replacing
/collect (in the snippet and redirect rule) with some random string (unique per website) in case somebody will decide to add this phrase to the block list.
The downside #
There is one significant downside of this solution.
The redirect rule that is happening in the background, far away from AdBlock eyes, happens on the hosting provider server. As I am using Netlify, it happens on the Netlify CDN side, hence this will happen on the Netlify distributed network of servers - the closes server to my location.
In that case, when I visited my website from the United Kingdom, the closest, less busy server is in Germany, hence all traffic sources will show that Active Users suddenly all come from Germany.
This is of course not true. If we want accurate data on that aspect, then we need to stick with the original call from snipped to
www.google-analytics.com. In that case, we will lose up to 30% of real traffic information but will gain information from where our users are coming from.
But are data with up to 30% error worth analysing?
I made a decision, that I would like more to know how many users are visiting my website than knowing from where they come from.
I know, that my Polish part of the website will have a majority of users coming from Poland, where the English part will have much more distributed users around the world.
Unless you target your website to a specific region and you need to know how many users are coming from which country, then this information is not as important. In my instance, I would rather like to know which parts of my website are most read so I can concentrate on content that I create to find what readers would like to read. The 70% accuracy is not the best source of information.
This downside is not the only issue with the above snippet. If you are using Plausible on Netlify, then you will have similar behaviour from their end when using the proxying method.
This typically happens if in a script we will refer to just part of the URL string and not to the full domain name and string, like in my above example.
I am using only the Image Compression part of asset optimization as Minimal Analytics, already in the pre-minified version (and other JS served) and if I am calling CSS or JS in my Hugo website, I am already using a build-in minifier along with fingerprinting, hence pointless is to minify things twice.
If this “downside” doesn’t put you out, then, despite losing accuracy in location tracking, you will see much closer to the real number of visits. But, if you need accurate data including the location of your users, and you are willing to pay for it, try Plausible Analytics.
Netlify also offers its own, server-side, payable analytics solution, so you got an option to try.