Skip to content

Instantly share code, notes, and snippets.

@scott-thrillist
Last active November 13, 2020 06:09
Show Gist options
  • Save scott-thrillist/bf7d429f6c78a417fe4717a2e1791dc1 to your computer and use it in GitHub Desktop.
Save scott-thrillist/bf7d429f6c78a417fe4717a2e1791dc1 to your computer and use it in GitHub Desktop.
/**
* Amazon APS TAM ad setup
*
* @param {string} options.slotId The ad slot ID
* @param {array} options.sizes Array of sizes in DFP sizeMapping format
*/
const setupTamAd = options => {
const { slotId, sizes, adElementRef = {} } = options;
// request the bids for the googletag slots
window.apstag.fetchBids(
{
slots: [
{
slotID: slotId,
slotName: this.pagePath,
sizes
}
]
},
() => {
window.googletag.cmd.push(() => {
window.apstag.setDisplayBids();
// deferring for reliability
setTimeout(() => {
DFPManager.refresh(slotId);
}, 0);
// IE11
setTimeout(() => {
const slotEl = adElementRef.current || document.getElementById(slotId);
if (slotEl && slotEl.innerHTML === '') {
console.log('AD BACKUP REFRESH', slotId);
DFPManager.refresh(slotId);
}
}, 2000);
});
}
);
};
/**********************************/
return (
<DFPSlotsProvider
singleRequest
disableInitialLoad // required for TAM
dfpNetworkId={pubId}
targetingArguments={adAttributes}
>
<AdSlot
sizes={sizes}
sizeMapping={sizeMapping}
adUnit={pagePath}
className={wrapperClass}
onSlotRegister={setupTamAd} // required for TAM
onSlotRender={slotRendered}
targetingArguments={{ adType }}
/>
</DFPSlotsProvider>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment