|
| 1 | +import { browser } from '$app/environment'; |
| 2 | +import type { BehaviorEvent } from '$lib/types'; |
| 3 | +import mixpanel from 'mixpanel-browser'; |
| 4 | +import { analyticsEnabled } from '../stores/analytics'; |
| 5 | +import { minutesToMilliSeconds } from '$lib/utils'; |
| 6 | +import type { MCUser } from '$lib/mermaidChartApi'; |
| 7 | +import log from '$lib/log'; |
| 8 | + |
| 9 | +let initialized = false; |
| 10 | + |
| 11 | +interface QueueItem { |
| 12 | + description: string; |
| 13 | + event: BehaviorEvent; |
| 14 | +} |
| 15 | + |
| 16 | +const MAX_QUEUE_SIZE = 100; |
| 17 | +const queue: QueueItem[] = []; |
| 18 | + |
| 19 | +function sendQueue() { |
| 20 | + if (queue.length === 0 || !initialized) { |
| 21 | + return; |
| 22 | + } |
| 23 | + log.info('Sending mixpanel events'); |
| 24 | + for (const { description, event } of queue) { |
| 25 | + try { |
| 26 | + mixpanel.track(description, event); |
| 27 | + } catch (error) { |
| 28 | + log.error(error); |
| 29 | + } |
| 30 | + } |
| 31 | + queue.length = 0; |
| 32 | +} |
| 33 | + |
| 34 | +function init(mixPanelProject: string, sessionID: string, session: MCUser) { |
| 35 | + if (initialized) { |
| 36 | + return; |
| 37 | + } |
| 38 | + // Execute code that sets marketing cookies |
| 39 | + mixpanel.init(mixPanelProject, { debug: true, ignore_dnt: true }); |
| 40 | + |
| 41 | + mixpanel.identify(session?.analyticsID || sessionID); |
| 42 | + mixpanel.people.set({ |
| 43 | + $email: session.emailAddress, |
| 44 | + $name: session.fullName, |
| 45 | + tier: session.subscriptionTier, |
| 46 | + AllowProductEmail: session.allowProductEmail |
| 47 | + }); |
| 48 | + initialized = true; |
| 49 | + sendQueue(); |
| 50 | +} |
| 51 | + |
| 52 | +export function initializeMixPanel(mixPanelProject: string, sessionID: string, session: MCUser) { |
| 53 | + if (!browser || !mixPanelProject) { |
| 54 | + return; |
| 55 | + } |
| 56 | + analyticsEnabled.subscribe((enabled) => { |
| 57 | + // This will initialize MixPanel only after the user has accepted analytics cookies |
| 58 | + if (enabled) { |
| 59 | + init(mixPanelProject, sessionID, session); |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function sendEvent(description: string, event: BehaviorEvent) { |
| 65 | + // Avoid queueing too many events |
| 66 | + if (queue.length < MAX_QUEUE_SIZE) { |
| 67 | + queue.push({ description, event }); |
| 68 | + } |
| 69 | + sendQueue(); |
| 70 | +} |
| 71 | + |
| 72 | +const delaysPerEvent: Record<string, number> = { |
| 73 | + DIAGRAM_TYPE: minutesToMilliSeconds(1) |
| 74 | +}; |
| 75 | +const timeouts: Record<string, number> = {}; |
| 76 | + |
| 77 | +export function sendBehaviorEvent(description: string, event: BehaviorEvent) { |
| 78 | + if (timeouts[event.eventID]) { |
| 79 | + clearTimeout(timeouts[event.eventID]); |
| 80 | + } |
| 81 | + if (delaysPerEvent[event.eventID] === undefined) { |
| 82 | + sendEvent(description, event); |
| 83 | + } else { |
| 84 | + timeouts[event.eventID] = window.setTimeout(() => { |
| 85 | + sendEvent(description, event); |
| 86 | + delete timeouts[event.eventID]; |
| 87 | + }, delaysPerEvent[event.eventID]); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function clear() { |
| 92 | + if (browser && initialized) { |
| 93 | + mixpanel.reset(); |
| 94 | + } |
| 95 | +} |
0 commit comments