Skip to content

Commit

Permalink
fix(app): fix dev ODD system image build crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Dec 13, 2024
1 parent a0bd4ee commit e0a4450
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions app/src/redux/analytics/mixpanel.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// mixpanel actions
import mixpanel from 'mixpanel-browser'

import { createLogger } from '../../logger'
import { createLogger } from '/app/logger'
import { CURRENT_VERSION } from '../shell'

import type { Config as MixpanelConfig } from 'mixpanel-browser'
import type { AnalyticsEvent, AnalyticsConfig } from './types'

const log = createLogger(new URL('', import.meta.url).pathname)

// pulled in from environment at build time
const MIXPANEL_ID = process.env.OT_APP_MIXPANEL_ID

const MIXPANEL_OPTS = {
const MIXPANEL_OPTS: Partial<MixpanelConfig> = {
// opt out by default
opt_out_tracking_by_default: true,
// user details are persisted in our own config store
Expand All @@ -27,9 +27,13 @@ export function initializeMixpanel(
isOnDevice: boolean | null
): void {
if (MIXPANEL_ID != null) {
initMixpanelInstanceOnce(config)
setMixpanelTracking(config, isOnDevice)
trackEvent({ name: 'appOpen', properties: {} }, config)
try {
initMixpanelInstanceOnce(config)
setMixpanelTracking(config, isOnDevice)
trackEvent({ name: 'appOpen', properties: {} }, config)
} catch (error) {
console.error('Failed to initialize Mixpanel:', error)
}
} else {
log.warn('MIXPANEL_ID not found; this is a bug if build is production')
}
Expand All @@ -43,11 +47,15 @@ export function trackEvent(

log.debug('Trackable event', { event, optedIn })
if (MIXPANEL_ID != null && optedIn) {
if (event.superProperties != null) {
mixpanel.register(event.superProperties)
}
if ('name' in event && event.name != null) {
mixpanel.track(event.name, event.properties)
try {
if (event.superProperties != null) {
mixpanel.register(event.superProperties)
}
if ('name' in event && event.name != null) {
mixpanel.track(event.name, event.properties)
}
} catch (error) {
console.error('Failed to track event:', error)
}
}
}
Expand All @@ -57,20 +65,23 @@ export function setMixpanelTracking(
isOnDevice: boolean | null
): void {
if (MIXPANEL_ID != null) {
initMixpanelInstanceOnce(config)
if (config.optedIn) {
log.debug('User has opted into analytics; tracking with Mixpanel')
mixpanel.identify(config.appId)
mixpanel.opt_in_tracking()
mixpanel.register({
appVersion: CURRENT_VERSION,
appId: config.appId,
appMode: Boolean(isOnDevice) ? 'ODD' : 'Desktop',
})
} else {
log.debug('User has opted out of analytics; stopping tracking')
mixpanel.opt_out_tracking?.()
mixpanel.reset?.()
try {
if (config.optedIn) {
log.debug('User has opted into analytics; tracking with Mixpanel')
mixpanel.identify(config.appId)
mixpanel.opt_in_tracking()
mixpanel.register({
appVersion: CURRENT_VERSION,
appId: config.appId,
appMode: Boolean(isOnDevice) ? 'ODD' : 'Desktop',
})
} else {
log.debug('User has opted out of analytics; stopping tracking')
mixpanel.opt_out_tracking()
mixpanel.reset()
}
} catch (error) {
console.error('Failed to set Mixpanel tracking:', error)
}
}
}
Expand All @@ -82,9 +93,13 @@ function initializeMixpanelInstanceOnce(

return function (config: AnalyticsConfig): undefined {
if (!hasBeenInitialized && MIXPANEL_ID != null) {
hasBeenInitialized = true
log.debug('Initializing Mixpanel', { config })
mixpanel.init(MIXPANEL_ID, MIXPANEL_OPTS)
try {
hasBeenInitialized = true
log.debug('Initializing Mixpanel', { config })
mixpanel.init(MIXPANEL_ID, MIXPANEL_OPTS)
} catch (error) {
console.error('Failed to initialize Mixpanel instance:', error)
}
}
}
}

0 comments on commit e0a4450

Please sign in to comment.