collapse unfulfilled ads by adding a class to the ad unit and collapsing the container with css :has

This commit is contained in:
hobbes7878 2024-09-24 05:54:59 +01:00
parent 349f2194da
commit 52eab435e7
3 changed files with 20 additions and 1 deletions

View file

@ -53,3 +53,9 @@
</script>
<div data-freestar-ad="{dataFreestarAd || null}" id="{adId}"></div>
<style>
:global(div.freestar-adslot:has(.unfulfilled-ad)) {
display: none;
}
</style>

View file

@ -47,7 +47,7 @@
<svelte:window bind:innerWidth="{windowWidth}" />
<div
class="leaderboard__sticky {cls}"
class="freestar-adslot leaderboard__sticky {cls}"
class:sticky
class:unstick
{id}

View file

@ -54,6 +54,19 @@ export const loadBootstrap = () => {
// @ts-ignore window global
window.googletag.pubads().enableAsyncRendering();
window.googletag.pubads().collapseEmptyDivs(true);
window.googletag
.pubads()
.addEventListener('slotRenderEnded', function (event) {
const adDiv = document.getElementById(event.slot.getSlotElementId());
if (!adDiv) return;
// If the ad slot is empty
if (event.isEmpty) {
adDiv.classList.add('unfulfilled-ad');
} else {
adDiv.classList.remove('unfulfilled-ad');
}
});
});
// Set page-level key-values