Skip to content

Commit

Permalink
fix(app): fix dev ODD system image build causing infinite spinner (#1…
Browse files Browse the repository at this point in the history
…7104)

Dev ODD system image builds have been failing. When digging into the Electron renderer process logs, this appears to be because of the way we inject the mixpanel config in our dev builds: it's not what mixpanel expects. During mixpanel initialization, if an unexpected configuration/id is supplied, certain methods on mixpanel are undefined. We've had similar issues like this in the past, and a more general-case solution to all of these issues is just to wrap the whole mixpanel initialization process in a try-catch block.
  • Loading branch information
mjhuff authored Dec 13, 2024
1 parent 352e4ac commit 8c43dcc
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 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 @@ -58,19 +66,23 @@ export function setMixpanelTracking(
): 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 +94,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 8c43dcc

Please sign in to comment.