import mixpanel from 'mixpanel-browser' ; import { createLogger } from '../../logger' ; import { CURRENT_VERSION } from '../shell' ; import type { AnalyticsEvent , AnalyticsConfig } from './types' ; const log = createLogger ( __filename ) ; const MIXPANEL_ID = process . env . OT_APP_MIXPANEL_ID ; const MIXPANEL_OPTS = { opt_out_tracking_by_default : true , disable_persistence : true , track_pageview : false , } ; export function initializeMixpanel ( config ) { if ( MIXPANEL_ID ) { log . debug ( 'Initializing Mixpanel' , { config } ) ; mixpanel . init ( MIXPANEL_ID , MIXPANEL_OPTS ) ; setMixpanelTracking ( config ) ; trackEvent ( { name : 'appOpen' , properties : { } } , config ) ; } else { log . warn ( 'MIXPANEL_ID not found; this is a bug if build is production' ) ; } } export function trackEvent ( event , config ) { const { optedIn } = config ; log . debug ( 'Trackable event' , { event , optedIn } ) ; if ( MIXPANEL_ID && optedIn ) { if ( event . superProperties ) mixpanel . register ( event . superProperties ) ; if ( event . name ) mixpanel . track ( event . name , event . properties ) ; } } export function setMixpanelTracking ( config ) { if ( MIXPANEL_ID ) { 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 } ) ; } else { log . debug ( 'User has opted out of analytics; stopping tracking' ) ; mixpanel . opt_out_tracking ( ) ; mixpanel . reset ( ) ; } } }