Last active
January 8, 2021 19:12
-
-
Save djrosenbaum/42d121c4f9e70b7f8e9bb9ec566586a3 to your computer and use it in GitHub Desktop.
Detect react wiped ad slots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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