|
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 |
| 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) |
7 | 76 | } |
8 | 77 | } |
9 | 78 |
|
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) |
13 | 94 | } |
14 | 95 | } |
15 | 96 |
|
|
24 | 105 |
|
25 | 106 | trackedElements.forEach((element) => { |
26 | 107 | element.addEventListener('click', (e) => { |
27 | | - trackEvent(element.dataset.track) |
| 108 | + trackEvent(element.dataset.track, {}, element) |
28 | 109 | }) |
29 | 110 | }) |
30 | 111 | } |
31 | 112 |
|
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. |
33 | 158 | window.trackEvent = trackEvent |
34 | 159 | window.trackLinkEvent = trackLinkEvent |
| 160 | + window.trackPage = trackPage |
35 | 161 | })() |
0 commit comments