| 1 |
/** |
| 2 |
* Amplitude Analytics Integration for Route Widget |
| 3 |
* Handles analytics tracking for widget interactions in WooCommerce |
| 4 |
* |
| 5 |
* @package RouteApp |
| 6 |
* @version 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
(function ($, window) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
let amplitudeInstance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize Amplitude analytics |
| 16 |
* |
| 17 |
* @returns {boolean} - Returns true if initialization successful |
| 18 |
*/ |
| 19 |
function initializeAmplitude() { |
| 20 |
// Check if Amplitude SDK is loaded |
| 21 |
if (!window.amplitude) { |
| 22 |
console.warn('Amplitude SDK not loaded'); |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
try { |
| 27 |
// Safely check if cartData exists before accessing its properties |
| 28 |
if (typeof cartData === 'undefined' || !cartData || !cartData.amplitudeKey) { |
| 29 |
console.error('Amplitude API key not found'); |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
// Initialize Amplitude if not already initialized |
| 34 |
if (!window.amplitudeInitialized) { |
| 35 |
window.amplitude.init(cartData.amplitudeKey, { |
| 36 |
transport: 'beacon', |
| 37 |
flushIntervalMillis: 0, |
| 38 |
flushQueueSize: 1, |
| 39 |
fetchRemoteConfig: true, |
| 40 |
|
| 41 |
deviceId: cartData.cartId, |
| 42 |
|
| 43 |
/** |
| 44 |
* Use localStorage for both - more reliable in iframes |
| 45 |
*/ |
| 46 |
identityStorage: 'localStorage', |
| 47 |
|
| 48 |
autocapture: { |
| 49 |
attribution: false, |
| 50 |
pageViews: false, |
| 51 |
sessions: true, |
| 52 |
formInteractions: false, |
| 53 |
fileDownloads: false, |
| 54 |
elementInteractions: false, |
| 55 |
pageUrlEnrichment: false, |
| 56 |
webVitals: false, |
| 57 |
} |
| 58 |
}); |
| 59 |
|
| 60 |
window.amplitudeInitialized = true; |
| 61 |
console.log('Amplitude initialized successfully'); |
| 62 |
} |
| 63 |
|
| 64 |
// Set the Amplitude instance for tracking functions |
| 65 |
amplitudeInstance = window.amplitude; |
| 66 |
return true; |
| 67 |
} catch (error) { |
| 68 |
console.error('Failed to initialize Amplitude:', error); |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get common analytics attributes |
| 75 |
* |
| 76 |
* @param {Object} additionalConfig - Additional data to merge |
| 77 |
* @returns {Object} - Analytics attributes object |
| 78 |
*/ |
| 79 |
function getAnalyticsAttributes(additionalConfig = {}) { |
| 80 |
const pathname = window.location.pathname; |
| 81 |
const pageType = pathname === "/cart/" || pathname === "/cart" ? 'cart' : |
| 82 |
pathname === "/checkout/" || pathname === "/checkout" ? 'checkout' : 'unknown'; |
| 83 |
|
| 84 |
return { |
| 85 |
app: { |
| 86 |
name: '@route/woocommerce-widget', |
| 87 |
version: '1.0.0' |
| 88 |
}, |
| 89 |
cart: { |
| 90 |
id: cartData.cartId, |
| 91 |
subtotal: parseFloat(cartData.subtotal) |
| 92 |
}, |
| 93 |
widgetVersion: 'optimal', |
| 94 |
environment: cartData.environment, |
| 95 |
merchant: { |
| 96 |
id: cartData.merchantId |
| 97 |
}, |
| 98 |
pageType: pageType, |
| 99 |
platform: { |
| 100 |
id: 'woocommerce' |
| 101 |
}, |
| 102 |
premium: { |
| 103 |
amount: cartData.totalRouteFee || 0.98, |
| 104 |
currency: cartData.currency |
| 105 |
}, |
| 106 |
widget: { |
| 107 |
id: cartData.widgetId |
| 108 |
}, |
| 109 |
...additionalConfig |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Track widget render decision event |
| 115 |
*/ |
| 116 |
function trackWidgetRenderDecision() { |
| 117 |
if (!amplitudeInstance) { |
| 118 |
console.warn('Amplitude not initialized - skipping widget render tracking'); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
try { |
| 123 |
const attributes = getAnalyticsAttributes(); |
| 124 |
|
| 125 |
amplitudeInstance.track('widget_render_decision', attributes); |
| 126 |
} catch (error) { |
| 127 |
console.error('Failed to track widget render:', error); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Track widget info modal change event |
| 133 |
* |
| 134 |
* @param {string} action - Action type ('opened' or 'closed') |
| 135 |
*/ |
| 136 |
function trackWidgetInfoChange(action) { |
| 137 |
if (!amplitudeInstance) { |
| 138 |
console.warn('Amplitude not initialized - skipping info change tracking'); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
try { |
| 143 |
const attributes = getAnalyticsAttributes({ |
| 144 |
action: action, // 'opened' or 'closed' |
| 145 |
timestamp: new Date().toISOString() |
| 146 |
}); |
| 147 |
|
| 148 |
amplitudeInstance.track('widget_info_change', attributes); |
| 149 |
} catch (error) { |
| 150 |
console.error('Failed to track info change:', error); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Track protection toggle event |
| 156 |
* |
| 157 |
* @param {boolean} isProtected - Whether protection is enabled |
| 158 |
*/ |
| 159 |
function trackWidgetProtectChange(isProtected) { |
| 160 |
if (!amplitudeInstance) { |
| 161 |
console.warn('Amplitude not initialized - skipping protect change tracking'); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
try { |
| 166 |
const attributes = getAnalyticsAttributes({ |
| 167 |
protectionEnabled: isProtected, |
| 168 |
action: isProtected ? 'added' : 'removed', |
| 169 |
timestamp: new Date().toISOString() |
| 170 |
}); |
| 171 |
|
| 172 |
amplitudeInstance.track('widget_protect_change', attributes); |
| 173 |
} catch (error) { |
| 174 |
console.error('Failed to track protect change:', error); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Check if Amplitude is initialized and ready |
| 180 |
* |
| 181 |
* @returns {boolean} |
| 182 |
*/ |
| 183 |
function isInitialized() { |
| 184 |
return amplitudeInstance !== null && window.amplitudeInitialized === true; |
| 185 |
} |
| 186 |
|
| 187 |
// Export to window object |
| 188 |
window.RouteAmplitudeAnalytics = { |
| 189 |
initialize: initializeAmplitude, |
| 190 |
trackWidgetRender: trackWidgetRenderDecision, |
| 191 |
trackInfoChange: trackWidgetInfoChange, |
| 192 |
trackProtectChange: trackWidgetProtectChange, |
| 193 |
isInitialized: isInitialized |
| 194 |
}; |
| 195 |
|
| 196 |
})(jQuery, window); |
| 197 |
|
| 198 |
|