|
| 1 | +/** |
| 2 | + * Simple Google Analytics 4 Measurement Protocol Service |
| 3 | + * Tracks: extension opens, feature navigation, popup link clicks |
| 4 | + */ |
| 5 | + |
| 6 | +// GA4 Configuration - Set via environment variables |
| 7 | +const GA_MEASUREMENT_ID = process.env.REACT_APP_GA_MEASUREMENT_ID || ''; |
| 8 | +const GA_API_SECRET = process.env.REACT_APP_GA_API_SECRET || ''; |
| 9 | + |
| 10 | +const GA_ENDPOINT = `https://www.google-analytics.com/mp/collect?measurement_id=${GA_MEASUREMENT_ID}&api_secret=${GA_API_SECRET}`; |
| 11 | + |
| 12 | +const CLIENT_ID_KEY = 'sp_editor_ga_client_id'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Get or generate a persistent client ID |
| 16 | + */ |
| 17 | +async function getClientId(): Promise<string> { |
| 18 | + return new Promise((resolve) => { |
| 19 | + if (typeof chrome !== 'undefined' && chrome.storage?.local) { |
| 20 | + chrome.storage.local.get([CLIENT_ID_KEY], (result) => { |
| 21 | + if (result[CLIENT_ID_KEY]) { |
| 22 | + resolve(result[CLIENT_ID_KEY]); |
| 23 | + } else { |
| 24 | + const newClientId = crypto.randomUUID(); |
| 25 | + chrome.storage.local.set({ [CLIENT_ID_KEY]: newClientId }); |
| 26 | + resolve(newClientId); |
| 27 | + } |
| 28 | + }); |
| 29 | + } else { |
| 30 | + // Fallback for development |
| 31 | + let clientId = localStorage.getItem(CLIENT_ID_KEY); |
| 32 | + if (!clientId) { |
| 33 | + clientId = crypto.randomUUID(); |
| 34 | + localStorage.setItem(CLIENT_ID_KEY, clientId); |
| 35 | + } |
| 36 | + resolve(clientId); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Get the extension ID and browser type |
| 43 | + */ |
| 44 | +function getExtensionInfo(): { extensionId: string; browser: string } { |
| 45 | + const extensionId = typeof chrome !== 'undefined' && chrome.runtime?.id |
| 46 | + ? chrome.runtime.id |
| 47 | + : 'development'; |
| 48 | + |
| 49 | + // Detect browser based on user agent |
| 50 | + const ua = navigator.userAgent; |
| 51 | + let browser = 'unknown'; |
| 52 | + if (ua.includes('Edg/')) { |
| 53 | + browser = 'edge'; |
| 54 | + } else if (ua.includes('Chrome/')) { |
| 55 | + browser = 'chrome'; |
| 56 | + } |
| 57 | + |
| 58 | + return { extensionId, browser }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Send event to GA4 |
| 63 | + */ |
| 64 | +async function sendEvent(eventName: string, params: Record<string, string | number> = {}): Promise<void> { |
| 65 | + try { |
| 66 | + const clientId = await getClientId(); |
| 67 | + const { extensionId, browser } = getExtensionInfo(); |
| 68 | + |
| 69 | + const payload = { |
| 70 | + client_id: clientId, |
| 71 | + events: [{ |
| 72 | + name: eventName, |
| 73 | + params: { |
| 74 | + ...params, |
| 75 | + extension_id: extensionId, |
| 76 | + browser: browser, |
| 77 | + page_location: `chrome-extension://${extensionId}`, |
| 78 | + engagement_time_msec: 100, |
| 79 | + } |
| 80 | + }] |
| 81 | + }; |
| 82 | + |
| 83 | + if (navigator.sendBeacon) { |
| 84 | + navigator.sendBeacon(GA_ENDPOINT, JSON.stringify(payload)); |
| 85 | + } else { |
| 86 | + fetch(GA_ENDPOINT, { |
| 87 | + method: 'POST', |
| 88 | + body: JSON.stringify(payload), |
| 89 | + keepalive: true, |
| 90 | + }); |
| 91 | + } |
| 92 | + } catch { |
| 93 | + // Silently fail - analytics should never break the app |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// ============================================================================ |
| 98 | +// Public API |
| 99 | +// ============================================================================ |
| 100 | + |
| 101 | +/** |
| 102 | + * Track when DevTools extension is opened |
| 103 | + */ |
| 104 | +export function trackDevToolsOpen(): void { |
| 105 | + sendEvent('devtools_open'); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Track when Popup extension is opened |
| 110 | + */ |
| 111 | +export function trackPopupOpen(): void { |
| 112 | + sendEvent('popup_open'); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Track navigation to a feature in DevTools |
| 117 | + * @param featureName - The feature being accessed (e.g., 'pnpjsconsole', 'search') |
| 118 | + * @param featureTitle - Human readable title (e.g., 'PnP JS Console') |
| 119 | + */ |
| 120 | +export function trackFeatureNavigation(featureName: string, featureTitle: string): void { |
| 121 | + sendEvent('feature_view', { |
| 122 | + feature_name: featureName, |
| 123 | + feature_title: featureTitle, |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Track link click in Popup |
| 129 | + * @param linkName - The link identifier (e.g., 'admin_center', 'site_settings') |
| 130 | + */ |
| 131 | +export function trackPopupLinkClick(linkName: string): void { |
| 132 | + sendEvent('popup_link_click', { |
| 133 | + link_name: linkName, |
| 134 | + }); |
| 135 | +} |
0 commit comments