Detect if someone is blocking Google Analytics or Google Tag Manager

These days the use of adblocking tools is growing fast. These tools aren’t always used only blocking the ads, also analytics software is blocked sometimes.

Browser extensions like Ghostery and uBlock are used to get rid of all those trackers. Why wouldn’t a non-marketer disable all the found tracking software? With a couple of clicks someone has disabled 2000 trackers.

Schermafbeelding 2016-01-30 om 12.41.32

In this case Google Analytics and Google Tag Manager are disabled on All the tags that should be loaded with GTM aren’t included anymore.

A user can have multiple reasons why he would do this. The problem is that our data gets more and more inaccurate. If 20% of our visitors is blocking Google Analytics we are missing 20% of the data we would like to have. Time to measure the percentage of our visitor that we can’t track anymore.

How to measure this?

After the page is loaded we are going to check if the Google stuff is loaded. Because i would track the GTM blockers as well we can’t use Google Tag Manager for this. So, we have to put our code in the source code of our website (the old school way). If we use GTM for this, the script isn’t loaded by visitors who are blocking Google Tag Manager.

Put the script above on your website. Clear the cache and reload the page. Now we are going to check if everything is going well. We are doing this by writing the output to the console so we can check if everything is setup right.

Right click on your page, click on inspect element to open the console tab. In this example I am using Ghostery to enable and disable Google Analytics and Google Tag Manager.

When you disable Google Analytics you will get the following result:

Schermafbeelding 2016-01-30 om 13.43.48

Schermafbeelding 2016-01-30 om 13.42.15

When you disable Google Tag Manager and Google Analytics should be activated by GTM you will get the following result:

Schermafbeelding 2016-01-30 om 13.45.22

Schermafbeelding 2016-01-30 om 13.44.52

If everything is detected right we could measure it with Google Analytics. This sounds strange, but we could use the Measurement protocol to send our data.

The most easy way is loading an image pixel with javascript. But there are Browser extensions that prevent the source from loading.

To show what is happening you have to replace the UA-XXXXXX-X with your own UA-code. The best practice is creating a new Google Analytics property, because the method described below will create a lot of duplicate (direct) traffic.

Copy this url into your browser and hit Enter.

Schermafbeelding 2016-01-30 om 15.31.52

If we load this url and you have uBlock installed you will get a notice like this.

Schermafbeelding 2016-01-30 om 15.03.24

If you have installed uBlock allow this source and reload the page again. If you don’t have, you will see a white screen. But you can watch the impact of this call in Google Analytics under Real-Time -> Events. If you don’t see anything happen make sure you do not have filtered out your own IP!

Schermafbeelding 2016-01-30 om 15.02.27

N.b. This is also the way a lot of referral spam is generated. They are generating UA codes and calling these kind of urls.

The solution is using PHP. What we are doing is loading an server side generated image with javascript if we detect someone have disabled Google Analytics or Google Tag Manager. Our uBlock software doesn’t detect it as an tracker.

Upload this script to the root of your server and name it collect.php.

Back to our script we are going to make some changes:

Replace the UA-XXXXXX-X everywhere in the script above with your own UA code.

Every pageview is now tracked as an event.

Go to Google Analytics and check if your events are registered as expected.

Schermafbeelding 2016-02-02 om 19.05.03

Was it interesting? Share it, so someone else can read this too!Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

39 thoughts on “Detect if someone is blocking Google Analytics or Google Tag Manager”

  1. Awesome Marthijn! This is a way better method than trying to measure ad blockers through (the possibly blocked) Tag Manager.

  2. Hi Marthin,

    first of all: what a great approach, thanks for sharing!

    BUT ;): I tested your setup in chrome, opera and firefox. For some reason, if I use Firefox, I sometimes get “GA is loaded” and sometimes “GA is not loaded” – GTM always gets loaded though.

    Do you have any explanation for that behavior?


  3. That’s just awesome mate, thanks for sharing such a qualitative tuto!
    It works great right after setting it up… in no more than 10 minutes.

    A custom dimension indicating blocking / allowing would be handy too.

    1. Oh sorry, that would be a nonsense since the server’s call is not tied to a visitor ID. So with this approach, we can see the ratio of (pageviews with GAor GTM blocked) / (pageviews with GAor GTM unblocked).
      Still very useful info!

  4. Hi Marthijn,

    Can you help me with the php code in Java as well
    my website backend is in JAVA and i want to test the optout for my website.

  5. The collect.php file is stored in the browser cache and therefore on page reload the collect.php event is not called again. Therefore I added a random string in JavaScript ,like this

    old line:

    img.src = ‘/collect.php?tid=UA-XXXXXX-X&ec=Allowing&ea=Google%20Tag%20Manager’;

    new line:

    img.src = ‘/collect.php?tid=UA-XXXXXX-X&ec=Allowing&ea=Google%20Tag%20Manager&’+ (new Date).getTime();

  6. Of je zou het het gewoon kunnen respecteren dat niet iedereen gediend is van tracking cookies en al dan wat niet meer om het gedrag van mensen te volgen.

  7. Hi Marthijn,
    GREAT attempt!!!
    Would/Could you share with us your findings?
    Can you tell, what is the actual ratio for your website?
    Is it about the 20%, you mentioned in your post?
    If anybody else uses this method, I would be glad, if somebody could share this. I suspect, that it certainly depends a little bit on the kind of website, you manage

  8. There might be a reason why the user disabled GA or GTM. I believe you are giving the user an opportunity to sue you by sending his private data out of your own domain.

    1. Har, Not if it’s in the terms-of-use on the site — if your site explicitly states it collects this data, the user has no right to sue – just the right not to visit your site (why terms-of use exist – to protect both parties). There is nothing illegal about gathering aggregate data to better your product – plus the funny thing is, most ad-blockers and track-blockers collect/track/report activity of their users – talk about irony!!

  9. This approach gives me headaches. Because of several reasons (mostly financial) I have 1Mbit to use, with another bundled 1Mbit that doesn’t apply to http requests. Yes. That’s 1Mbit.

    The problem is that it takes too long (according to the server and the ISP) to load things, and therefore I even get on both Windows, Debian and Mac (I don’t use adblock for some pages that I need) (I don’t even use Safari more than nessecary on the Macs neither Edge, but both of them get that annoying Please remove your adblocker).

    I can understand that. Even though I want to use adblocking software on several sites. And, understand, that I mostly have 1Mbit.

    When I have to click on something as “Like us on Twitter, G+, Facebook”, Whatever, if it takes too long a JS activates a closing curtain to the site I’m visiting. I blame this partly on both google and their programmers. Remember, not everybody has 1Gbit connections.

  10. Very useful. I recently noticed a drop in traffic after I implemented GTM (before we used GA directly), I was puzzled at first but then I found out that GTM is more likely to be blocked by ads blocker than GA.

    We have implemented a new tracking similar to what you suggest here. I would also like to add that if your GTM has tons of tags other than the GA you will not have those tags run when GTM is blocked. This is really bad if you put the livechat script inside the GTM tag for example. Beware of this issue.

    In my opinion, the decision of ad blockers to block GTM is really bad, because we could have many useful things to run in GTM, not just ads and trackers.

  11. Hi,
    Great post!
    I know you said this will increase the number the event hits and so I have set up another property as you suggested.

    These event hits are being sent directly to the property without any filtering. So, does that mean that these hits are being triggered by Bots and other spammers visiting the website along with legitimate human users?

    The site I have set up with this JS has very low traffic, is there a way to only send these event hits when real users arrive on the site instead?


  12. Amazing script and worked great!!!
    Does this work for Google Publisher for Doubleclick with any examples?

  13. Works great!

    Do you have a solution for people who get the warning and never return to your site?

  14. Wow really helped a lot for my daily Practice! Thanks a lot guru hoiting! Now i know this I can solve many of my problems! Big fan of your work!

  15. The main problem with this approach is that you’ll end with 2 new random user for every pageview the users with the block do. Better to store in a cookie a random value and use it as a cid.

  16. Hey, sounds like a nice solution. I implemented everything as described. But when I then view my Real Time stats in Analytics, I see too many “current users” (like 12 users, all from one single city). When I remove your code these users drop out of Analytics.

    I have also setup a new view in Analytics to just view results from “Blocking” users, but also this looks strange. Have you tested viewing stats in Analytics after you have implemented this ^^ code?

  17. Just a note from your basic web user worried about privacy.
    We understand your need for internal analytics. Trouble is, you use google’s free stuff. They give it to you free in exchange for you leaking my privacy to google. They build up a shadow profile on me. I in turn reject any ad if i get the slightest scent of facebook or google “personalizing” ads for me. I avoid both companies due to their Nazi type behavior. So i am offended that websites hand this to them freely. Anonyimized advertisement is fine. Profiling is wrong, so don’t expect users to do anything other than block. We cannot tell the difference between good and “bad” cookies, so block everything!

  18. If the “Google Analytics Opt-out Add-on” chrome extension is installed, I still get “Google Analytics is loaded” and “Google Tag Manager is loaded” appearing in console.

    Does the if( && ga.create) and if(window.google_tag_manager) code need updated to work properly?

  19. Where do you add the first script? You state “Put the script above on your website.” – but where would this be for a WrodPress site?

Leave a Reply

Your e-mail address will not be published. Required fields are marked *