Skip to content

Instantly share code, notes, and snippets.

@itslukej
Last active December 6, 2020 14:05
Show Gist options
  • Select an option

  • Save itslukej/a9be5f22ba21e4361b00f5ba1035f776 to your computer and use it in GitHub Desktop.

Select an option

Save itslukej/a9be5f22ba21e4361b00f5ba1035f776 to your computer and use it in GitHub Desktop.
NitroPay Consent Framework x AdSense
function injectAdSense() {
const script = document.createElement('script');
script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
script.async = true;
script.setAttribute('data-ad-client', 'ca-pub-XXXXXXXXXXXXXXXX');
document.head.appendChild(script);
}
function awaitTCFReady() {
return new Promise(r => document.addEventListener("nitroAds.loaded", r));
}
async function run() {
if (!window.nitroAds?.loaded) await awaitTCFReady();
if (!window.__tcfapi) return injectAdSense();
window.__tcfapi('addEventListener', 2, (tcData, success) => {
if (success && (tcData.eventStatus == 'useractioncomplete' || tcData.eventStatus == 'tcloaded') && tcData.purpose.consents[1]) {
injectAdSense();
window.__tcfapi('removeEventListener', 2, () => {}, tcData.listenerId);
}
});
}
export default function() {
run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment