Detect Adblockers

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today I needed a way to detect an adblocker. You might think why? In this case, I wanted to verify some user data with an external system, but adblockers will block this call (mainly ghostery).
So I tried and research ways to detect adblockers!
When doing my research, I came across several ways of which some in theory work, but not for all browsers/adblockers. Let me walk you through the options we have.
This method, I only found late in the game and is the solution I went for. I like the simplicity, and it worked for by far the most combinations I tried!
So in the HTML we add the following:
<!-- Fake js script to inject, adblockers will block this script load -->
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
onerror="adBlockFunction();"
></script>
The idea is this script will be blocked by adblockers and if it does it will run the adBlockFunction.
So the function:
var adblocker = document.getElementById('adblock-message');
adblocker.style.display = 'block';
Then we can have a simple adblocker div which is hidden by default.
<div id="adblock-message" class="hidden">Sorry, you have your adblocker on!</div>
As mentioned, this way works for most combination of browsers/adblockers.
Another way, you'll come across quite a lot if defining a JavaScript file like this:
<script src="/js/ads.js"></script>
inside:
var canRunAds = true;
And then have a JavaScript as such:
if (window.canRunAds === undefined) {
var adblocker = document.getElementById('adblock-message');
adblocker.style.display = 'block';
}
This is almost the same as solution one, but I found that it doesn't work with Adblock in the latest Chrome.
Another way, is by using a fixed "ad" in your html:
<div
id="detect"
class="ads ad adsbox doubleclick ad-placement carbon-ads"
style="background-color:red;height:300px;width:300px;position: absolute;left:0;top:0;"
>
</div>
This should be blocked by adblockers so if we then go and check for the height, it shouldn't work:
var testAd = document.getElementById('detect');
window.setTimeout(function() {
if (testAd.offsetHeight === 0) {
var adblocker = document.getElementById('adblock-message');
adblocker.style.display = 'block';
}
testAd.remove();
}, 100);
As mentioned very cool solution, but not rock solid!
Try them all on Codepen.
Let me know if you know of any other ways, without using any plugin!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter