Skip to content

Commit d602fd0

Browse files
author
Dominik
committed
included insights service
1 parent 23f339e commit d602fd0

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/src/app/app.component.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
2+
import { ApplicationInsightsService } from './services/common/application-insights.service';
3+
import { Router } from '@angular/router';
24

35
@Component({
46
selector: 'app-root',
57
templateUrl: './app.component.html',
68
styleUrls: ['./app.component.scss']
79
})
8-
export class AppComponent {
10+
export class AppComponent implements OnInit {
911
title = 'insights';
12+
private appInsights;
13+
14+
constructor(private router: Router) {
15+
this.appInsights = new ApplicationInsightsService(router);
16+
}
17+
18+
ngOnInit() {
19+
this.appInsights.loadAppInsights();
20+
}
1021
}

src/src/app/app.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NgModule } from '@angular/core';
33

44
import { AppRoutingModule } from './app-routing.module';
55
import { AppComponent } from './app.component';
6+
import { ApplicationInsightsService } from './services/common/application-insights.service';
67

78
@NgModule({
89
declarations: [
@@ -12,7 +13,9 @@ import { AppComponent } from './app.component';
1213
BrowserModule,
1314
AppRoutingModule
1415
],
15-
providers: [],
16+
providers: [
17+
ApplicationInsightsService
18+
],
1619
bootstrap: [AppComponent]
1720
})
1821
export class AppModule { }
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, OnInit } from '@angular/core';
2+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
3+
import { ActivatedRouteSnapshot, ResolveEnd, Router } from '@angular/router';
4+
import { filter } from 'rxjs/operators';
5+
import { Subscription } from 'rxjs';
26

37
@Injectable({
48
providedIn: 'root'
59
})
610
export class ApplicationInsightsService {
711

8-
constructor() { }
12+
private routerSubscription: Subscription;
13+
14+
private appInsights = new ApplicationInsights({
15+
config: {
16+
instrumentationKey: '...'
17+
}
18+
});
19+
20+
constructor(private router: Router) {
21+
this.appInsights.loadAppInsights();
22+
this.routerSubscription = this.router.events.pipe(filter(event => event instanceof ResolveEnd)).subscribe((event: ResolveEnd) => {
23+
const activatedComponent = this.getActivatedComponent(event.state.root);
24+
if (activatedComponent) {
25+
this.logPageView(`${activatedComponent.name} ${this.getRouteTemplate(event.state.root)}`, event.urlAfterRedirects);
26+
}
27+
});
28+
}
29+
30+
setUserId(userId: string) {
31+
this.appInsights.setAuthenticatedUserContext(userId);
32+
}
33+
34+
clearUserId() {
35+
this.appInsights.clearAuthenticatedUserContext();
36+
}
37+
38+
logPageView(name?: string, uri?: string) {
39+
this.appInsights.trackPageView({ name, uri });
40+
}
41+
42+
private getActivatedComponent(snapshot: ActivatedRouteSnapshot): any {
43+
if (snapshot.firstChild) {
44+
return this.getActivatedComponent(snapshot.firstChild);
45+
}
46+
47+
return snapshot.component;
48+
}
49+
50+
private getRouteTemplate(snapshot: ActivatedRouteSnapshot): string {
51+
let path = '';
52+
if (snapshot.routeConfig) {
53+
path += snapshot.routeConfig.path;
54+
}
55+
56+
if (snapshot.firstChild) {
57+
return path + this.getRouteTemplate(snapshot.firstChild);
58+
}
59+
60+
return path;
61+
}
962
}

0 commit comments

Comments
 (0)