From d388c2c5631ba6ce490b942de5eba9e403f4c9d3 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Fri, 21 Mar 2025 17:54:54 +0000 Subject: [PATCH] Remove empty ad slot --- src/components/Analytics/GoogleAd.astro | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/components/Analytics/GoogleAd.astro b/src/components/Analytics/GoogleAd.astro index 57e80c256..512135e09 100644 --- a/src/components/Analytics/GoogleAd.astro +++ b/src/components/Analytics/GoogleAd.astro @@ -90,5 +90,24 @@ const page = Astro.url; } googletag.enableServices(); + + googletag.pubads().addEventListener('slotRenderEnded', function (e) { + if (!e.isEmpty) { + return; + } + + const slotId = e.slot.getSlotElementId(); + if (!slotId) { + return; + } + + // If empty, hide the ad slot after a small delay. + setTimeout(() => { + const adContainer = document.getElementById(slotId); + if (adContainer) { + adContainer.style.display = 'none'; + } + }, 1800); + }); });