Most effective way to detect ad blockers

Completely free & easy to implement

You're blocking ads
You're allowing ads

Provided by Adblock Analytics

When we launched Adblock Analytics the feedback we received was extremely interesting, but a common theme developed and that was "cost". Our service wasn't free and a lot of websites couldn't afford to sign up after their free trial ended. So we've decided to explain how we detect ad blockers and provide a few examples of what you can do with that information.

The reason we're doing this is because we'd rather help everyone than only those who can afford it. Also, we know that the majority of larger websites prefer to use third party solutions over developing their own and we hope that they'll consider Adblock Analytics.

Step 1 - Understand how ad blockers work

When a browser requests a website address it downloads the corresponding HTML file which includes internal & external references to Javascript files, CSS stylesheets and images. If the browser has an ad blocker installed, it will compare the names of referenced scripts & files against a "block list" and if there are any matches those files will be ignored.

View popular block list - easylist.txt  

Step 2 - Leverage the "block list" to reveal the ad blocker

All block lists include a reference to "ads.js" because it's a common name for JavaScript files that are associated with serving ads. Knowing this, save the following JavaScript code which creates a hidden div to a file called "ads.js" and place it in the root directory of your website.

var e=document.createElement('div');
e.id='iJRFGyOhczlL';
e.style.display='none';
document.body.appendChild(e);

Note: the obscure div id is to ensure that it doesn't conflict with any other div's that your website might use.

View our ads.js file  

Step 3 - Check if "ads.js" was loaded or blocked

Place the following JavaScript within your website's HTML source code just above the </body> tag. It's purpose is to check if the hidden div created within "ads.js" exists (ads are allowed) or not (ads are blocked). And that's all there is to it!

<script src="/ads.js" type="text/javascript"></script>
<script type="text/javascript">

if(document.getElementById('iJRFGyOhczlL')){
  alert('Blocking Ads: No');
} else {
  alert('Blocking Ads: Yes');
}

</script>

Example 1 - Send ad blocking data to Google Analytics

You can easily track your website's ad blocking activity within Google Analytics via the following script which supports all versions. Be aware that there's a data limit if you're using the free version.

<script src="/ads.js" type="text/javascript"></script>
<script type="text/javascript">

if(document.getElementById('iJRFGyOhczlL')){
  iJRFGyOhczlL='No';
} else {
  iJRFGyOhczlL='Yes';
}

if(typeof ga !=='undefined'){
  ga('send','event','Blocking Ads',iJRFGyOhczlL,{'nonInteraction':1});
} else if(typeof _gaq !=='undefined'){
  _gaq.push(['_trackEvent','Blocking Ads',iJRFGyOhczlL,undefined,undefined,true]);
}

</script>

Example 2 - Post ad blocking data to an external script

Using this method you can record your website's ad blocking statistics to your own database, but to save yourself a lot of development you'd be better off just using Adblock Analytics.

<script src="/ads.js" type="text/javascript"></script>
<script type="text/javascript">

if(document.getElementById('iJRFGyOhczlL')){
  iJRFGyOhczlL='No';
} else {
  iJRFGyOhczlL='Yes';
}

var r=new XMLHttpRequest();
r.open('POST','https://www.domain.com/script/');
r.setRequestHeader('Content-type','application/x-www-form-urlencoded');
r.send('blockingAds='+iJRFGyOhczlL);

</script>

Example 3 - Display a message on your website

This is a little controversial, but if you're finding that the majority of your website's visitors are blocking ads then you might want to try displaying a friendly message asking them to disable it.

<style>
#dCUaKsBHjLbQ {
display: none;
margin-bottom: 30px;
padding: 20px 10px;
background: #D30000;
text-align: center;
font-weight: bold;
color: #fff;
border-radius: 5px;
}
</style>

<div id="dCUaKsBHjLbQ">
  Our website is made possible by displaying online advertisements to our visitors.<br>
  Please consider supporting us by disabling your ad blocker.
</div>

<script src="/ads.js" type="text/javascript"></script>
<script type="text/javascript">

if(!document.getElementById('iJRFGyOhczlL')){
  document.getElementById('dCUaKsBHjLbQ').style.display='block';
}

</script>

This is what the message looks like:

Our website is made possible by displaying online advertisements to our visitors.
Please consider supporting us by disabling your ad blocker.