2

AdSense script results in three things:

  1. An ad.
  2. A blank space the size of a ad.
  3. No ad or blank space added.

AdSense used to have a setting to allow you to show a backup ad in case of #2 and #3, but this has been removed.

I have an ad placed in a division below the main content of each page. I placed the following script just above the <body> tag to place whatever Adsense being served to the division in a alert box so as to examine it.

alert(document.getElementById(&quot;adsenseads&quot;).innerHTML);

I was hoping that there would be someting different added to the division by Google so I could detect #2 or #3 and rewrite the innerHTML of the division with an ad form another network. I don't really see any pattern one could work with. Even though the division is not showing any content or a blank space the size of an ad Google is still delivering the same type of content to the division.

This unacceptable as I can't have a unsitely blank space or waste the space for unrendered ads. Adsense provides no help unless you have huge traffic. Google does not even bother to update its own Help files as they are still talking about the option of a backup ad in your Adsense settings.

Loaded Ad:

<ins class="adsbygoogle" data-ad-client="ca-pub-XXXXXXXXXXXXXXX" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="XXXXXXXXXX" style="display: block; text-align: center; height: 200px;" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><ins id="aswift_0_anchor" style="display:block;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><iframe width="1000" height="200" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;border:0px;width:1000px;height:200px;"></iframe></ins></ins></ins>
<script>
                  (adsbygoogle = window.adsbygoogle || []).push({});
                  </script>


Not Loaded

<ins class="adsbygoogle" data-ad-client="ca-pub-XXXXXXXXXXXXXXX" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="XXXXXXXXXX" style="display: block; text-align: center; height: 200px;" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><ins id="aswift_0_anchor" style="display:block;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><iframe marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;border:0px;width:1000px;height:200px;" width="1000" height="200" frameborder="0"></iframe></ins></ins></ins>
<script>
                  (adsbygoogle = window.adsbygoogle || []).push({});
                  </script>


Not Loaded

<ins class="adsbygoogle" data-ad-client="ca-pub-XXXXXXXXXXXXXXX" data-ad-format="fluid" data-ad-layout="in-article" data-ad-slot="XXXXXXXXXX" style="display: block; text-align: center; height: 200px;" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><ins id="aswift_0_anchor" style="display:block;border:none;height:200px;margin:0;padding:0;position:relative;visibility:visible;width:1000px;background-color:transparent;"><iframe marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;border:0px;width:1000px;height:200px;" width="1000" height="200" frameborder="0"></iframe></ins></ins></ins>
<script>
                  (adsbygoogle = window.adsbygoogle || []).push({});
                  </script>

--- ADDITIONAL INFORMATION & REPLY ---

I added a extensive response directly to the reply of Stephen Ostermiller, but that was removed so here we go again.

Thank you for your reply Stephen!

Firstly, the provided code will not be accepted in Blogger's XHTML editor unless its first placed between CDATA brackets. without these brackets it will flag the ==, > and some other content. My site is hosted on Blogger buts uses a custom Google .com domain.

<script type='text/javascript'>
<![CDATA[

// Run when the DOM content is loaded
document.addEventListener('DOMContentLoaded',function(){
    // AdSense ads lazy load, so they can't be examined right away
    setTimeout(function(){
        // Search the DOM for ad slots assuming they all have <div class=adsenseads> around them
        var x=document.getElementsByClassName("adsenseads")
        for(var i=0;i<x.length;i++){
            // See if adsense loaded an ad by checking for an iframe
            if(x[i].innerHTML.search(/iframe/i)==-1){
                // No ad loaded         
                x[i].innerHTML="BACKUP AD CONTENT HERE";
            }
        }
    },3000); // 3000ms or 3 seconds
});

]]>
</script>

I don't think the above code will work because it is checking for the length. As previously disclosed Adsense is delivering the same content to the division regardless of whether a ad is shown, a blank space is shown or nothing is shown.

If you add this script after the previously script it always returns content from Adsense regardless of whether a ad is shown, a blank space is shown or nothing is shown. There is no difference in the content for each results so one can't simply search the content for a difference in order to a serve a backup ad as a result.

<script>
alert(document.getElementById(&quot;adsenseads&quot;).innerHTML);
</script>

I believe this is Google's attempt to prevent us from using backup ads at all. Google removed the ability to use backup ads directly within Adsense accounts. By serving the same base code for each state of the ad Google is preventing us from differentiating an actual ad from a blank space or a case where nothing is rendered - thus preventing you from offering a backup ad. It appears when the base code delivered by Adsense communicates to Google's servers a decision is then made to actually render an ad or not.

If Gooogle would instead deliver some type of standard alternative code for ads they have no intent on filling, such as a special tag like <noadd/>, it could be checked and a backup ad delivered if it was present. But Google does not do this thus it would seem Google also does not want us to use backup ads at all from our end.

This is unacceptable as large blank spaces make my site look bad. It also means the space reserved for ads will be unused in some cases and thus lower my income. Unfortunately I don't think Google really cares at all as their own help system still refers to the defunct method of offering backup ads via Adsense.

1 Answer 1

1

The reason that your code is not detecting the ads is because you haven't given them time to load. AdSense ads lazy load after much of the rest of the page has loaded. To be able to check for them, you have to defer evaluation of your script.

I implemented this for one of my sites. I put backup ads into place if AdSense didn't write an ad in 3 seconds. (There may be a more elegant way to check if AdSense is done running, but I don't know it.) Here is the code:

// Run when the DOM content is loaded
document.addEventListener('DOMContentLoaded',function(){
    // AdSense ads lazy load, so they can't be examined right away
    setTimeout(function(){
        // Search the DOM for ad slots assuming they all have <div class=adsenseads> around them
        var x=document.getElementsByClassName("adsenseads")
        for(var i=0;i<x.length;i++){
            // See if adsense loaded an ad by checking for an iframe
            if(x[i].innerHTML.search(/iframe/i)==-1){
                // No ad loaded         
                x[i].innerHTML="BACKUP AD CONTENT HERE";
            }
        }
    },3000); // 3000ms or 3 seconds
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.