How To: Detect Ad Blockers (adblock, uBlock, etc)

As a blogger or any other kind of publisher, you might be interested in if the user accessing your page right now is using an ad blocker or not. You might want to do that to display a box that encourages them to disable their ad blocker, because you’re trying to make a living off of blogging or to make more conscious decisions about how you’re going to layout your page.

If a user has an adblocker installed, you don’t have to reserve the space in your layout for the ad units, so you might as well get rid of them, so in this post we’re going to have a look at how we can determine if a user has installed an ad blocker like Adblock, Adblock Plus or uBlock.

Disclaimer: Some countries have discussed if it is legal for a webmaster to determine if their users have an adblocker installed or not, because it technically is a method of figuring out what software a user has installed, which quickly becomes a privacy matter.

How do Adblockers work?

Basically Adblockers are a huge blacklist of which files not to load or from which domains NOT to load files. This goes for Brave, Adblock, Adblock Plus and uBlock as well.

So when you open a page, this adblocker looks at what (mostly JavaScript files) and domains are on its blacklist.

https://pagead2.googlesyndication.com/*
https://static.ads-twitter.com/uwt.js

and every time your browser will try to load one of these scripts or from one of the listed domains, the blocker will stop your browser from loading that and as a result, the ads will not be loaded and also not displayed.

More important than not seeing the ads for most users is that they will not be tracked and no major company will know that they looked at a new swim suit and therefor spam them with swimsuit ads for the rest of the year.

You can have a look at a huge collection of blacklists for adblockers at filterlists.com

In your browser you can see if your plugin has been active by looking at your JavaScript console in the developer tools:

chrome devtools blocked request

Note the message: Failed to load resource: net::ERR_BLOCKED_BY_CLIENT, which is caused by your adblocker.

Vanilla JS Adblock / ublock Detection

If you want to know if a user has an ad blocker installed, it’s quite trivial with a few lines of JavaScript. You’ll just need an example script or file to load that you know will be blocked, if an adblocker is installed.

Let’s try to write a JavaScript function that tries to load a script we know will be blocked. Try enabling and disabling your adblocker to test if it works properly:

See the Pen Detect Adblockers by Jonathan M. Hethey (@jonathanmh) on CodePen.

For this example I picked: https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js which is the default JavaScript file for Google Adsense.

This is the difference in page load when using GET vs HEAD, as you can see the HEAD request is significantly smaller / faster:

  • 25.1KB by using GET as the method
  • 70B by using HEAD as the method

If the current user has an adblocker installed it should read: ads are blocked else: ads are not blocked.

If you have any use cases for this, any comments or questions, leave them below or tweet at me!

Full code:

document.addEventListener('DOMContentLoaded', init, false);

function init(){
  adsBlocked(function(blocked){
    if(blocked){
      document.getElementById('result').innerHTML = 'ads are blocked';
    } else {
      document.getElementById('result').innerHTML = 'ads are not blocked';
    }
  })
}

function adsBlocked(callback){
  var testURL = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'

  var myInit = {
    method: 'HEAD',
    mode: 'no-cors'
  };

  var myRequest = new Request(testURL, myInit);

  fetch(myRequest).then(function(response) {
    return response;
  }).then(function(response) {
    console.log(response);
    callback(false)
  }).catch(function(e){
    console.log(e)
    callback(true)
  });
}

Thank you for reading! If you have any comments, additions or questions, please leave them in the form below! You can also tweet them at me

If you want to read more like this, follow me on feedly or other rss readers

8 thoughts on “How To: Detect Ad Blockers (adblock, uBlock, etc)”

    1. I would believe it works for generally all adblocker plugins, because it looks at the result of the network request

  1. Websites the aggressively try to bypass my ad blocker lose more trust. Try to circumvent my security, and I’ll make sure your site never shows me ads.

    It’s that simple. It’s time for better ads, stop trying to smuggle bad ones through our networks.

    Also, publishing bypasses makes it easier for us to block your ads.

      1. I read the post but I agree with . Although free sites deserve to be paid, all that said isn’t the same for sites, free or not, that impose obtrusive ads, spams and roadblocks on a user’s device(s). If you had a credit card eligible for online buying (unlike me) who would you you donate money to? The polite one (they only ask you, without detecting, and in a non-obtrusive way, if you would avoid the use of ad blocking but notices that you have your rights too, or simply doesn’t mind at all) or the less polite one, maybe even rude (they have a strict “law”, where you are not “authorized” to use the services on their “property” unless you avoid the adblockers)?
        IMPORTANT NOTE EVERYBODY, I’M NOT THE ONLY PERSON SAYING THIS : The only thing that could reduce the number of ad blockers is to promise and apply the exclusive use of reasonable ads, as in not annoying to any users. How difficult is that?! If a webmaster is too lazy to verify and take responsability for the ads he accepts, and attempts to hack someone else’s computer to monitor if they’re using adblock, he/she shouldn’t be shocked or find this unlawful if they see an ad blocker on their site. Anyway, adblock detection may be unlawful themselves in certain countries.

        1. I don’t think we agree on what qualifies as “hacking a computer”.

          Thank you for your comment, but it lacks a clear message or purpose.

    1. The author explicitly noted that it’s about detection.

      That said, the solution could certainly be used to serve more aggressive ads to visitors, but those ads would need to be properly masked (using random element structures, automatic javascript file obfuscation and rotation, plus the advertisements would need to be served from the current domain).

      One concern I have with the article is that requesting that Google’s ad URL could potentially expose visitors’ data to that corporation.
      A list of alternatives would certainly be useful.

Leave a Reply

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