@@ -17,8 +17,8 @@ export class AppInsightsService {
17
17
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
18
18
// trackEvent(name: string, properties?: {[string]:string}, measurements?: {[string]:number})
19
19
// Log a user action or other occurrence.
20
- trackEvent ( eventName : string , eventProperties ?: { string : string } , metricProperty ?: { string : number } ) {
21
- if ( ! this . isBrowser ) {
20
+ static trackEvent ( eventName : string , eventProperties ?: { [ name : string ] : string } , metricProperty ?: { [ name : string ] : number } ) {
21
+ if ( ! AppInsightsService . isBrowser ) {
22
22
return ;
23
23
}
24
24
@@ -33,8 +33,8 @@ export class AppInsightsService {
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 ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } , duration ?: number ) {
37
- if ( ! this . isBrowser ) {
36
+ static trackPageView ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } , duration ?: number ) {
37
+ if ( ! AppInsightsService . isBrowser ) {
38
38
return ;
39
39
}
40
40
@@ -50,8 +50,8 @@ export class AppInsightsService {
50
50
// Starts the timer for tracking a page view. Use this instead of trackPageView if you want to control when the
51
51
// page view timer starts and stops, but don't want to calculate the duration yourself. This method doesn't send any
52
52
// telemetry. Call stopTrackPage to log the end of the page view and send the event.
53
- startTrackPage ( name ?: string ) {
54
- if ( ! this . isBrowser ) {
53
+ static startTrackPage ( name ?: string ) {
54
+ if ( ! AppInsightsService . isBrowser ) {
55
55
return ;
56
56
}
57
57
@@ -66,8 +66,8 @@ export class AppInsightsService {
66
66
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
67
67
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
68
68
// specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.
69
- stopTrackPage ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } ) {
70
- if ( ! this . isBrowser ) {
69
+ static stopTrackPage ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } ) {
70
+ if ( ! AppInsightsService . isBrowser ) {
71
71
return ;
72
72
}
73
73
@@ -78,48 +78,117 @@ export class AppInsightsService {
78
78
}
79
79
}
80
80
81
- // [[ TODO ]] **
82
-
83
81
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackmetric
84
82
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
85
83
// Log a positive numeric value that is not associated with a specific event.
86
84
// Typically used to send regular reports of performance indicators.
87
- trackMetric ( ) { return this . notImplemented ( 'trackMetric' ) ; }
85
+ static trackMetric ( name : string , average : number , sampleCount ?: number , min ?: number , max ?: number , properties ?: { [ name : string ] : string } ) {
86
+ if ( ! AppInsightsService . isBrowser ) {
87
+ return ;
88
+ }
89
+
90
+ try {
91
+ AppInsights . trackMetric ( name , average , sampleCount , min , max , properties ) ;
92
+ } catch ( ex ) {
93
+ console . warn ( 'Angular application insights Error [trackTrace]: ' , ex ) ;
94
+ }
95
+ }
88
96
89
97
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackexception
90
98
// trackException(exception: Error, handledAt?: string, properties?: {[string]:string}, measurements?: {[string]:number}, severityLevel?: AI.SeverityLevel)
91
99
// Log an exception you have caught. (Exceptions caught by the browser are also logged.)
92
- trackException ( ) { return this . notImplemented ( 'trackException' ) ; }
100
+ static trackException ( exception : Error , handledAt ?: string , properties ?: { [ name : string ] : string } ,
101
+ measurements ?: { [ name : string ] : number } , severityLevel ?: AI . SeverityLevel ) {
102
+ if ( ! AppInsightsService . isBrowser ) {
103
+ return ;
104
+ }
105
+
106
+ try {
107
+ AppInsights . trackException ( exception , handledAt , properties , measurements , severityLevel ) ;
108
+ } catch ( ex ) {
109
+ console . warn ( 'Angular application insights Error [trackException]: ' , ex ) ;
110
+ }
111
+ }
93
112
94
113
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#tracktrace
95
114
// trackTrace(message: string, properties?: {[string]:string}, measurements?: {[string]:number})
96
115
// Log a diagnostic event such as entering or leaving a method.
97
- trackTrace ( ) { return this . notImplemented ( 'trackTrace' ) ; }
116
+ static trackTrace ( message : string , properties ?: { [ name : string ] : string } ) {
117
+ if ( ! AppInsightsService . isBrowser ) {
118
+ return ;
119
+ }
120
+
121
+ try {
122
+ AppInsights . trackTrace ( message , properties ) ;
123
+ } catch ( ex ) {
124
+ console . warn ( 'Angular application insights Error [trackTrace]: ' , ex ) ;
125
+ }
126
+ }
98
127
99
128
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackdependency
100
129
// trackDependency(id: string, method: string, absoluteUrl: string, pathName: string, totalTime: number, success: boolean, resultCode: number)
101
130
// Log a dependency call (for instance: ajax)
102
- trackDependency ( ) { return this . notImplemented ( 'trackDependency' ) ; }
131
+ static trackDependency ( id : string , method : string , absoluteUrl : string , pathName : string , totalTime : number , success : boolean , resultCode : number ) {
132
+ if ( ! AppInsightsService . isBrowser ) {
133
+ return ;
134
+ }
135
+
136
+ try {
137
+ AppInsights . trackDependency ( id , method , absoluteUrl , pathName , totalTime , success , resultCode ) ;
138
+ } catch ( ex ) {
139
+ console . warn ( 'Angular application insights Error [trackDependency]: ' , ex ) ;
140
+ }
141
+ }
103
142
104
143
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#flush
105
144
// flush()
106
145
// Immediately send all queued telemetry. Synchronous.
107
146
// * You don't usually have to use this, as it happens automatically on window closing.
108
- flush ( ) { return this . notImplemented ( 'flush' ) ; }
147
+ static flush ( ) {
148
+ if ( ! AppInsightsService . isBrowser ) {
149
+ return ;
150
+ }
151
+ try {
152
+ AppInsights . flush ( ) ;
153
+ } catch ( ex ) {
154
+ console . warn ( 'Angular application insights Error [flush]: ' , ex ) ;
155
+ }
156
+
157
+ }
109
158
110
159
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#setauthenticatedusercontext
111
160
// setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string)
112
161
// Set the authenticated user id and the account id in this session. Use this when you have identified a specific
113
162
// signed-in user. Parameters must not contain spaces or ,;=|
114
- setAuthenticatedUserContext ( ) { return this . notImplemented ( 'setAuthenticatedUserContext' ) ; }
163
+ static setAuthenticatedUserContext ( authenticatedUserId : string , accountId ?: string ) {
164
+ if ( ! AppInsightsService . isBrowser ) {
165
+ return ;
166
+ }
167
+
168
+ try {
169
+ AppInsights . setAuthenticatedUserContext ( authenticatedUserId , accountId ) ;
170
+ } catch ( ex ) {
171
+ console . warn ( 'Angular application insights Error [setAuthenticatedUserContext]: ' , ex ) ;
172
+ }
173
+ }
115
174
116
175
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#clearauthenticatedusercontext
117
176
// clearAuthenticatedUserContext ()
118
177
// Clears the authenticated user id and the account id from the user context, and clears the associated cookie.
119
- clearAuthenticatedUserContext ( ) { return this . notImplemented ( 'clearAuthenticatedUserContext' ) ; }
178
+ static clearAuthenticatedUserContext ( ) {
179
+ if ( ! AppInsightsService . isBrowser ) {
180
+ return ;
181
+ }
182
+
183
+ try {
184
+ AppInsights . clearAuthenticatedUserContext ( ) ;
185
+ } catch ( ex ) {
186
+ console . warn ( 'Angular application insights Error [clearAuthenticatedUserContext]: ' , ex ) ;
187
+ }
188
+ }
120
189
121
190
public init ( ) : void {
122
- if ( ! this . isBrowser ) {
191
+ if ( ! AppInsightsService . isBrowser ) {
123
192
return ;
124
193
}
125
194
@@ -128,27 +197,20 @@ export class AppInsightsService {
128
197
129
198
this . router . events . filter ( event => event instanceof NavigationStart )
130
199
. subscribe ( ( event : NavigationStart ) => {
131
- this . startTrackPage ( event . url ) ;
200
+ AppInsightsService . startTrackPage ( event . url ) ;
132
201
} ) ;
133
202
134
203
this . router . events . filter ( event => event instanceof NavigationEnd )
135
204
. subscribe ( ( event : NavigationEnd ) => {
136
- this . stopTrackPage ( event . url ) ;
205
+ AppInsightsService . stopTrackPage ( event . url ) ;
137
206
} ) ;
138
207
} catch ( ex ) {
139
208
console . warn ( 'Angular application insights Error [downloadAndSetup]: ' , ex ) ;
140
209
}
141
210
}
142
211
143
- private isBrowser ( ) : boolean {
212
+ private static isBrowser ( ) : boolean {
144
213
return ( typeof ( < any > window ) !== undefined ) ;
145
214
}
146
-
147
- private notImplemented ( methodName : string ) {
148
- return new Error ( `
149
- This method is not implemented in ApplicationInsightsService: [ ${ methodName } ]
150
- Please file any issues here: https://github.com/MarkPieszak/angular-application-insights/issues` ) ;
151
- }
152
-
153
215
}
154
216
0 commit comments