Detecting Ad Blockers in Javascript
Posted: Sep 28, 2013
ad-block-test demo
Ad Blocker Detected: unknown
Library Version:
Ad blocking is a oddly emotional subject for some people. I don't want an ethical debate, but I am interested in data. So I wrote a brief Javascript snippet that test for the presence of several common Ad Blockers.
The code and instructions for using it are available on the ad-block-test project on Github. You can try it out against your browser using the box at the right.
How it works
There's a bit of an arms race between ad filtering software and a small number of anti-ad filtering sites. This means ad blockers can generally only be detected indirectly by examining the effects they have on a loaded page.
My snippet works by creating an off-screen element with a class name commonly used for advertisements, waiting a beat for the ad blocker to kill it, and then checking to see if it's still there and the proper size and with no weird CSS styles applied to it. This is a very conservative approach in that it will only detect ad filtering software that 1) filter elements in a way that is detectable 2) has this particular class name blacklisted.
It will not find, for example, filtering based on blocking hostnames through a HOSTS file. However, it should work with the most common configuration for AdBlock Plus and similar software that uses EasyList filters. I suspect this represents a sizable portion of the population blocking ads.
Script Goals:
- Be able to gather general statistics about how many visitors are actively blocking ads via Google Analytics, Mixpanel, or other similar system.
- Minimal performance impact on visitors, whether they're blocking ads or not. Script doesn't make any additional network requests.
- Be conservative. Better to miss some ad blockers (false negative) than to incorrectly flag some browsers as having an ad blocker (false positive).
- Minimal prerequisites. Does not require JQuery. Does not require a particular ad serving platform or even any actual ads on the page.
Future plans
I'd like to expand this system to not only detect a wider variety of ad filtering software, but to detect a all sorts of issues with loading ads for the end user.