import { Injectable } from '@angular/core' ; import { ApplicationInsights } from '@microsoft/applicationinsights-web' ; import { StorageService } from './storage.service' ; import { environment } from 'src/environments/environment' ; import { Router , NavigationEnd , ActivationEnd } from '@angular/router' ; import { StringUtils } from '../Utils/StringUtils' ; @ Injectable ( { providedIn : 'root' } ) export class TelemetryService { constructor ( private storage , public routing ) { this . telemetryEnabled = this . storage . getValueBoolean ( TelemetryService . localStorageKey , true ) ; this . appInsights = new ApplicationInsights ( { config : { instrumentationKey : environment . telemetryKey , isCookieUseDisabled : true , disableAjaxTracking : true , disableFetchTracking : true , enableAutoRouteTracking : true , disableTelemetry : ! this . telemetryEnabled || ! environment . telemetryKey } } ) ; this . appInsights . loadAppInsights ( ) ; this . appInsights . context . application . ver = environment . version ; let lastActivationEnd = null ; this . routing . events . subscribe ( event => { if ( event instanceof ActivationEnd ) { lastActivationEnd = event ; } if ( event instanceof NavigationEnd ) { try { let name = '' ; let snapshot = lastActivationEnd . snapshot ; while ( snapshot ) { const path = snapshot . routeConfig . path ; if ( path . length > 0 ) { name += StringUtils . EnsureStartsWith ( snapshot . routeConfig . path , '/' ) ; } snapshot = snapshot . firstChild ; } this . trackPageEvent ( name ) ; } catch { } lastActivationEnd = null ; } } ) ; } static readonly localStorageKey = 'sfx-telemetry-enabled' ; static readonly localStoragePromptedTelemetryKey = 'sfx-telemetry-prompted' ; private uniqueEmitCache : Set < string > = new Set ( ) ; appInsights : ApplicationInsights ; telemetryEnabled = true ; public SetTelemetry ( state ) { this . storage . setValue ( TelemetryService . localStorageKey , state ) ; this . telemetryEnabled = state ; this . appInsights . config . disableTelemetry = ! state ; } trackActionEvent ( name , data , uniqueSessionId ? ) { if ( uniqueSessionId && this . uniqueEmitCache . has ( uniqueSessionId ) ) { return ; } else { this . uniqueEmitCache . add ( uniqueSessionId ) ; } this . appInsights . trackEvent ( { name } , data ) ; } trackPageEvent ( name ) { this . appInsights . trackPageView ( { name } ) ; } }