How to detect Adblock on my website?

My solution is not specific to a certain ad network and is very lightweight. I’ve been running it in production for a few years. AdBlock blocks all URLs containing the word “ads” or “prebid”. So this is what I did:

I added a small js file to my webroot with the name prebid-ads.js

This is the only line of code in that file. Update 2022-04-26 Call this variable something else, see below!

var canRunAds = true;

Then somewhere in your page:

<html>
  <head>
    <script src="/js/prebid-ads.js"></script>
  </head>
  <body>
    <script>
      if( window.canRunAds === undefined ){
        // adblocker detected, show fallback
        showFallbackImage();
      }
    </script>
  </body>
</html>

Update 2022-04-26 uBlock Origin loads their own ads-prebid.js that reverts the trick described in this answer (proud!), their file contains the following:

(function() {
    'use strict';
    window.canRunAds = true;
    window.isAdBlockActive = false;
})();

As a solution just rename your canRunAds variable to something fun, like window.adsAreWithUs or window.moneyAbovePrivacy.

Discovery and workaround by Ans de Nijs. Thanks!

Supporting extensions

Files like ads.js are blocked by at least these adblockers on Chrome:

  • AdBlock
  • Adblock Plus
  • Adblock Pro
  • Ghostery

Does not work with:

Privacy Badger

Leave a Comment