An Efficient Ad Blocker Detector Javascript Code For Google AdSense

This article would be focusing mainly on detecting the Google AdSense Ads being blocked by Ad blockers.

If you came into this website with your ad blocker on, you should have got the notification asking to disable the ad blocker. Yes, I am gonna share the same technique that I've made for this website.


What's an Ad blocker?

My way of explaining is, a tool which burns a big hole in your pocket, taking away your advertisement revenue by disabling all the Ads shown on your website.

That would be the accurate explanation for a blogger who monetize their blogs with Advertisement.

Why people block Ads?

Here comes the real question, are you yourself an Ad block user? Most probably yes, even though we know each and every website we visit may contain advertisement, solely meant for the website owner whose hard work put behind the website and it's content. Then as a blogger yourself, why do you use an Ad blocker, after all knowing the pain in getting your advertisements blocked?

2 Different Types of Advertisements

I like my own categorizing :P.

1)Gentle Man Advertisements

Advertisements, which acts like polite gentle men. Those are the Ads, which just sit around where the developers have placed them. They're so unobstrusive, sometimes we won't even know if there are ads placed on the page. Only shows them off, when the user scrolls to them.

An example of those advertisements would be the Ads provided by Google AdSense itself.

2)Reckless Advertisements

These are the advertisements, every single internet user on this planet hate about. They're so intrusive that, the user wouldn't even get a chance to read the article or page on which these types of Ads are placed on. So, gradually or even on-the-spot they would close the tab and seek for some other website.

So, by now you would have got the fair knowledge of "Why people block Advertisements?", they are obviously because of the so called Reckless or Intrusive Advertisements.

But, hold on for a second. Ad blockers won't categorize them, if they were, there wouldn't be a need of this post. They just block 'em all.

So, how do we detect those Ad blockers and politely ask the users to white list the website, especially in-case of Google AdSense.

Detecting Ad blockers

We all AdSense publishers know each and every Ad snippet contains a script tag like shown below.

<script async="true" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"/>

The above script is what the Ad blockers block them before execution by filtering the URL present in the src attribute.

If you know the basics of Javascript, you would know every elements like img, svg, script, would fire up an error when the URL in the src attribute can't be fetched or in a non standard way. We could pretty much catch those errors by using the onerror attribute.

So just add an onerror attribute to the above script tag,

<script async="true" onerror="alert('Hey Adblocker, you suck ;');" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"/>

Replace your Ad script with the above script and turn the ad blocker on your page, now you would see an alert message, which is being executed at the time of the URL being blocked by the Ad blocker.

Now there, you could write any function, which just shows an alert box asking user to white list you, each and every time they visit your page, or a simple script which shows a message on the top asking to disable the ad blocker, there are multitudes of possibilities.

That's all for now, hope you got an idea on how to detect Ad blockers.

In an upcoming article, I'll share the display code, when an Ad blocker is detected, just like as shown in this website.
Share on Google Plus

About ShihabSoft

I am Shihab, people used to call me a Security researcher or sometimes a Reverse engineer or sometimes a creepy hacker ;). Got a brain full of knowledge in different programming languages, and not more nor less I am a blogger too :).
    Blogger Comment
    Facebook Comment

9 comments:-

  1. The idea is awesome and ofcourse, it is a main matter which affects bloggers. Now Ad Blocker sucks. Thank You for the great article Shihab. Expecting more content worthy articles from you.

    Regards,
    Murshid VH

    ReplyDelete
    Replies
    1. Hey Mufeed, glad that you've got something informative :)

      Delete
  2. Cn u tell the theme name of ur blogger site ?

    ReplyDelete
  3. Notification required aaraha ha

    ReplyDelete
  4. Notification required aaraha ha

    ReplyDelete

Don't be shy. Just leave your comments, am so happy to assist you. And, don't forgot that I am such a nice person who you are dealing with :)