import { Injectable , Optional , Injector } from '@angular/core' ; import { Router , NavigationStart , NavigationEnd , NavigationCancel , NavigationError } from '@angular/router' ; import { AppInsights } from 'applicationinsights-js' ; import { filter } from 'rxjs/operators' ; import IAppInsights = Microsoft . ApplicationInsights . IAppInsights ; export enum SeverityLevel { Verbose = 0 , Information = 1 , Warning = 2 , Error = 3 , Critical = 4 } export class AppInsightsConfig implements Microsoft . ApplicationInsights . IConfig { instrumentationKeySetLater ? : boolean ; instrumentationKeySetlater ? : boolean ; instrumentationKey ? : string ; endpointUrl ? : string ; emitLineDelimitedJson ? : boolean ; accountId ? : string ; sessionRenewalMs ? : number ; sessionExpirationMs ? : number ; maxBatchSizeInBytes ? : number ; maxBatchInterval ? : number ; enableDebug ? : boolean ; disableExceptionTracking ? : boolean ; disableTelemetry ? : boolean ; verboseLogging ? : boolean ; diagnosticLogInterval ? : number ; samplingPercentage ? : number ; autoTrackPageVisitTime ? : boolean ; disableAjaxTracking ? : boolean ; overridePageViewDuration ? : boolean ; maxAjaxCallsPerView ? : number ; disableDataLossAnalysis ? : boolean ; disableCorrelationHeaders ? : boolean ; correlationHeaderExcludedDomains ? : string [ ] ; disableFlushOnBeforeUnload ? : boolean ; enableSessionStorageBuffer ? : boolean ; isCookieUseDisabled ? : boolean ; cookieDomain ? : string ; isRetryDisabled ? : boolean ; url ? : string ; isStorageUseDisabled ? : boolean ; isBeaconApiDisabled ? : boolean ; sdkExtension ? : string ; isBrowserLinkTrackingEnabled ? : boolean ; appId ? : string ; enableCorsCorrelation ? : boolean ; namePrefix ? : string ; overrideTrackPageMetrics ? : boolean ; enableAutoRouteTracking ? : boolean ; enableRequestHeaderTracking ? : boolean ; enableResponseHeaderTracking ? : boolean ; enableAjaxErrorStatusText ? : boolean ; enableAjaxPerfTracking ? : boolean ; maxAjaxPerfLookupAttempts ? : number ; ajaxPerfLookupDelay ? : number ; distributedTracingMode ? : boolean ; enableUnhandledPromiseRejectionTracking ? : boolean ; disableInstrumentaionKeyValidation ? : boolean ; enablePerfMgr ? : boolean ; perfEvtsSendAll ? : boolean ; } @ Injectable ( ) export class AppInsightsService implements IAppInsights { get context ( ) : Microsoft . ApplicationInsights . ITelemetryContext { return AppInsights . context ; } get queue ( ) : Array < ( ) => void > { return AppInsights . queue ; } config : AppInsightsConfig ; constructor ( @ Optional ( ) _config , private _injector ) { this . config = _config ; } param name param properties param measurements trackEvent ( eventName , eventProperties ? , metricProperty ? ) { try { AppInsights . trackEvent ( eventName , eventProperties , metricProperty ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackEvent]: ' , ex ) ; } } /**
     * Start timing an extended event. Call  stopTrackEvent to log the event when it ends.
     *  param name startTrackEvent ( name ) { try { AppInsights . startTrackEvent ( name ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [startTrackEvent]: ' , ex ) ; } } /**
     * Log an extended event that you started timing with  startTrackEvent .
     *  param name param properties param measurements stopTrackEvent ( name , properties ? , measurements ? ) { try { AppInsights . stopTrackEvent ( name , properties , measurements ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [stopTrackEvent]: ' , ex ) ; } } param name param url param properties param measurements param duration trackPageView ( name ? , url ? , properties ? , measurements ? , duration ? ) { try { AppInsights . trackPageView ( name , url , properties , measurements , duration ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackPageView]: ' , ex ) ; } } /**
     * Starts timing how long the user views a page or other item. Call this when the page opens.
     * This method doesn't send any telemetry. Call  stopTrackTelemetry to log the page when it closes.
     *  param name startTrackPage ( name ? ) { try { AppInsights . startTrackPage ( name ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [startTrackPage]: ' , ex ) ; } } /**
     * Logs how long a page or other item was visible, after  startTrackPage . Call this when the page closes.
     *  param name param url param properties param measurements stopTrackPage ( name ? , url ? , properties ? , measurements ? ) { try { AppInsights . stopTrackPage ( name , url , properties , measurements ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [stopTrackPage]: ' , ex ) ; } } param name param average param sampleCount param min param max trackMetric ( name , average , sampleCount ? , min ? , max ? , properties ? ) { try { AppInsights . trackMetric ( name , average , sampleCount , min , max , properties ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackTrace]: ' , ex ) ; } } param exception param properties param measurements param severityLevel trackException ( exception , handledAt ? , properties ? , measurements ? , severityLevel ? ) { try { AppInsights . trackException ( exception , handledAt , properties , measurements , severityLevel ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackException]: ' , ex ) ; } } param message param properties trackTrace ( message , properties ? , severityLevel ? ) { try { AppInsights . trackTrace ( message , properties , severityLevel ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackTrace]: ' , ex ) ; } } param id param method param absoluteUrl param pathName param totalTime param success param resultCode param properties param measurements trackDependency ( id , method , absoluteUrl , pathName , totalTime , success , resultCode , properties ? , measurements ? ) { try { AppInsights . trackDependency ( id , method , absoluteUrl , pathName , totalTime , success , resultCode , properties , measurements ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [trackDependency]: ' , ex ) ; } } flush ( ) { try { AppInsights . flush ( ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [flush]: ' , ex ) ; } } param authenticatedUserId { string } param accountId { string } param storeInCookie { boolean } setAuthenticatedUserContext ( authenticatedUserId , accountId ? , storeInCookie = false ) { try { AppInsights . setAuthenticatedUserContext ( authenticatedUserId , accountId , storeInCookie ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [setAuthenticatedUserContext]: ' , ex ) ; } } /**
     * Clears the authenticated user id and the account id from the user context.
     */ clearAuthenticatedUserContext ( ) { try { AppInsights . clearAuthenticatedUserContext ( ) ; } catch ( ex ) { console . warn ( 'Angular application insights Error [clearAuthenticatedUserContext]: ' , ex ) ; } } _onerror ( message ) { console . warn ( 'Angular application insights Error [_onerror]: ' , message ) ; } /**
     * Initialize Application Insights for Angular
     * Make sure your config{} has been set
     */ public init ( ) { if ( this . config ) { if ( this . config . instrumentationKeySetlater ) { console . warn ( `\n\n
          Warning: [instrumentationKeySetlater] will soon be deprecated.\n
          Use .instrumentationKeySetLater (capital "L" in "L"ater) instead
          to prevent any possible errors in the future!
          \n\n` ) ; } if ( this . config . instrumentationKey ) { try { AppInsights . downloadAndSetup ( this . config ) ; if ( ! this . config . overrideTrackPageMetrics && this . router ) { this . router . events . pipe ( filter ( event => event instanceof NavigationStart ) ) . subscribe ( ( event ) => { this . startTrackPage ( event . url ) ; } ) ; this . router . events . pipe ( filter ( event => ( event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError ) ) ) . subscribe ( ( event ) => { this . stopTrackPage ( event . url ) ; } ) ; } } catch ( ex ) { console . warn ( 'Angular application insights Error [downloadAndSetup]: ' , ex ) ; } } else { if ( ! this . config . instrumentationKeySetLater && ! this . config . instrumentationKeySetlater ) { console . warn ( 'An instrumentationKey value is required to initialize AppInsightsService' ) ; } } } else { console . warn ( 'You need forRoot on ApplicationInsightsModule, with or instrumentationKeySetLater or instrumentationKey set at least' ) ; } } private get router ( ) { try { return this . _injector . get ( Router ) ; } catch ( ex ) { return null ; } } }