Forum Moderators: phranque

Message Too Old, No Replies

More fun with GAM, removing parent styles when ad isn't filled

         

csdude55

5:42 am on Jul 22, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I currently have something like this:

<style>
.ad_block {
display: block;
padding: 20px 0;
background: #F5F7F9
}
</style>

<script>
googletag.cmd.push(function() {
googletag.defineSlot('/12345/foo', ['fluid'], 'ad_block')
.addService(googletag.pubads());

googletag.pubads().enableSingleRequest();
googletag.enableServices();
googletag.display('ad_block');
});
</script>


This works fine, but when there's no ad available then I end up with a 40px blank spot. Using collapseEmptyDivs() doesn't help either, I guess because the style is on the parent container?

Based on this:
[developers.google.com...]

I discovered that I could do this:

<script>
googletag.cmd.push(function() {
googletag.defineSlot('/12345/foo', ['fluid'], 'ad_block')
.addService(googletag.pubads());

googletag.pubads().enableSingleRequest();

// this is the new part
if (googletag.pubads().collapseEmptyDivs())
console.log('collapsed!');

googletag.enableServices();
googletag.display('ad_block');
});
</script>


and I do see "collapsed!" in the console!

But when I change that to document.getElementById('ad_block').style.display = 'none';, nothing is changed. On a 1am hunch I also tried = 'none !important';, but that didn't work either.

Any other thoughts or suggestions?