How to Detect Ad blocker using JavaScript

Advertisements are very important for any post published because this will decide the revenue of the publisher. Most of blogs out there use some kind of revenue scheme to pay for server expenses and compensate on the hard work put on them. But now the ad blocker system is killing revenue of the publisher . I notice 30% of my readers using adblocke softwares. Today I will talk about How to Detect  Ad blocker  using JavaScript.

You cannot block users addblock yet, but you can detect it and then can do something. There are many ways which can be used once you detect someone using an Adblock software such as:

  1. Show them a friendly message that your blog is sustained by advertisement and hope they white list you.
  2. Show them other replacement advertisement.
  3. Disable your blog until they disable it (I wouldn’t recommend it).

 

How to detect Ad Blocker using JavaScript

Google Adsense

Here you have to add onerror event  into your script tag, this will trigger ‘adBlockDetected()’ JavaScript function if user has any addblock software:


<script async onerror="adBlockDetected()" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Homepage Leaderboard -->
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1234567890123456"
data-ad-slot="1234567890"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

‘adBlockDetected()’ function

This function will contains very simple JavaScript or jQuery code  which will simply replace content of blog:


<script>
function adBlockDetected()
{
//Website Users
document.getElementById('blogContentDiv').innerHTML = 'Please disable your ad blocker';
//Or may be you can hide show divs
}
</script>

You can also add these count into Google Analytics using following code:


<script>
function adBlockDetected()
{
//Google Analytics Tracking
setTimeout(function() {
ga('send', 'event', 'AdBlocker', 'click','AdBlocker');
},1000);

//Website Users
document.getElementById('blogContentDiv').innerHTML = 'Please disable your ad blocker';

//Or may be you can hide show divs

}
</script>

Please share with friends if you like information.

 

One Comment

  1. Asif November 2, 2017 Reply

Add a Comment

Your email address will not be published. Required fields are marked *