Skip to content

Instantly share code, notes, and snippets.

@djrosenbaum
Last active January 8, 2021 19:12
Show Gist options
  • Save djrosenbaum/42d121c4f9e70b7f8e9bb9ec566586a3 to your computer and use it in GitHub Desktop.
Save djrosenbaum/42d121c4f9e70b7f8e9bb9ec566586a3 to your computer and use it in GitHub Desktop.
Detect react wiped ad slots
/*
sometimes react wipes ad slots from the DOM
this code is one option to detect when ad slots have been wiped by react
*/
const slotsWiped = [];
/**
* Checks for wiped ad slots from react
*
* @returns {undefined} - undefined
*/
function checkForWipedAdSlots() {
// store defined slots from googletag
const slots = window.googletag.pubads().getSlots();
// loop through each slot checking if the defined slot id exists in the DOM
slots.forEach((slot) => {
const slotId = slot.getSlotElementId();
// conditionally check if the defined slot id exists in the DOM and not already stored
if (!document.getElementById(slotId) && slotsWiped.indexOf(slotId) === -1) {
// handle wiped ad slots here
slotsWiped.push(slotId);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment