Skip to content

Commit d11b767

Browse files
authored
Merge pull request TrilonIO#4 from KiselN/feature/page-tracking
feature(page-tracking) implemented page tracking with angular router
2 parents 26455f6 + a668db0 commit d11b767

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
},
2424
"main": "./dist/index.js",
2525
"dependencies": {
26+
"@angular/router": "^3.4.5",
2627
"@types/applicationinsights-js": "^0.23.5",
27-
"applicationinsights-js": "^1.0.7"
28+
"applicationinsights-js": "^1.0.7",
29+
"rxjs": "^5.0.3"
2830
},
2931
"devDependencies": {
3032
"@angular/core": "^2.0.0",
3133
"@angular/common": "^2.0.0",
3234
"codelyzer": "^0.0.28",
33-
"rxjs": "^5.0.1",
3435
"tslint": "^3.15.1",
3536
"typescript": "2.0.2",
3637
"zone.js": "0.7.2",

src/app-insight.service.ts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Injectable, Inject } from '@angular/core';
2+
import { Router, NavigationStart, NavigationEnd } from '@angular/router';
23
import { AppInsights } from 'applicationinsights-js';
34
import { APP_INSIGHT_ID, APP_NAME } from './app-insight.config';
5+
import 'rxjs/add/operator/filter';
46

57
@Injectable()
68
export class AppInsightsService {
79

810
constructor(
911
@Inject(APP_INSIGHT_ID) public appID: string,
10-
@Inject(APP_NAME) public appName: string
12+
@Inject(APP_NAME) public appName: string,
13+
public router: Router
1114
) {
12-
this.init();
1315
}
1416

1517
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
@@ -28,25 +30,55 @@ export class AppInsightsService {
2830

2931
}
3032

31-
// [[ TODO ]] **
32-
3333
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
3434
// trackPageView(name?: string, url?: string, properties?:{[string]:string}, measurements?: {[string]:number}, duration?: number)
3535
// Logs that a page or similar container was displayed to the user.
36-
trackPageView() { return this.notImplemented('trackPageView'); }
36+
trackPageView(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}, duration?: number) {
37+
if (!this.isBrowser) {
38+
return;
39+
}
40+
41+
try {
42+
AppInsights.trackPageView(name, url, properties, measurements, duration);
43+
} catch (ex) {
44+
console.warn('Angular application insights Error [trackPageView]: ', ex);
45+
}
46+
}
3747

3848
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#starttrackpage
3949
// startTrackPage(name?: string)
4050
// Starts the timer for tracking a page view. Use this instead of trackPageView if you want to control when the
4151
// page view timer starts and stops, but don't want to calculate the duration yourself. This method doesn't send any
4252
// telemetry. Call stopTrackPage to log the end of the page view and send the event.
43-
startTrackPage(name?: string) { return this.notImplemented('startTrackPage'); }
53+
startTrackPage(name?: string) {
54+
if (!this.isBrowser) {
55+
return;
56+
}
57+
58+
try {
59+
AppInsights.startTrackPage(name);
60+
} catch (ex) {
61+
console.warn('Angular application insights Error [startTrackPage]: ', ex);
62+
}
63+
}
4464

4565
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#stoptrackpage
4666
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
4767
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
4868
// specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.
49-
stopTrackPage() { return this.notImplemented('stopTrackPage'); }
69+
stopTrackPage(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}) {
70+
if (!this.isBrowser) {
71+
return;
72+
}
73+
74+
try {
75+
AppInsights.stopTrackPage(name, url, properties, measurements);
76+
} catch (ex) {
77+
console.warn('Angular application insights Error [stopTrackPage]: ', ex);
78+
}
79+
}
80+
81+
// [[ TODO ]] **
5082

5183
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackmetric
5284
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
@@ -86,17 +118,23 @@ export class AppInsightsService {
86118
// Clears the authenticated user id and the account id from the user context, and clears the associated cookie.
87119
clearAuthenticatedUserContext() { return this.notImplemented('clearAuthenticatedUserContext'); }
88120

89-
90-
/*
91-
* Internal
92-
*/
93-
private init(): void {
121+
public init(): void {
94122
if (!this.isBrowser) {
95123
return;
96124
}
97125

98126
try {
99127
AppInsights.downloadAndSetup({ instrumentationKey: this.appID });
128+
129+
this.router.events.filter(event => event instanceof NavigationStart)
130+
.subscribe((event: NavigationStart) => {
131+
this.startTrackPage(event.url);
132+
});
133+
134+
this.router.events.filter(event => event instanceof NavigationEnd)
135+
.subscribe((event: NavigationEnd) => {
136+
this.stopTrackPage(event.url);
137+
});
100138
} catch (ex) {
101139
console.warn('Angular application insights Error [downloadAndSetup]: ', ex);
102140
}

0 commit comments

Comments
 (0)