import { Injectable } from '@angular/core' ; import { Title } from '@angular/platform-browser' ; import { NavigationEnd , NavigationError , NavigationStart , Router , } from '@angular/router' ; import { filter } from 'rxjs/operators' ; import { AppInsightsSettings } from '../../angulartics2-config' ; import { Angulartics2 } from '../../angulartics2-core' ; declare const appInsights ; export class AppInsightsDefaults implements AppInsightsSettings { userId = null ; } @ Injectable ( { providedIn : 'root' } ) export class Angulartics2AppInsights { loadStartTime : number = null ; loadTime : number = null ; metrics : { [ name ] : number ; } = null ; dimensions : { [ name ] : string ; } = null ; measurements : { [ name ] : number ; } = null ; constructor ( private angulartics2 , private title , private router ) { if ( typeof appInsights === 'undefined' ) { console . warn ( 'appInsights not found' ) ; } const defaults = new AppInsightsDefaults ( ) ; this . angulartics2 . settings . appInsights = { ... defaults , ... this . angulartics2 . settings . appInsights , } ; this . angulartics2 . setUsername . subscribe ( ( x ) => this . setUsername ( x ) ) ; this . angulartics2 . setUserProperties . subscribe ( ( x ) => this . setUserProperties ( x ) ) ; } startTracking ( ) { this . angulartics2 . pageTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . pageTrack ( x . path ) ) ; this . angulartics2 . eventTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . eventTrack ( x . action , x . properties ) ) ; this . angulartics2 . exceptionTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . exceptionTrack ( x ) ) ; this . router . events . pipe ( this . angulartics2 . filterDeveloperMode ( ) , filter ( ( event ) => event instanceof NavigationStart ) ) . subscribe ( ( event ) => this . startTimer ( ) ) ; this . router . events . pipe ( filter ( ( event ) => event instanceof NavigationError || event instanceof NavigationEnd ) ) . subscribe ( ( error ) => this . stopTimer ( ) ) ; } startTimer ( ) { this . loadStartTime = Date . now ( ) ; this . loadTime = null ; } stopTimer ( ) { this . loadTime = Date . now ( ) - this . loadStartTime ; this . loadStartTime = null ; } param path link pageTrack ( path ) { appInsights . trackPageView ( this . title . getTitle ( ) , path , this . dimensions , this . metrics , this . loadTime ) ; } param name param properties link eventTrack ( name , properties ) { appInsights . trackEvent ( name , properties , this . measurements ) ; } param properties link exceptionTrack ( properties ) { const description = properties . event || properties . description || properties ; appInsights . trackException ( description ) ; } link setUsername ( userId ) { this . angulartics2 . settings . appInsights . userId = userId ; appInsights . setAuthenticatedUserContext ( userId ) ; } setUserProperties ( properties ) { if ( properties . userId ) { this . angulartics2 . settings . appInsights . userId = properties . userId ; } if ( properties . accountId ) { appInsights . setAuthenticatedUserContext ( this . angulartics2 . settings . appInsights . userId , properties . accountId ) ; } else { appInsights . setAuthenticatedUserContext ( this . angulartics2 . settings . appInsights . userId ) ; } } }