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 marthijnhoiting.com. 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 google-analytics.com 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.

https://www.google-analytics.com/collect?v=1&t=event&ec=Blocking&ea=Google%20Tag%20Manager&ni=1&cid=1234567890&tid=UA-XXXXXX-X

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

24 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?

    Cheers!

  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?

    Cheers

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

Leave a Reply

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