I would like to be able to detect if the user is using adblocking software when they visit my website. If they are using it, I want to display a message asking them to turn it off in order to support the project, like this website does.
If you enter to that site and your browser has some kind of adblock software enabled, then the site instead of showing the actual ads shows a little banner telling the users that the ad revenue is used for hosting the project and they should consider turning Adblock off.
I want to do that on my website, I'm using adsense ads on it, How can I do that?
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 2020-07-27: you might want to call the file prebid-ads.js
or something, because uBlock Origin does not 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:
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