DEV Community

Cover image for Detecting Blocked Google Ads with Simple and Effective JavaScript
Trieu.iv
Trieu.iv

Posted on

Detecting Blocked Google Ads with Simple and Effective JavaScript

I've experimented with various solutions, but some older methods have become ineffective due to the proliferation of browsers and ad blockers.

Previously, to check if an ad (Google Ads) was blocked, most AdBlocks targeted specific CSS classes (like class="ad-box") and applied the "display: none" property to the DOM with that class.

This would make the ad disappear from your browser. However, many of these older solutions no longer work and are not included in the solutions above. Therefore, we will focus on more suitable solutions.

By default, there is no common API to determine if an ad is blocked. There are some tricks to check if AdBlock is being used, such as checking if the ad script is loaded correctly.

This solution works for all ad blockers, including the default blockers for EDGE, Brave, ad blocking apps like AdGuard, and DNS blockers.

The check will be performed after all website components have loaded, so it will not affect website performance or Core Web Vitals (CWV).

Always test carefully to ensure you don't affect the experience of users who don't use AdBlock.

Image description

How does it work?

It's quite simple. The code above will be executed when the entire website has finished loading. Its task is to check if the adsbygoogle.js file (specifically this path: https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js) is blocked ((failed) net::ERR_UNSAFE_REDIRECT).

Open Developer Tools and select the Console and Network tabs to check.

Image description

Image description

Good luck!

Ad Blocking: Challenges and Solutions for Website Owners

Top comments (1)

Collapse
 
mattycoderx profile image
Mattycoderx

Google ads is an effective way of advertising, but you need to have knowledge about it to manage the campaign well. I recommend this post on this topic: gamerseo.com/blog/what-is-conversi...