-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-segment-analytics.js
More file actions
157 lines (136 loc) · 4.97 KB
/
04-segment-analytics.js
File metadata and controls
157 lines (136 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;(function () {
'use strict'
/**
* IBM Segment Event Types:
* - 'CTA Clicked': Call-to-action clicks (links, buttons)
* - 'UI Interaction': General UI interactions (excludes userId)
* - 'User Form': Form interactions (handled separately in form-specific files)
*/
// Determine if a data-track event should be CTA Clicked or UI Interaction
const getEventType = (eventName) => {
// Links are typically CTAs
if (eventName.includes('Link Clicked') || eventName.includes('Clicked')) {
return 'CTA Clicked'
}
// Buttons and other interactions
if (eventName.includes('Button') || eventName.includes('Form')) {
return 'UI Interaction'
}
// Default to UI Interaction
return 'UI Interaction'
}
// Extract CTA text from event name or element
const extractCTA = (eventName, element) => {
if (element) {
return element.textContent?.trim() || element.getAttribute('title') || eventName
}
return eventName
}
// Extract namespace from event name
const extractNamespace = (eventName) => {
if (eventName.includes('Footer')) return 'footer'
if (eventName.includes('Tutorial')) return 'tutorial'
if (eventName.includes('Feedback')) return 'feedback'
if (eventName.includes('Edit')) return 'content'
if (eventName.includes('Colab')) return 'tutorial'
return 'docs'
}
// Extract action from event name
const extractAction = (eventName) => {
if (eventName.includes('Clicked')) return 'clicked'
if (eventName.includes('Submitted')) return 'submitted'
if (eventName.includes('Copied')) return 'copied'
return 'interacted'
}
const trackEvent = (name, payload = {}, element = null) => {
if (window.analytics && window.getSegmentCommonProperties) {
const eventType = getEventType(name)
const commonProps = window.getSegmentCommonProperties(eventType)
let eventPayload = { ...commonProps, ...payload }
if (eventType === 'CTA Clicked') {
eventPayload = {
...eventPayload,
CTA: extractCTA(name, element),
location: extractNamespace(name),
}
} else if (eventType === 'UI Interaction') {
eventPayload = {
...eventPayload,
action: extractAction(name),
CTA: extractCTA(name, element),
namespace: extractNamespace(name),
elementId: element?.id || '',
payload: payload,
}
}
window.analytics.track(eventType, eventPayload)
}
}
const trackLinkEvent = (element, name, payload = {}) => {
if (window.analytics && window.getSegmentCommonProperties) {
const eventType = 'CTA Clicked' // Links are always CTAs
const commonProps = window.getSegmentCommonProperties(eventType)
const eventPayload = {
...commonProps,
...payload,
CTA: extractCTA(name, element),
location: extractNamespace(name),
type: 'Link',
text: element.textContent?.trim() || element.getAttribute('title') || '',
}
window.analytics.trackLink(element, eventType, eventPayload)
}
}
// Add click event listeners to all elements with a data-track attribute.
if (window.analytics) {
const trackedLinkElements = document.querySelectorAll('a[data-track]')
const trackedElements = document.querySelectorAll('[data-track]:not(a)')
trackedLinkElements.forEach((element) => {
trackLinkEvent(element, element.dataset.track)
})
trackedElements.forEach((element) => {
element.addEventListener('click', (e) => {
trackEvent(element.dataset.track, {}, element)
})
})
}
/**
* Track page view with friendly name
* IBM requires page events to have a friendly "page" property
*/
const trackPage = (pageName) => {
if (window.analytics && window.SEGMENT_COMMON_PROPERTIES) {
// Get friendly page name from title or use provided name
const friendlyName = pageName || document.title.split('|')[0].trim()
// Get common properties for page event (excludes userId per IBM requirements)
const pageProperties = window.getSegmentCommonProperties('page')
window.analytics.page(friendlyName, {
...pageProperties,
path: window.location.pathname,
url: window.location.href,
title: document.title,
})
}
}
// Wait for analytics to load, then track page view on initial load
const waitForAnalytics = (callback, maxAttempts = 50, interval = 100) => {
let attempts = 0
const checkAnalytics = () => {
attempts++
if (window.analytics && window.analytics.initialized) {
callback()
} else if (attempts < maxAttempts) {
setTimeout(checkAnalytics, interval)
}
}
checkAnalytics()
}
// Track page view on initial load once analytics is ready
waitForAnalytics(() => {
trackPage()
})
// Expose trackEvent, trackLinkEvent, and trackPage to the global scope.
window.trackEvent = trackEvent
window.trackLinkEvent = trackLinkEvent
window.trackPage = trackPage
})()