import Analytics from 'analytics-node' ; import { machineId } from 'node-machine-id' ; import { BudgetTrackingEventEmitter , EventNames } from '@/backend/eventEmitters/EventEmitter' ; const analytics = SEGMENT_WRITE_KEY ? new Analytics ( SEGMENT_WRITE_KEY ) : null ; type EventProperties = Record < string , string | number | boolean | undefined > ; const EVENTS_TO_TRACK = [ EventNames . IMPORT_PROCESS_START , EventNames . IMPORTER_START , EventNames . IMPORTER_ERROR , EventNames . IMPORTER_END , EventNames . IMPORT_PROCESS_END , EventNames . EXPORT_PROCESS_START , EventNames . EXPORTER_START , EventNames . EXPORTER_ERROR , EventNames . EXPORTER_END , EventNames . EXPORT_PROCESS_END , EventNames . GENERAL_ERROR , ] ; export async function trackPage ( pageName , properties ? ) { const event = await buildEvent ( properties ) ; analytics ?. page ( { name : pageName , ... event } ) ; } export async function trackEvent ( eventType , properties ? ) { const event = await buildEvent ( properties ) ; analytics ?. track ( { ... event , event : eventType } ) ; } export async function initAnalyticsEventHandling ( eventEmitter ) { eventEmitter . onAny ( ( ( eventName , eventData ) => { if ( EVENTS_TO_TRACK . includes ( eventName ) ) { trackEvent ( eventName . toString ( ) , { name : eventData ?. vendorId , accountType : eventData ?. accountType , isError : ! ! eventData ?. error } ) ; } } ) ) ; } async function buildEvent ( properties ? ) { const id = await getMachineId ( ) ; return { anonymousId : id , properties : { env : process . env . NODE_ENV , ... properties } } ; } let machineIdCached ; async function getMachineId ( ) { if ( machineIdCached ) { return machineIdCached ; } machineIdCached = await machineId ( ) ; return machineIdCached ; }