diff --git a/src/components/AdSlot/AdSlot.svelte b/src/components/AdSlot/AdSlot.svelte index c78646f6..57673a83 100644 --- a/src/components/AdSlot/AdSlot.svelte +++ b/src/components/AdSlot/AdSlot.svelte @@ -14,15 +14,26 @@ const adId = getRandomAdId(); onMount(() => { - window.graphicsAdQueue = window.graphicsAdQueue || []; - window.graphicsAdQueue.push({ + const adSlot = { placementName, slotId: adId, targeting: { div_id: adId, type: adType, }, - }); + }; + // @ts-ignore + const freestar = window?.freestar; + // Add adSlot to freestar queue directly if already initialised + if (freestar) { + freestar.queue.push(function () { + freestar.newAdSlots([adSlot], freestar.config.channel); + }); + // ... otherwise add to the graphicsAdQueue queue. + } else { + window.graphicsAdQueue = window.graphicsAdQueue || []; + window.graphicsAdQueue.push(adSlot); + } }); diff --git a/src/components/AdSlot/adScripts/bootstrap.ts b/src/components/AdSlot/adScripts/bootstrap.ts index be41d5ec..02612682 100644 --- a/src/components/AdSlot/adScripts/bootstrap.ts +++ b/src/components/AdSlot/adScripts/bootstrap.ts @@ -57,14 +57,6 @@ export const loadBootstrap = () => { window.googletag.pubads().collapseEmptyDivs(true); }); - if (!Array.isArray((window).graphicsAdQueue)) { - console.error('Ad queue not initialized!'); - } - - freestar.queue.push(function() { - freestar.newAdSlots((window).graphicsAdQueue || [], freestar.config.channel); - }); - // Set page-level key-values // cf: https://help.freestar.com/help/using-key-values freestar.queue.push(function() { @@ -81,5 +73,13 @@ export const loadBootstrap = () => { window.googletag.pubads().setTargeting('template', 'graphics'); window.googletag.pubads().setTargeting('graphicId', graphicId); }); + + if (!Array.isArray((window).graphicsAdQueue)) { + console.error('Ad queue not initialized!'); + } + + freestar.queue.push(function() { + freestar.newAdSlots((window).graphicsAdQueue || [], freestar.config.channel); + }); }); };