1
1
import { Injectable , Inject } from '@angular/core' ;
2
+ import { Router , NavigationStart , NavigationEnd } from '@angular/router' ;
2
3
import { AppInsights } from 'applicationinsights-js' ;
3
4
import { APP_INSIGHT_ID , APP_NAME } from './app-insight.config' ;
5
+ import 'rxjs/add/operator/filter' ;
4
6
5
7
@Injectable ( )
6
8
export class AppInsightsService {
7
9
8
10
constructor (
9
11
@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
11
14
) {
12
- this . init ( ) ;
13
15
}
14
16
15
17
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
@@ -28,25 +30,55 @@ export class AppInsightsService {
28
30
29
31
}
30
32
31
- // [[ TODO ]] **
32
-
33
33
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
34
34
// trackPageView(name?: string, url?: string, properties?:{[string]:string}, measurements?: {[string]:number}, duration?: number)
35
35
// 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
+ }
37
47
38
48
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#starttrackpage
39
49
// startTrackPage(name?: string)
40
50
// Starts the timer for tracking a page view. Use this instead of trackPageView if you want to control when the
41
51
// page view timer starts and stops, but don't want to calculate the duration yourself. This method doesn't send any
42
52
// 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
+ }
44
64
45
65
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#stoptrackpage
46
66
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
47
67
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
48
68
// 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 ]] **
50
82
51
83
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackmetric
52
84
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
@@ -86,17 +118,23 @@ export class AppInsightsService {
86
118
// Clears the authenticated user id and the account id from the user context, and clears the associated cookie.
87
119
clearAuthenticatedUserContext ( ) { return this . notImplemented ( 'clearAuthenticatedUserContext' ) ; }
88
120
89
-
90
- /*
91
- * Internal
92
- */
93
- private init ( ) : void {
121
+ public init ( ) : void {
94
122
if ( ! this . isBrowser ) {
95
123
return ;
96
124
}
97
125
98
126
try {
99
127
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
+ } ) ;
100
138
} catch ( ex ) {
101
139
console . warn ( 'Angular application insights Error [downloadAndSetup]: ' , ex ) ;
102
140
}
0 commit comments