|
| 1 | +;(function () { |
| 2 | + 'use strict' |
| 3 | + |
| 4 | + window.analytics.ready(() => { |
| 5 | + window.ketch('once', 'consent', onKetchConsent) |
| 6 | + window.ketch('on', 'consent', onKetchConsentGtagTrack) |
| 7 | + window.ketch('on', 'userConsentUpdated', onKetchConsentUpdated) |
| 8 | + }) |
| 9 | + |
| 10 | + // If the user is in the US-CA region, change the text of the preference center link |
| 11 | + window.ketch('on', 'regionInfo', (regionInfo) => { |
| 12 | + const customTextRegions = ['US-CA'] |
| 13 | + if (customTextRegions.includes(regionInfo)) { |
| 14 | + const preferenceCenterLinkElement = document.getElementById('preferenceCenterLink') |
| 15 | + if (preferenceCenterLinkElement) { |
| 16 | + preferenceCenterLinkElement.textContent = 'Do Not Sell My Personal Information' |
| 17 | + } |
| 18 | + } |
| 19 | + }) |
| 20 | + |
| 21 | + // If the user is in the default jurisdiction, remove the preference center link |
| 22 | + window.ketch('on', 'jurisdiction', (jurisdiction) => { |
| 23 | + if (jurisdiction.includes('default')) { |
| 24 | + const preferenceCenterContainerElement = document.getElementById('preferenceCenterContainer') |
| 25 | + if (preferenceCenterContainerElement) { |
| 26 | + preferenceCenterContainerElement.remove() |
| 27 | + } |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + // Once - This will be fired only one time, will initialize all the main features. |
| 32 | + const onKetchConsent = (consent) => { |
| 33 | + window.ketchConsent = consent |
| 34 | + addKetchConsentToContextMiddleware() |
| 35 | + window.analytics.page() |
| 36 | + // loadScripts(); // Load any script if we have scripts to fire after ketch consent is fired. |
| 37 | + } |
| 38 | + |
| 39 | + // on - Each time the user changes the preferences, save them to the global variable |
| 40 | + const onKetchConsentUpdated = (consent) => { |
| 41 | + window.ketchConsent = consent |
| 42 | + } |
| 43 | + |
| 44 | + // On - each time the consent is loaded, track it to the gtag event |
| 45 | + const onKetchConsentGtagTrack = (consent) => { |
| 46 | + if (window.gtag && |
| 47 | + consent.purposes && |
| 48 | + 'analytics' in consent.purposes && |
| 49 | + 'targeted_advertising' in consent.purposes |
| 50 | + ) { |
| 51 | + const analyticsString = consent.purposes.analytics === true ? 'granted' : 'denied' |
| 52 | + const targetedAdsString = consent.purposes.targeted_advertising === true ? 'granted' : 'denied' |
| 53 | + |
| 54 | + const gtagObject = { |
| 55 | + analytics_storage: analyticsString, |
| 56 | + ad_personalization: targetedAdsString, |
| 57 | + ad_storage: targetedAdsString, |
| 58 | + ad_user_data: targetedAdsString, |
| 59 | + } |
| 60 | + window.gtag('consent', 'update', gtagObject) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Use the analytics.addSourceMiddleware function to include the consent on all the events |
| 65 | + const addKetchConsentToContextMiddleware = () => { |
| 66 | + window.analytics.addSourceMiddleware(({ payload, next }) => { |
| 67 | + if (window.ketchConsent) { |
| 68 | + const analyticsString = window.ketchConsent.purposes.analytics === true ? 'granted' : 'denied' |
| 69 | + const targetedAdsString = window.ketchConsent.purposes.targeted_advertising === true ? 'granted' : 'denied' |
| 70 | + |
| 71 | + payload.obj.properties = { |
| 72 | + ...(payload.obj.properties || {}), |
| 73 | + analyticsStorageConsentState: analyticsString, |
| 74 | + adsStorageConsentState: targetedAdsString, |
| 75 | + adUserDataConsentState: targetedAdsString, |
| 76 | + adPersonalizationConsentState: targetedAdsString, |
| 77 | + } |
| 78 | + payload.obj.context.consent = { |
| 79 | + categoryPreferences: window.ketchConsent?.purposes, |
| 80 | + } |
| 81 | + } |
| 82 | + next(payload) |
| 83 | + }) |
| 84 | + } |
| 85 | +})() |
0 commit comments