import { ipcMain , BrowserWindow } from 'electron' ; import Analytics from 'electron-google-analytics' ; import { SettingsService } from './settings' ; export class AnalyticsService { private window : BrowserWindow ; private settings : SettingsService ; private analytics : any ; private clientID = "" ; private appVersion = "NA" ; private trackingID = "" ; private appName = "S3Uploader" ; constructor ( version , tid ) { this . appVersion = version ; this . trackingID = tid ; ipcMain . on ( 'Analytics-OptChanged' , ( event , arg ) => { this . initializeAnalytics ( arg . enabled ) ; } ) ; ipcMain . on ( 'Analytics-SetUserAgent' , ( event , arg ) => { this . setUserAgent ( arg . agent ) ; } ) ; ipcMain . on ( 'Analytics-ScreenView' , ( event , arg ) => { this . screenView ( arg . screenName ) ; } ) ; ipcMain . on ( 'Analytics-Exception' , ( event , arg ) => { this . exception ( arg . exceptionName , arg . fatal ) ; } ) ; ipcMain . on ( 'Analytics-Event' , ( event , arg ) => { this . logEvent ( arg . category , arg . action ) ; } ) ; } public initialize ( window , settings ) { this . window = window ; this . settings = settings ; } public initializeAnalytics ( enabled ) { if ( ! enabled ) { this . analytics = undefined ; } else { this . analytics = new Analytics ( this . trackingID ) ; this . analytics . set ( 'aip' , 1 ) ; this . clientID = this . settings . getSetting ( 'client-id' ) . toString ( ) ; this . analytics . set ( 'cid' , this . clientID ) ; } } public setUserAgent ( agent ) { if ( this . analytics && this . trackingID ) { this . analytics . set ( 'ua' , agent ) ; } } public screenView ( screenName ) { if ( this . analytics && this . trackingID ) { this . analytics . screen ( this . appName , this . appVersion , 'com.rohdiumcode.s3uploader' , 'com.rhodiumcode.s3uploader' , screenName ) ; } } public exception ( desc , fatal = false ) { if ( this . analytics && this . trackingID ) { this . analytics . exception ( desc , fatal ? 1 : 0 ) ; } } public logEvent ( category , action ) { if ( this . analytics && this . trackingID ) { this . analytics . event ( category , action ) ; } } }