Skip to content

Commit

Permalink
included insights service
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik committed Jul 12, 2019
1 parent 23f339e commit d602fd0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/src/app/app.component.ts
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();
}
}
5 changes: 4 additions & 1 deletion src/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NgModule } from '@angular/core';

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

@NgModule({
declarations: [
Expand All @@ -12,7 +13,9 @@ import { AppComponent } from './app.component';
BrowserModule,
AppRoutingModule
],
providers: [],
providers: [
ApplicationInsightsService
],
bootstrap: [AppComponent]
})
export class AppModule { }
57 changes: 55 additions & 2 deletions src/src/app/services/common/application-insights.service.ts
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;
}
}

0 comments on commit d602fd0

Please sign in to comment.