Skip to content

Commit 3c2eea0

Browse files
committed
add ketch logic to js file and let middleware handle context
1 parent a363a1c commit 3c2eea0

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

src/js/04-segment-analytics.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
;(function () {
22
'use strict'
33

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-
184
const trackEvent = (name, payload) => {
195
if (window.analytics) {
20-
window.analytics.track(name, payload || {}, ketchConsentContext)
6+
window.analytics.track(name, payload || {})
217
}
228
}
239

2410
const trackLinkEvent = (element, name, payload) => {
2511
if (window.analytics) {
26-
window.analytics.trackLink(element, name, payload || {}, ketchConsentContext)
12+
window.analytics.trackLink(element, name, payload || {})
2713
}
2814
}
2915

16+
// Add click event listeners to all elements with a data-track attribute.
3017
if (window.analytics) {
3118
const trackedLinkElements = document.querySelectorAll('a[data-track]')
3219
const trackedElements = document.querySelectorAll('[data-track]:not(a)')
@@ -43,7 +30,51 @@
4330
}
4431

4532
// 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.
4733
window.trackEvent = trackEvent
4834
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+
}
4980
})()

src/partials/head-scripts.hbs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,8 @@ analytics.page();
77
</script>
88
{{/with}}
99

10-
{{#with site.keys.ketchSma rtTagUrl}}
10+
{{#with site.keys.ketchSmartTagUrl}}
1111
<script>!function(){window.semaphore=window.semaphore||[],window.ketch=function(){window.semaphore.push(arguments)};var e=document.createElement("script");e.type="text/javascript",e.src="{{this}}",e.defer=e.async=!0,document.getElementsByTagName("head")[0].appendChild(e)}();</script>
12-
<script>
13-
ketch('showPreferences');
14-
</script>
15-
<script>
16-
function saveConsent(consent) {
17-
window.ketchConsent = consent;
18-
};
19-
ketch("on", "consent", (consent) => {
20-
saveConsent(consent);
21-
if (window.analytics) {
22-
window.analytics.addSourceMiddleware(({ payload, next }) => {
23-
if (window.ketchConsent) {
24-
payload.obj.context.consent = {
25-
categoryPreferences: window.ketchConsent?.purposes,
26-
};
27-
}
28-
next(payload);
29-
});
30-
}
31-
});
32-
ketch("on", "userConsentUpdated", saveConsent);
33-
ketch("on", "regionInfo", regionInfo => {
34-
var customTextRegions = ["US-CA"];
35-
if (customTextRegions.includes(regionInfo)) {
36-
var preferenceCenterLinkElement = document.getElementById("preferenceCenterLink");
37-
preferenceCenterLinkElement.textContent = "Do Not Sell My Personal Information";
38-
}
39-
})
40-
</script>
4112
{{/with}}
4213

4314
<script>var uiRootPath = '{{{uiRootPath}}}'</script>

0 commit comments

Comments
 (0)