import { Injectable } from '@angular/core'; import { Router, ResolveEnd, ActivatedRouteSnapshot } from '@angular/router'; import { ApplicationInsights } from '@microsoft/applicationinsights-web'; import { filter, map } from 'rxjs/operators'; import { environment } from 'projects/core/src//environment'; import { MwUserService } from 'projects/core/src//auth'; import { AngularPlugin } from '@microsoft/applicationinsights-angularplugin-js'; @Injectable() export class MwMonitorService { private appInsightKeySet = !!environment.applicationInsightKey; private angularPlugin = new AngularPlugin(); appInsights: ApplicationInsights | any; constructor(private router: Router, private userService: MwUserService) { if (this.appInsightKeySet) { this.appInsights = new ApplicationInsights({ config: { instrumentationKey: environment.applicationInsightKey, extensions: [this.angularPlugin], extensionConfig: { [this.angularPlugin.identifier]: { router: this.router }, }, }, }); this.appInsights.loadAppInsights(); } this.router.events .pipe( filter((event) => event instanceof ResolveEnd), map((event) => event as ResolveEnd) ) .subscribe((event) => { const activatedComponent = this.getActivatedComponent(event.state.root); if (activatedComponent) { this.logPageView( `${activatedComponent.name} ${this.getRouteTemplate( event.state.root )}`, event.urlAfterRedirects ); } }); this.userService.userProfile$.subscribe((userProfile) => { if (userProfile) { this.setUserId(userProfile.userName); } else { this.clearUserId(); } }); } setUserId(userId: string): void { if (this.appInsightKeySet) { this.appInsights.setAuthenticatedUserContext(userId); } } clearUserId(): void { if (this.appInsightKeySet) { this.appInsights.clearAuthenticatedUserContext(); } } public logPageView( name: string, url?: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number } ): void { if (this.appInsightKeySet) { this.appInsights.trackPageView({ name, uri: url, properties: this.addGlobalProperties(properties), measurements, }); } } private getActivatedComponent(snapshot: ActivatedRouteSnapshot): any { if (snapshot.firstChild) { return this.getActivatedComponent(snapshot.firstChild); } return snapshot.component; } private getRouteTemplate(snapshot: ActivatedRouteSnapshot): string { let path = ''; if (snapshot.routeConfig) { path += snapshot.routeConfig.path; } if (snapshot.firstChild) { return path + this.getRouteTemplate(snapshot.firstChild); } return path; } private addGlobalProperties(properties?: { [key: string]: string }): { [key: string]: string; } { if (!properties) { properties = {}; } // add your custom properties such as app version return properties; } public logEvent( name: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number } ): void { if (this.appInsightKeySet) { this.appInsights.trackEvent({ name, properties: this.addGlobalProperties(properties), measurements, }); } } public logError(error: Error, properties?: { [key: string]: string }): void { if (this.appInsightKeySet) { this.appInsights.trackException({ exception: error, }); } } }