I am attempting to modify code found in the post below.
How can I detect if my AdSense ad is blocked?
I want to detect when a Adsense ad is not loaded and show a backup ad from another network. Previously Adsense allowed you set an alternative backup URL for each ad but this was removed by Google.
I am placing Adsense ads below the main content of each post. The main content of each post is represented by the <data:post.body/>
tag in Blogger's XHTML editor. The <b:if cond='data:view.isPost'>
& </b:if>
tags check to make sure the ad only appears on a actual post page. There are two instances of this code in Blogger's XHTML template.
<div class='adsense-ads' id='adsenseads'>
<!--<script async='async' src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'/>-->
<ins class='adsbygoogle' data-ad-client='ca-pub-XXXXXXXXXXXXXXXX' data-ad-format='fluid' data-ad-layout='in-article' data-ad-slot='XXXXXXXXXX' style='display:block; text-align:center;'/>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</b:if>
Above the tag I've added the code below. This code checks if the AdSense code was added and if not changes the innerHTML of the the ad's outer division to a instead show a tag from another ad network. This tag is a placeholder where the other network inserts a ad after the rest of the page has loaded.
This code is showing the second network's ads on all posts instead of just on pages on which Adsense ads do not show. If the remove the else code Adsense will show on most pages but not all. I tried to add a timer to delay this code as discussed in the referenced thread but then no ads would show at all from either network.
<script src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'/>
<script>
if (!adsbygoogle.loaded) {
//Ad Loaded
} else {
document.getElementById("adsenseads").innerHTML = "<input name='IL_IN_ARTICLE' type='hidden'/>";
}
</script>
ADDITIONAL INFORMATION
I added this script above the closing body tag to check exactly what content Adsense's script was delivering to an outer division used for ads.
Adsense is delivering an ad; a blank space the size of an ad; or no content. In all such cases the innerHTMl contains similar content.
If Adsense was not intending on showing an ad it should instead being delivering no content or a special tag so the innerHTML could be checked so as to deliver a backup ad. The fact that the content is same suggests Google wants to prevent publishers from delivering backup ads at all.
Googgle's motto appears to have changed from Do No Evil to Do All Evil.
<script>
alert(document.getElementById("adsenseads").innerHTML);
</script>
Ad Created:
<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&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!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>
Blank Space / No Content:
<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&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!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>