Ultimate guide to detecting adblock software
I debated writing this article as I may be unleashing a new evil on the internet. Like most things, I came to the conclusion that if I don't, some other smart person eventually will.
I've been playing with the idea of adding ads to this site for a while, and recently I've been testing a couple different ad layouts. I myself run ablocking software and I noticed that they simply removed the ads without removing/hiding the containing divs. That meant that when my site was viewed with adblocking software enabled, there were huge white spaces in my site that could be used for displaying content.
That's when I set out on my path to find a way to detect ad-blocking software and change my site accordingly. The plan was to find out if the user or client was preventing ads from being displayed, and if so, get rid of the ad divs all together.
That's when I came across this site: http://detect-ad-blocking-software.webconrad.com/. This author was faced with a similar dilemma but wished to display a different message and donate button in the event that the ads weren't being displayed. The method he used is found elsewhere on the internet, so I don't know who to give original credit to. I simply chose his site because it was the easiest to understand and it actually worked.
His method involves making a fake or dummy "ad" that no ad blocker would possibly allow, then seeing if that made it through. If that ad didn't, it would follow that the real ones probably didn't either. If it did, then certainly the real ones are being displayed.
I tried the code myself and it does work for most ad blocking software. It didn't however, work for my firewalls built in ad blocking abilities. I use NetBarrier and when I have the "Banner Filter" enabled, that guys ad block detection method doesn't work. That's why I've written my own method which should work for any ad blocking software around. The key difference between his and my approach is that mine tests the actual ad itself to see if it was displayed, and his tests a decoy ad.
How it works:
- Get the ad code as the browser evaluates it with ad blocking software disabled.
- Get the ad code as the browser evaluates it with ad blocking software enabled.
- Search for a key difference that you can test between the two.
- Set up a simple if statement and change the code accordingly.
The HTML for my ad always looks like this: But that's not how it looks to the DOM after the browser has loaded it.
After that it's just a matter of running that script once with ad blocking software enabled and once without. Here are my results when I do that:
Disabled See the difference?
Now it's just a matter of picking a piece of the unblocked code to test for. In my case, I decided to test window.google_render_ad(). Here's the test: That will display an alert with what "type" the function google_render_ad() is. If you run that code with ad blocking software enabled you should get undefined. If you run it without ad blocking software, you should get function.
Using that information it is easy to write a simple if statement that contains code to change your website after it's been executed depending on whether ad block software is enabled or not: I think that's about all there is to say about that!
Please, use the code and method responsibly. Remember, web developers spend lots of time and money to bring you free web pages and clicking on their ads is just one way they can recoup some of that investment.