Skip to content

Commit 1c1bc69

Browse files
authored
Replace Ketch with IBM TrustArc consent management (#204)
1 parent f8ce864 commit 1c1bc69

File tree

6 files changed

+244
-114
lines changed

6 files changed

+244
-114
lines changed

src/js/04-segment-analytics.js

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,96 @@
11
;(function () {
22
'use strict'
33

4-
const trackEvent = (name, payload) => {
5-
if (window.analytics) {
6-
window.analytics.track(name, payload || {})
4+
/**
5+
* IBM Segment Event Types:
6+
* - 'CTA Clicked': Call-to-action clicks (links, buttons)
7+
* - 'UI Interaction': General UI interactions (excludes userId)
8+
* - 'User Form': Form interactions (handled separately in form-specific files)
9+
*/
10+
11+
// Determine if a data-track event should be CTA Clicked or UI Interaction
12+
const getEventType = (eventName) => {
13+
// Links are typically CTAs
14+
if (eventName.includes('Link Clicked') || eventName.includes('Clicked')) {
15+
return 'CTA Clicked'
16+
}
17+
// Buttons and other interactions
18+
if (eventName.includes('Button') || eventName.includes('Form')) {
19+
return 'UI Interaction'
20+
}
21+
// Default to UI Interaction
22+
return 'UI Interaction'
23+
}
24+
25+
// Extract CTA text from event name or element
26+
const extractCTA = (eventName, element) => {
27+
if (element) {
28+
return element.textContent?.trim() || element.getAttribute('title') || eventName
29+
}
30+
return eventName
31+
}
32+
33+
// Extract namespace from event name
34+
const extractNamespace = (eventName) => {
35+
if (eventName.includes('Footer')) return 'footer'
36+
if (eventName.includes('Tutorial')) return 'tutorial'
37+
if (eventName.includes('Feedback')) return 'feedback'
38+
if (eventName.includes('Edit')) return 'content'
39+
if (eventName.includes('Colab')) return 'tutorial'
40+
return 'docs'
41+
}
42+
43+
// Extract action from event name
44+
const extractAction = (eventName) => {
45+
if (eventName.includes('Clicked')) return 'clicked'
46+
if (eventName.includes('Submitted')) return 'submitted'
47+
if (eventName.includes('Copied')) return 'copied'
48+
return 'interacted'
49+
}
50+
51+
const trackEvent = (name, payload = {}, element = null) => {
52+
if (window.analytics && window.getSegmentCommonProperties) {
53+
const eventType = getEventType(name)
54+
const commonProps = window.getSegmentCommonProperties(eventType)
55+
56+
let eventPayload = { ...commonProps, ...payload }
57+
58+
if (eventType === 'CTA Clicked') {
59+
eventPayload = {
60+
...eventPayload,
61+
CTA: extractCTA(name, element),
62+
location: extractNamespace(name),
63+
}
64+
} else if (eventType === 'UI Interaction') {
65+
eventPayload = {
66+
...eventPayload,
67+
action: extractAction(name),
68+
CTA: extractCTA(name, element),
69+
namespace: extractNamespace(name),
70+
elementId: element?.id || '',
71+
payload: payload,
72+
}
73+
}
74+
75+
window.analytics.track(eventType, eventPayload)
776
}
877
}
978

10-
const trackLinkEvent = (element, name, payload) => {
11-
if (window.analytics) {
12-
window.analytics.trackLink(element, name, payload || {})
79+
const trackLinkEvent = (element, name, payload = {}) => {
80+
if (window.analytics && window.getSegmentCommonProperties) {
81+
const eventType = 'CTA Clicked' // Links are always CTAs
82+
const commonProps = window.getSegmentCommonProperties(eventType)
83+
84+
const eventPayload = {
85+
...commonProps,
86+
...payload,
87+
CTA: extractCTA(name, element),
88+
location: extractNamespace(name),
89+
type: 'Link',
90+
text: element.textContent?.trim() || element.getAttribute('title') || '',
91+
}
92+
93+
window.analytics.trackLink(element, eventType, eventPayload)
1394
}
1495
}
1596

@@ -24,12 +105,57 @@
24105

25106
trackedElements.forEach((element) => {
26107
element.addEventListener('click', (e) => {
27-
trackEvent(element.dataset.track)
108+
trackEvent(element.dataset.track, {}, element)
28109
})
29110
})
30111
}
31112

32-
// Expose trackEvent and trackLinkEvent to the global scope.
113+
/**
114+
* Track page view with friendly name
115+
* IBM requires page events to have a friendly "page" property
116+
*/
117+
const trackPage = (pageName) => {
118+
if (window.analytics && window.SEGMENT_COMMON_PROPERTIES) {
119+
// Get friendly page name from title or use provided name
120+
const friendlyName = pageName || document.title.split('|')[0].trim()
121+
122+
// Get common properties for page event (excludes userId per IBM requirements)
123+
const pageProperties = window.getSegmentCommonProperties('page')
124+
125+
window.analytics.page(friendlyName, {
126+
...pageProperties,
127+
path: window.location.pathname,
128+
url: window.location.href,
129+
title: document.title,
130+
})
131+
}
132+
}
133+
134+
// Wait for analytics to load, then track page view on initial load
135+
const waitForAnalytics = (callback, maxAttempts = 50, interval = 100) => {
136+
let attempts = 0
137+
const checkAnalytics = () => {
138+
attempts++
139+
if (window.analytics && window.analytics.initialized) {
140+
callback()
141+
} else if (attempts < maxAttempts) {
142+
setTimeout(checkAnalytics, interval)
143+
}
144+
}
145+
checkAnalytics()
146+
}
147+
148+
// Track page view on initial load once analytics is ready
149+
waitForAnalytics(() => {
150+
// Identify user with anonymous ID before tracking page
151+
if (window.SEGMENT_COMMON_PROPERTIES && window.SEGMENT_COMMON_PROPERTIES.userId) {
152+
window.analytics.identify(window.SEGMENT_COMMON_PROPERTIES.userId)
153+
}
154+
trackPage()
155+
})
156+
157+
// Expose trackEvent, trackLinkEvent, and trackPage to the global scope.
33158
window.trackEvent = trackEvent
34159
window.trackLinkEvent = trackLinkEvent
160+
window.trackPage = trackPage
35161
})()

src/js/05-feedback-dialog.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,19 @@
4040
form.onsubmit = (e) => {
4141
e.preventDefault()
4242
const message = form.elements.message.value
43-
if (message && window.trackEvent) {
44-
window.trackEvent('Feedback Form', {
45-
message,
43+
if (message && window.analytics && window.getSegmentCommonProperties) {
44+
const commonProps = window.getSegmentCommonProperties('User Form')
45+
46+
// Map to IBM User Form schema
47+
window.analytics.track('User Form', {
48+
...commonProps,
49+
action: 'submitted',
50+
formId: 'feedback_form',
51+
formType: 'feedback',
52+
field: 'feedback_message',
53+
fieldType: 'text_field',
54+
title: 'Give Feedback',
55+
data: message,
4656
})
4757
}
4858
form.elements.message.value = ''
@@ -52,6 +62,20 @@
5262

5363
const feedbackButtonYes = document.getElementById('feedback_button_yes')
5464
feedbackButtonYes.addEventListener('click', (e) => {
65+
if (window.analytics && window.getSegmentCommonProperties) {
66+
const commonProps = window.getSegmentCommonProperties('User Form')
67+
68+
// Track positive feedback button click
69+
window.analytics.track('User Form', {
70+
...commonProps,
71+
action: 'clicked',
72+
formId: 'feedback_buttons',
73+
formType: 'feedback',
74+
field: 'helpful_yes',
75+
fieldType: 'button',
76+
title: 'Was This Helpful?',
77+
})
78+
}
5579
showThankYou()
5680
})
5781

src/js/06-copy-to-clipboard.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,22 @@
9292
}
9393

9494
function trackCopy (language, title, text) {
95-
if (window.trackEvent) {
95+
if (window.analytics && window.getSegmentCommonProperties) {
9696
var sample = text.slice(0, 50).replace(/\s+/g, ' ').trim()
97-
window.trackEvent('Code Snippet Copied', {
98-
snippetLanguage: language,
99-
snippetTitle: title,
100-
snippetSample: sample,
97+
var commonProps = window.getSegmentCommonProperties('UI Interaction')
98+
99+
// Map to IBM UI Interaction schema
100+
window.analytics.track('UI Interaction', {
101+
...commonProps,
102+
action: 'copied',
103+
CTA: 'Copy Code',
104+
namespace: 'code-snippet',
105+
elementId: 'copy-button',
106+
payload: {
107+
language: language,
108+
title: title,
109+
sample: sample,
110+
},
101111
})
102112
}
103113
}

src/js/09-ketch-consent.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/partials/footer.hbs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
target="_blank"
2525
data-track="Footer Terms of Use Link Clicked"
2626
>Terms of use</a>
27-
{{#with site.keys.ketchSmartTagUrl}}
27+
{{#with site.keys.ibmSegment}}
2828
<span id="preferenceCenterContainer">
29-
|
29+
·&nbsp;
3030
<a
3131
id="preferenceCenterLink"
32-
href="https://www.datastax.com/preferences?redirect_to={{{../site.url}}}{{{../page.url}}}"
32+
href="#"
33+
onclick="if(typeof window !== 'undefined' && window.truste && window.truste.eu && window.truste.eu.clickListener) { window.truste.eu.clickListener(); } return false;"
3334
data-track="Footer Consent Preference Link Clicked"
35+
style="cursor: pointer;"
3436
>Manage Privacy Choices</a>
3537
</span>
3638
{{/with}}
@@ -93,4 +95,4 @@
9395
9496
</p>
9597
</div>
96-
</footer>
98+
</footer>

0 commit comments

Comments
 (0)