|
1 | 1 | ;(function () {
|
2 | 2 | 'use strict'
|
3 | 3 |
|
4 |
| - const ketchConsentContext = { |
5 |
| - context: { |
6 |
| - consent: { |
7 |
| - categoryPreferences: { |
8 |
| - ...(window.ketchConsent?.purposes || { |
9 |
| - analytics: false, |
10 |
| - essential_services: false, |
11 |
| - targeted_advertising: false, |
12 |
| - }), |
13 |
| - }, |
14 |
| - }, |
15 |
| - }, |
16 |
| - } |
17 |
| - |
18 | 4 | const trackEvent = (name, payload) => {
|
19 | 5 | if (window.analytics) {
|
20 |
| - window.analytics.track(name, payload || {}, ketchConsentContext) |
| 6 | + window.analytics.track(name, payload || {}) |
21 | 7 | }
|
22 | 8 | }
|
23 | 9 |
|
24 | 10 | const trackLinkEvent = (element, name, payload) => {
|
25 | 11 | if (window.analytics) {
|
26 |
| - window.analytics.trackLink(element, name, payload || {}, ketchConsentContext) |
| 12 | + window.analytics.trackLink(element, name, payload || {}) |
27 | 13 | }
|
28 | 14 | }
|
29 | 15 |
|
| 16 | + // Add click event listeners to all elements with a data-track attribute. |
30 | 17 | if (window.analytics) {
|
31 | 18 | const trackedLinkElements = document.querySelectorAll('a[data-track]')
|
32 | 19 | const trackedElements = document.querySelectorAll('[data-track]:not(a)')
|
|
43 | 30 | }
|
44 | 31 |
|
45 | 32 | // Expose trackEvent and trackLinkEvent to the global scope.
|
46 |
| - // All tracking events should be done through these functions because they handle the Ketch consent context. |
47 | 33 | window.trackEvent = trackEvent
|
48 | 34 | window.trackLinkEvent = trackLinkEvent
|
| 35 | + |
| 36 | + // Save the Ketch consent state to the global scope and add consent middleware to analytics calls. |
| 37 | + if (window && window.ketch && window.analytics) { |
| 38 | + const saveConsent = (consent) => { |
| 39 | + window.ketchConsent = consent |
| 40 | + } |
| 41 | + window.ketch('on', 'consent', saveConsent) |
| 42 | + window.ketch('on', 'userConsentUpdated', saveConsent) |
| 43 | + window.ketch('on', 'regionInfo', (regionInfo) => { |
| 44 | + const customTextRegions = ['US-CA'] |
| 45 | + if (customTextRegions.includes(regionInfo)) { |
| 46 | + const preferenceCenterLinkElement = document.getElementById('preferenceCenterLink') |
| 47 | + preferenceCenterLinkElement.textContent = 'Do Not Sell My Personal Information' |
| 48 | + } |
| 49 | + }) |
| 50 | + window.analytics.addSourceMiddleware(({ payload, next }) => { |
| 51 | + if (window.ketchConsent) { |
| 52 | + payload.obj.properties = { |
| 53 | + ...(payload.obj.properties || {}), |
| 54 | + 'Analytics Storage Consent State': |
| 55 | + (window.ketchConsent?.purposes || {})?.analytics === true |
| 56 | + ? 'granted' |
| 57 | + : 'denied', |
| 58 | + 'Ads Storage Consent State': |
| 59 | + (window.ketchConsent?.purposes || {})?.targeted_advertising === true |
| 60 | + ? 'granted' |
| 61 | + : 'denied', |
| 62 | + 'Ad User Data Consent State': |
| 63 | + (window.ketchConsent?.purposes || {})?.targeted_advertising === true |
| 64 | + ? 'granted' |
| 65 | + : 'denied', |
| 66 | + context: { |
| 67 | + consent: { |
| 68 | + categoryPreferences: window.ketchConsent?.purposes || {}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + traits: window.ketchConsent?.purposes || {}, |
| 72 | + } |
| 73 | + payload.obj.context.consent = { |
| 74 | + categoryPreferences: window.ketchConsent?.purposes, |
| 75 | + } |
| 76 | + } |
| 77 | + next(payload) |
| 78 | + }) |
| 79 | + } |
49 | 80 | })()
|
0 commit comments