import ua from 'universal-analytics' ; import { state } from '@lib/state' ; function initializeTracker ( ) { return ua ( __ANALYTICS_ID__ , state . get ( 'userid' ) ) ; } let trackerPromise = null ; function getTracker ( ) { if ( trackerPromise ) { return trackerPromise ; } trackerPromise = new Promise < any > ( ( resolve , reject ) => { try { resolve ( initializeTracker ( ) ) ; } catch ( err ) { reject ( err ) ; } } ) ; return trackerPromise ; } class Track { protected async send ( ... params ) { try { const method = params . shift ( ) ; if ( __DEV__ || process . env . NODE_ENV !== 'production' ) { console . log ( 'Tracking [' + method + ']' , params . join ( ', ' ) ) ; return ; } const tracker = await getTracker ( ) ; await new Promise < void > ( ( resolve , reject ) => { tracker [ method ! ] ( ... params ) . send ( ) ; resolve ( ) ; } ) ; } catch ( error ) { } } public async screenview ( ... params ) { return this . send ( 'screenview' , ... params ) ; } public async event ( ... params ) { return this . send ( 'event' , ... params ) ; } } export const track = new Track ( ) ;