@@ -5,6 +5,7 @@ import 'rxjs/add/operator/filter';
5
5
import IAppInsights = Microsoft . ApplicationInsights . IAppInsights ;
6
6
7
7
export class AppInsightsConfig implements Microsoft . ApplicationInsights . IConfig {
8
+ instrumentationKeySetlater ?: boolean ;
8
9
instrumentationKey ?: string ;
9
10
endpointUrl ?: string ;
10
11
emitLineDelimitedJson ?: boolean ;
@@ -41,14 +42,14 @@ export class AppInsightsService implements IAppInsights {
41
42
context : Microsoft . ApplicationInsights . ITelemetryContext ;
42
43
queue : Array < ( ) => void > ;
43
44
config : AppInsightsConfig ;
44
- constructor ( @Optional ( ) _config : AppInsightsConfig , public router : Router ) {
45
+ constructor ( @Optional ( ) _config : AppInsightsConfig , public router : Router ) {
45
46
this . config = _config ;
46
47
}
47
48
48
49
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
49
50
// trackEvent(name: string, properties?: {[string]:string}, measurements?: {[string]:number})
50
51
// Log a user action or other occurrence.
51
- trackEvent ( eventName : string , eventProperties ?: { [ name : string ] : string } , metricProperty ?: { [ name : string ] : number } ) {
52
+ trackEvent ( eventName : string , eventProperties ?: { [ name : string ] : string } , metricProperty ?: { [ name : string ] : number } ) {
52
53
try {
53
54
AppInsights . trackEvent ( eventName , eventProperties , metricProperty ) ;
54
55
} catch ( ex ) {
@@ -75,7 +76,7 @@ export class AppInsightsService implements IAppInsights {
75
76
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
76
77
// trackPageView(name?: string, url?: string, properties?:{[string]:string}, measurements?: {[string]:number}, duration?: number)
77
78
// Logs that a page or similar container was displayed to the user.
78
- trackPageView ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } , duration ?: number ) {
79
+ trackPageView ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } , duration ?: number ) {
79
80
try {
80
81
AppInsights . trackPageView ( name , url , properties , measurements , duration ) ;
81
82
} catch ( ex ) {
@@ -100,7 +101,7 @@ export class AppInsightsService implements IAppInsights {
100
101
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
101
102
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
102
103
// specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.
103
- stopTrackPage ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } ) {
104
+ stopTrackPage ( name ?: string , url ?: string , properties ?: { [ name : string ] : string } , measurements ?: { [ name : string ] : number } ) {
104
105
try {
105
106
AppInsights . stopTrackPage ( name , url , properties , measurements ) ;
106
107
} catch ( ex ) {
@@ -112,7 +113,7 @@ export class AppInsightsService implements IAppInsights {
112
113
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
113
114
// Log a positive numeric value that is not associated with a specific event.
114
115
// Typically used to send regular reports of performance indicators.
115
- trackMetric ( name : string , average : number , sampleCount ?: number , min ?: number , max ?: number , properties ?: { [ name : string ] : string } ) {
116
+ trackMetric ( name : string , average : number , sampleCount ?: number , min ?: number , max ?: number , properties ?: { [ name : string ] : string } ) {
116
117
try {
117
118
AppInsights . trackMetric ( name , average , sampleCount , min , max , properties ) ;
118
119
} catch ( ex ) {
@@ -123,8 +124,8 @@ export class AppInsightsService implements IAppInsights {
123
124
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackexception
124
125
// trackException(exception: Error, handledAt?: string, properties?: {[string]:string}, measurements?: {[string]:number}, severityLevel?: AI.SeverityLevel)
125
126
// Log an exception you have caught. (Exceptions caught by the browser are also logged.)
126
- trackException ( exception : Error , handledAt ?: string , properties ?: { [ name : string ] : string } ,
127
- measurements ?: { [ name : string ] : number } , severityLevel ?: AI . SeverityLevel ) {
127
+ trackException ( exception : Error , handledAt ?: string , properties ?: { [ name : string ] : string } ,
128
+ measurements ?: { [ name : string ] : number } , severityLevel ?: AI . SeverityLevel ) {
128
129
try {
129
130
AppInsights . trackException ( exception , handledAt , properties , measurements , severityLevel ) ;
130
131
} catch ( ex ) {
@@ -135,7 +136,7 @@ export class AppInsightsService implements IAppInsights {
135
136
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#tracktrace
136
137
// trackTrace(message: string, properties?: {[string]:string}, measurements?: {[string]:number})
137
138
// Log a diagnostic event such as entering or leaving a method.
138
- trackTrace ( message : string , properties ?: { [ name : string ] : string } ) {
139
+ trackTrace ( message : string , properties ?: { [ name : string ] : string } ) {
139
140
try {
140
141
AppInsights . trackTrace ( message , properties ) ;
141
142
} catch ( ex ) {
@@ -195,28 +196,33 @@ export class AppInsightsService implements IAppInsights {
195
196
}
196
197
197
198
public init ( ) : void {
198
- if ( this . config . instrumentationKey ) {
199
- try {
200
- AppInsights . downloadAndSetup ( this . config ) ;
199
+ if ( this . config ) {
200
+ if ( this . config . instrumentationKey ) {
201
+ try {
202
+ AppInsights . downloadAndSetup ( this . config ) ;
201
203
202
- if ( ! this . config . overrideTrackPageMetrics ) {
203
- this . router . events . filter ( event => event instanceof NavigationStart )
204
+ if ( ! this . config . overrideTrackPageMetrics ) {
205
+ this . router . events . filter ( event => event instanceof NavigationStart )
204
206
. subscribe ( ( event : NavigationStart ) => {
205
207
this . startTrackPage ( event . url ) ;
206
208
} ) ;
207
209
208
- this . router . events . filter ( event => event instanceof NavigationEnd )
210
+ this . router . events . filter ( event => event instanceof NavigationEnd )
209
211
. subscribe ( ( event : NavigationEnd ) => {
210
212
this . stopTrackPage ( event . url ) ;
211
213
} ) ;
214
+ }
215
+ } catch ( ex ) {
216
+ console . warn ( 'Angular application insights Error [downloadAndSetup]: ' , ex ) ;
217
+ }
218
+ } else {
219
+ if ( this . config . instrumentationKeySetlater ) {
220
+ console . warn ( 'An instrumentationKey value is required to initialize AppInsightsService' ) ;
212
221
}
213
- } catch ( ex ) {
214
- console . warn ( 'Angular application insights Error [downloadAndSetup]: ' , ex ) ;
215
222
}
216
223
} else {
217
- console . warn ( 'An instrumentationKey value is required to initialize AppInsightsService ' ) ;
224
+ console . warn ( 'You need forRoot on ApplicationInsightsModule, with or instrumentationKeySetlater or instrumentationKey set at least ' ) ;
218
225
}
219
-
220
226
}
221
227
}
222
228
0 commit comments