-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dominik
committed
Jul 12, 2019
1 parent
23f339e
commit d602fd0
Showing
3 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { ApplicationInsightsService } from './services/common/application-insights.service'; | ||
import { Router } from '@angular/router'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.scss'] | ||
}) | ||
export class AppComponent { | ||
export class AppComponent implements OnInit { | ||
title = 'insights'; | ||
private appInsights; | ||
|
||
constructor(private router: Router) { | ||
this.appInsights = new ApplicationInsightsService(router); | ||
} | ||
|
||
ngOnInit() { | ||
this.appInsights.loadAppInsights(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 55 additions & 2 deletions
57
src/src/app/services/common/application-insights.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,62 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Injectable, OnInit } from '@angular/core'; | ||
import { ApplicationInsights } from '@microsoft/applicationinsights-web'; | ||
import { ActivatedRouteSnapshot, ResolveEnd, Router } from '@angular/router'; | ||
import { filter } from 'rxjs/operators'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ApplicationInsightsService { | ||
|
||
constructor() { } | ||
private routerSubscription: Subscription; | ||
|
||
private appInsights = new ApplicationInsights({ | ||
config: { | ||
instrumentationKey: '...' | ||
} | ||
}); | ||
|
||
constructor(private router: Router) { | ||
this.appInsights.loadAppInsights(); | ||
this.routerSubscription = this.router.events.pipe(filter(event => event instanceof ResolveEnd)).subscribe((event: ResolveEnd) => { | ||
const activatedComponent = this.getActivatedComponent(event.state.root); | ||
if (activatedComponent) { | ||
this.logPageView(`${activatedComponent.name} ${this.getRouteTemplate(event.state.root)}`, event.urlAfterRedirects); | ||
} | ||
}); | ||
} | ||
|
||
setUserId(userId: string) { | ||
this.appInsights.setAuthenticatedUserContext(userId); | ||
} | ||
|
||
clearUserId() { | ||
this.appInsights.clearAuthenticatedUserContext(); | ||
} | ||
|
||
logPageView(name?: string, uri?: string) { | ||
this.appInsights.trackPageView({ name, uri }); | ||
} | ||
|
||
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; | ||
} | ||
} |