import * as MixpanelInstance from 'mixpanel' ; import { Mixpanel } from 'mixpanel' ; import { UserEntity } from '@notifire/dal' ; export class AnalyticsService { private mixpanel : Mixpanel ; async initialize ( ) { if ( process . env . MIXPANEL_TOKEN ) { this . mixpanel = MixpanelInstance . init ( process . env . MIXPANEL_TOKEN ) ; } } alias ( distinctId , userId ) { if ( ! this . analyticsEnabled ) return ; this . mixpanel . alias ( distinctId , userId ) ; } upsertUser ( user , distinctId ) { if ( ! this . analyticsEnabled ) return ; this . mixpanel . people . set ( distinctId , { $first_name : user . firstName , $last_name : user . lastName , $created : user . createdAt || new Date ( ) , $email : user . email , userId : user . _id , } ) ; } setValue ( userId , propertyName , value ) { if ( ! this . analyticsEnabled ) return ; this . mixpanel . people . set ( userId , propertyName , value ) ; } track ( name , userId , data = { } ) { if ( ! this . analyticsEnabled ) return ; this . mixpanel . track ( name , { distinct_id : userId , ... data , } ) ; } private get analyticsEnabled ( ) { return process . env . NODE_ENV !== 'test' && this . mixpanel ; } }