最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I detect if my AdSense ad is blocked? - Stack Overflow

programmeradmin1浏览0评论

If user has some kind of ad blocker installed, ad blocker will of course remove all ads from my website, and it leaves empty spaces where the ads used to be. I would like to use that empty space by putting some other content in it, like links to most important pages of my website, to do that I need to detect if AdSense javascript is loaded.

Methods that I have tried so far:

if (!document.getElementById("google_ads_frame1"))
{
}

and:

if (typeof(window.google_render_ad) == "undefined")
{
}

Both of those seem to fail in certain situation, for example if browser downloads AdSense javascript files a bit slower, it will execute above mentioned code before AdSense code is loaded and I end up hiding ads for users that don't even have ads blocked.

Do you have any suggestions on how could I make sure that my code is run after AdSense? Or some other way of detecting that AdSense scripts are not loaded?

If user has some kind of ad blocker installed, ad blocker will of course remove all ads from my website, and it leaves empty spaces where the ads used to be. I would like to use that empty space by putting some other content in it, like links to most important pages of my website, to do that I need to detect if AdSense javascript is loaded.

Methods that I have tried so far:

if (!document.getElementById("google_ads_frame1"))
{
}

and:

if (typeof(window.google_render_ad) == "undefined")
{
}

Both of those seem to fail in certain situation, for example if browser downloads AdSense javascript files a bit slower, it will execute above mentioned code before AdSense code is loaded and I end up hiding ads for users that don't even have ads blocked.

Do you have any suggestions on how could I make sure that my code is run after AdSense? Or some other way of detecting that AdSense scripts are not loaded?

Share Improve this question asked Feb 19, 2011 at 20:31 Igor JerosimićIgor Jerosimić 13.7k7 gold badges46 silver badges54 bronze badges 1
  • Related: stackoverflow.com/questions/4869154/… – Jon Schneider Commented May 12, 2017 at 15:40
Add a comment  | 

6 Answers 6

Reset to default 9

If using the new AdSense code, you can do an easy check, without resorting to content or css checks.

Place your ads as normal in your markup:

<ins class="adsbygoogle" style="display: block;"
   data-ad-client="ca-pub-######"
   data-ad-slot="#######"
   data-ad-format="auto"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

Then you call the adsense code at the bottom of your page (note do not use the "async" flag when calling the adsbygoogle.js script):

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

Then add this little snippet of code below that:

<script>
if (!adsbygoogle.loaded) {
   // do something to alert the user
}
</script>

AdSense always creates/sets the flag adsbygoogle.loaded to true when the ads are loaded. You could place the check in a setTimeout function to delay the check by a few seconds.

This ultra-lightweight method seems to be infallible and is agnostic to all ad networks.

Place a file call ads.js in your root folder and add this line inside it:

var can_run_ads = true;

Adblock won't allow a file named ads.js to be loaded. So, just place this code before </body>

<script src="/ads.js"></script>
<script>
if(window.can_run_ads === undefined) {
    alert('Please Disable Adblock, ****** ******!');
}
</script>';

I strongly recommend to redirect the user to a page that has instructions on how to remove Adblock.

Run this code on the window.onload event. window.onload event is fired when the page has completed loading.

window.onload = function() {
  // your checks
}

If you're using jQuery, use

$(window).load(function() {
  // your checks
});

I suggest you use FuckAdBlock to detect if AdBlock is activated. If this is the case, you are free to add as many html in your page you want.

See the documentation for examples: https://github.com/sitexw/FuckAdBlock

Detect data-ad-status="unfilled" parameter in ins.adsbygoogle to know if the adslot is empty.

After some more tests I realized that issue was not about timing as I previously thought. Problem was that AdSense started to use some new code, this code doesn't have "google_render_ad" function and it doesn't create iframe with ID "google_ads_frame1" (new ID is "aswift_0"). Considering that AdSense now uses at least two different IDs for iframes I made new detection code that checks just for presence of iframe element regardless of its ID:

if (document.getElementsByTagName("iframe").length === 0)
{
    // ad is blocked
}
发布评论

评论列表(0)

  1. 暂无评论