Detecting If Adblocker Extensions Are Blocking Ads

javascript
Published on December 20, 2020

A possible way of detecting adblockers is to listen to the onerror event of the ad serving <script> element. If the script fails to load, probably it has been blocked by an adblocker.

Lots of users use adblocker extensions to disallow ads showing up on their end (~ 20% for usefulangle.com). This causes loss of revenue to sites that offer free content.

A possible way to make up for this loss would be to detect if ad serving scripts (Adsense, Carbon etc) are being blocked. If they are blocked, sites can possibly insert HTML to show other monetizing content (for eg. affiliate links).

Ads are shown in the website through a <script> that is included in the page. What adblockers do is that they block the <script> from loading.

However to detect this blocking we can use the onerror event of the script element. If the adblocker blocks the ad script, this event would be fired, and we would come to know that ads won't be showing up.

This is a technique not to detect adblockers, bur rather detecting if the ad script has failed to load. However ad scripts rarely fail to load, unless there is an adblocker working.

Example - Adsense

Normal Adsense script markup is :

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

To detect if the script loading fails :

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" onerror="adLoadError()"></script>
<script>
function adLoadError() {
	// ad script has failed to load
}
</script>

The above technique has been tested on AdBlocker Ultimate & Sigma Adblock extensions.

In this Tutorial