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?
The primary cause for the notification to appear is another extension that is preventing JavaScript from running, hiding ads, or blocking trackers and cookies. Some of the most common culprits encountered are other ad blockers, Kaspersky Anti-Banner, Ghostery, PrivacyBadger, Disconnect and NoScript.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With