7

I am using google adsense responsive ads. Sometimes adsense don't find ant ad and leave blank space. Is there a way, using a javascript to determine if an adsense block is empty? and than i will hide the entire adsense container.

Here is the code i've been using for the adsense:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- Sidebar skyscrapper -->
                        <ins class="adsbygoogle"
                            style="display: block; width: 300px; height: 600px"
                            data-ad-client="ca-pub-xxxxxx"
                            data-ad-slot="xxxxxx"
                            data-ad-format="auto"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                    <!-- Sidebar skyscrapper -->
                    <ins class="adsbygoogle"
                        style="display: block; width: 300px; height: 600px"
                        data-ad-client="ca-pub-xxxxxx"
                        data-ad-slot="xxxxxx"
                        data-ad-format="auto"></ins>
                    <script>
                        (adsbygoogle = window.adsbygoogle || []).push({});
                    </script>
user1610208
  • 237
  • 1
  • 3
  • 13
  • I'd post this as an answer but since I haven't used adsense with the web, QUOTE, "AdSense always creates/sets the flag adsbygoogle.loaded to true when the ads are loaded," http://stackoverflow.com/a/29321408/4012266 – CmosBattery Sep 05 '15 at 18:43
  • Cant use this as i cant use the "async" flag. Also, i have a lots of adsense blocks on the webpage and i need to know which one has an empty ad. – user1610208 Sep 05 '15 at 19:27
  • You wrote: "i have a lots of adsense blocks" - you can't have more than three adsense blocks per page. – Anton Dec 03 '15 at 19:26
  • Related: http://stackoverflow.com/q/5053317/12484 – Jon Schneider May 12 '17 at 15:41

1 Answers1

2

Google actually has information on how to handle this - https://support.google.com/adsense/answer/10762946. There is an attribute in the ins advert element called data-ad-status which tells you if the advert is "filled" or "unfilled". They suggest using css to hide unfilled ads:

<style>
ins.adsbygoogle[data-ad-status="unfilled"] {
   display: none !important;
}
</style>

And if their ad fails to find anything for your page, you can also replace it with your own image wrwapped in a link like so:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-1234567890"
     data-ad-slot="1234567890">
    <a href="/page"><img src="/backup.jpg" width="300px" height="250px"></a>
</ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<style>
ins.adsbygoogle a {
    display: none !important;
}
ins.adsbygoogle[data-ad-status="unfilled"] a {
    display: block;
}
</style>
mulllhausen
  • 4,069
  • 7
  • 45
  • 69
  • 1
    This will absolutely destroy your CLS, an important ranking factor. What will happen is that you will get inline style with width and height generated by google, so your layout will shift for the first time, then you will have it gone once more, then your layout will shift again. Tried this, it's terrible. The unfilled value of the google generated attribute is one of the last attributes of that element. The inline style attribute comes first. Disaster CLS-wise. – Catalin Mar 11 '22 at 09:20