No matter how you feel about the ethics of ad blocking, as a publisher, it’s definitely useful to know how many of your users have an ad blocker installed. Below is a piece of javascript that does exactly that.

How does ad blocking work?

There are basically two techniques used by ad blockers

Blocking Specific HTML

A simple technique is to look for anything in the HTML that is labeled like an ad. For example, there might be a block of code like this:

<div class="ad ad-300x250">
...
</div>

To an ad blocker, this is very clearly an ad. They can remove it from the code altogether, or simply hide it from view. Some ad blockers go even further and block all file names that include common ad sizes. So for example, if you uploaded an image to your website named “my-puppy-300×250.jpg”, it could be blocked, even though it’s not an ad!

Blocking Specific Domains

Another common form of ad blocking is to prevent connecting to any domain that is associated with ad serving. You can see one of the default lists here: AdBlockPlus EasyList. This approach can be very effective. By blacklisting those domains, they will never even get a chance to serve an ad.

Ad Blocker Detection

Below is a script to detect the use of an ad blocker and push that data into Google Analytics as an Event. This is a simple implementation, but there are more sophisticated scripts out there too. Finally, note that some ad blockers even block Google Analytics – so this technique won’t be able to detect that particular case.

In fact, there is an entire industry surrounding ad blocking, detecting ad blockers and even blocking ad blockers. For a simple drop-in script that works with Google Analytics, you can try something like this:

<script>
var MeasureAdblock = (function() {
	var init = function() {
		document.write('<div id="adsense" style="visibility:hidden;height:0px;">adsense ad</div>');
		var detect = function() {
			setTimeout(function() {
				var detected = 'No';
				var elt = document.getElementById('adsense');
				if( ! elt || elt.innerHTML.length === 0 || elt.clientHeight === 0 ) {
					detected = 'Yes';
				}
				elt.style.display = 'none';
				if(typeof ga === "function") {
					ga('send', 'event', 'AdBlock', detected);
				}
				else if(typeof _gaq != "undefined") {
					_gaq.push(['_trackEvent', 'AdBlock', detected]);
				}
			}, 2000);
		};
		if(window.addEventListener) {
		    window.addEventListener('load', detect, false);
		} else {
		    window.attachEvent('onload', detect);
		}
	};
	return {
		init: init
	};
})();
MeasureAdblock.init();
</script>

Drop Us a Line

We’re dedicated to working with the hungriest minds in our field. If you’re involved with digital content production or digital advertising and marketing, we’d love to hear from you. Big, small, experienced or beginner, please feel free to get in contact if you think we can be of service.

Say Hello