Detect AdBlock with JavaScript

D
Published in: Javascript

If you would like to know whether a visitor on your website is blocking Google AdSense and other online advertising networks or not, you can easily do that with the help of some JavaScript. Here are some approaches:

  1. You can check for the existence of window.google_jobrunner after the page has finished loading. We are using setTimeout to take care of asynchronous Google AdSense that may not load immediately.
<script>
  window.onload = function() {
    setTimeout(function() {
      if ( typeof(window.google_jobrunner) === "undefined" ) {
        console.log("ad blocker installed");
      } else {
        console.log("no ad blocking found.");
      }
    }, 10000);
  };
</script>
  1. The other more popular approach is that you create a file called /ads.js in your server and inside that file, set a variable as false. AdBlockers routinely block JavaScript files that have .ads in the name and hence, the variable will not be set if the ad blocker is active.
// Put this in the ads.js file
isAdBlockActive=false;

Now put this somewhere inside the HTML of your main web page.

<script>var isAdBlockActive=true;</script>
<script src="ads.js"></script>
<script>
if (isAdBlockActive) {
  console.log("The visitor is blocking ads");
}
</script>
  1. Here’s another option that works with the new Asynchronous Responsive Google Ads.
window.onload = function () {
  setTimeout(function () {
    var ad = document.querySelector("ins.adsbygoogle");
    if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
      ad.style.cssText = "display:block !important";
      ad.innerHTML = "You seem to blocking Google AdSense ads in your browser.";
    }
  }, 2000);
};

In the new format, the ads are inserted using the INS tag. The snippet checks for the length of the tags that are contained inside the INS tag. If it is 0, Google Ads were blocked and the user is shown a custom message.

We also need to set the CSS display property as block as AdBlock may be blocking ads with the adsbygoogle class by simply hiding them on the screen with CSS.

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Javascript

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube