|
1 | 1 | ;(function () { |
2 | 2 | 'use strict' |
3 | 3 |
|
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 |
7 | 29 | } |
| 30 | + return eventName |
8 | 31 | } |
9 | 32 |
|
10 | | - const trackLinkEvent = (element, name, payload) => { |
11 | | - if (window.analytics) { |
12 | | - window.analytics.trackLink(element, name, payload || {}) |
| 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 | + // IBM requires identify() call before track events |
| 76 | + if (window.SEGMENT_COMMON_PROPERTIES?.userId) { |
| 77 | + const identifyTraits = window.getSegmentIdentifyTraits() |
| 78 | + window.analytics.identify(window.SEGMENT_COMMON_PROPERTIES.userId, identifyTraits) |
| 79 | + } |
| 80 | + |
| 81 | + window.analytics.track(eventType, eventPayload) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const trackLinkEvent = (element, name, payload = {}) => { |
| 86 | + if (window.analytics && window.getSegmentCommonProperties) { |
| 87 | + const eventType = 'CTA Clicked' // Links are always CTAs |
| 88 | + const commonProps = window.getSegmentCommonProperties(eventType) |
| 89 | + |
| 90 | + const eventPayload = { |
| 91 | + ...commonProps, |
| 92 | + ...payload, |
| 93 | + CTA: extractCTA(name, element), |
| 94 | + location: extractNamespace(name), |
| 95 | + type: 'Link', |
| 96 | + text: element.textContent?.trim() || element.getAttribute('title') || '', |
| 97 | + } |
| 98 | + |
| 99 | + // IBM requires identify() call before track events |
| 100 | + if (window.SEGMENT_COMMON_PROPERTIES?.userId) { |
| 101 | + const identifyTraits = window.getSegmentIdentifyTraits() |
| 102 | + window.analytics.identify(window.SEGMENT_COMMON_PROPERTIES.userId, identifyTraits) |
| 103 | + } |
| 104 | + |
| 105 | + window.analytics.trackLink(element, eventType, eventPayload) |
13 | 106 | } |
14 | 107 | } |
15 | 108 |
|
|
24 | 117 |
|
25 | 118 | trackedElements.forEach((element) => { |
26 | 119 | element.addEventListener('click', (e) => { |
27 | | - trackEvent(element.dataset.track) |
| 120 | + trackEvent(element.dataset.track, {}, element) |
28 | 121 | }) |
29 | 122 | }) |
30 | 123 | } |
|
0 commit comments