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
Update 2021-04-12: you might want to call the file prebid-ads.js
or ads-prebid.js
something, because not all ad blockers block files with name like ads.js
anymore.
This is the only line of code in that file
var canRunAds = true;
Then somewhere in my 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>
Files like ads.js are blocked by at least these adblockers on Chrome:
- AdBlock
- Adblock Plus
- Adblock Pro
- Ghostery
Update on 2019-02-15:
Added Ghostery in the list above because the extension now also blocks requests to ads.js. Very handy. Does this mean that Ghostery is actually helping us devs to detect the blocking of ads with their extension?
Does not work with:
Privacy Badger