Last active
December 6, 2020 14:05
-
-
Save itslukej/a9be5f22ba21e4361b00f5ba1035f776 to your computer and use it in GitHub Desktop.
NitroPay Consent Framework x AdSense
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
| 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