We provide the code snippets and content on this page without any guarantee or support. In most cases, these pages are created on requests or based on individual solutions sent to us by other users. After it was published, we are only reviewing the code and content when we are made aware of an issue. Please let us know if you find one.
Please only use the codes if you understand them and know how to add custom code to your WordPress site.
This page contains examples of custom code that allows you to show an alternative ad when a Google AdSense ad leaves white space or an ad server like Google Ad Manager has blank or unfilled output.
AdSense removed the fallback ad feature in their account so the method below is the only one to somehow display a fallback ad for unfilled AdSense Space.
Google Ad Manager does normally not need a fallback ad set up on the site that runs the ad code since publishers can enable them through their account. The example for an Ad Manager backfill below might still be useful to those who have no control over the Google Ad Manager account or use other ad networks.
If you manage your ads locally and not through an ad network then you should rather use the fallback ad setup Advanced Ads provides with ordered groups.
The basic principle behind the solution is to check after a short period if the ad space is collapsed and in that case deliver another ad code.
When using Advanced Ads, you need to manage the code using the “Plain Text and Code” ad type and not the dedicated AdSense ad type or our Google Ad Manager integration. It also works well with features like cache busting and conditions.
General setup
The basic idea we are following here:
- we wrap the ad in a container, e.g.,
adsense-responsive-ad-container
in the first example below - wait x milliseconds (e.g.,
2000
) and then check if the container is just0
pixels high - in that case, we show the alternative ad
Limitations:
- the alternative ad might need to be rewritten, especially, if it uses
document.write
. You can find examples on how to rewrite it here. - this will work with the AdSense ad type for display ads only.
Alternative methods:
- instead of checking for the height, one can do any other kind of checks and action. E.g., you could check if a specific element exists in the DOM and remove it when the ad container is empty.
Fallback ad for AdSense
The example below does only work when AdSense collapses unfilled ad units on your website. This does not always seem to be the case, especially for new AdSense accounts.
How to test this
- get a responsive ad unit from your AdSense account
- change the ad unit ID or the publisher account ID in the code so that AdSense won’t find the account and not return any ad content
How the code looks
<div class="adsense-responsive-ad-container">
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-123123123" data-ad-slot="321321" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<script>
setTimeout(function(){
// get the ad container ID
var ad_container = document.querySelector('.adsense-responsive-ad-container');
if( 0 == ad_container.offsetHeight ) {
// show fallback ad code
ad_container.innerHTML = '<img src="https://wpadvancedads.com/wp-content/uploads/2015/08/300x250.png" width="300" height="250" alt="icon"/>';
}
// check the height
}, 2000);
</script>
Code language: HTML, XML (xml)
Fallback ad for Google Ad Manager
How the code works
2 seconds after loading the page, collapsed space should show the static image defined in the fallback code section since the Google Ad Manager ad unit code I used here does not deliver any ads and collapses due to that.
To test it, just reload this page and wait 2 seconds.
How the code looks
In contrast to AdSense, Google Ad Manager doesn’t load anything if the details of the ad unit are wrong so make sure to replace them with your own.
The code below is a so-called “inline tag” and uses setCollapseEmptyDiv(true)
to collapse the ad space if the Ad Manager cannot fill it. Tags and options like this are created automatically by our Google Ad Manager WordPress Integration.
<div class="gam-responsive-ad-container">
<script async="async" src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script> var googletag = googletag || {}; googletag.cmd = googletag.cmd || [];</script>
<div id="gpt-ad-987654321-0">
<script>
googletag.cmd.push(function() {
googletag.defineSlot( '/123456789/Medium_Rectangle', [300,250], 'gpt-ad-987654321-0' )
.addService(googletag.pubads()).setCollapseEmptyDiv(true);
googletag.enableServices();
googletag.display( 'gpt-ad-987654321-0' );
});
</script>
</div>
</div>
<script>
setTimeout(function(){
// get the ad container ID
var ad_container = document.querySelector('.gam-responsive-ad-container');
if( 0 == ad_container.offsetHeight ) {
// show fallback ad code
ad_container.innerHTML = '<img src="https://wpadvancedads.com/wp-content/uploads/2015/08/300x250.png" width="300" height="250" alt="icon"/>';
}
// check the height
}, 2000);
</script>
Code language: HTML, XML (xml)