-
Notifications
You must be signed in to change notification settings - Fork 235
/
AppInsightsModule.ts
106 lines (86 loc) · 3.69 KB
/
AppInsightsModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/// <reference path="../JavaScriptSDK.Interfaces/IConfig.ts"/>
/// <reference path="../JavaScriptSDK.Interfaces/IAppInsights.ts"/>
"use strict";
class AppInsightsModule {
private static appInsightsInitialized: boolean = false;
private static appInsightsName = "appInsights";
private static _createLazyMethod(name) {
var aiObject = window[AppInsightsModule.appInsightsName];
// Define a temporary method that queues-up a the real method call
aiObject[name] = function () {
// Capture the original arguments passed to the method
var originalArguments = arguments;
// If the queue is available, it means that the function wasn't yet replaced with actual function value
if (aiObject.queue) {
aiObject.queue.push(() => aiObject[name].apply(aiObject, originalArguments));
}
else {
// otherwise execute the function
aiObject[name].apply(aiObject, originalArguments);
}
}
};
private static _defineLazyMethods() {
var aiObject = window[AppInsightsModule.appInsightsName];
// capture initial cookie if possible
try {
(<any>aiObject).cookie = document.cookie;
}
catch(e){
}
aiObject.queue = [];
var method = [
"clearAuthenticatedUserContext",
"flush",
"setAuthenticatedUserContext",
"startTrackEvent",
"startTrackPage",
"stopTrackEvent",
"stopTrackPage",
"trackDependency",
"trackEvent",
"trackException",
"trackMetric",
"trackPageView",
"trackTrace"
];
while (method.length) {
AppInsightsModule._createLazyMethod(method.pop());
}
}
private static _download(aiConfig: Microsoft.ApplicationInsights.IConfig) {
AppInsightsModule.appInsightsInstance.config = aiConfig;
var aiObject = window[AppInsightsModule.appInsightsName];
// if script was previously downloaded and initialized, queue will be deleted, reinitialize it
if (!aiObject.queue) {
aiObject.queue = [];
}
var scriptElement = document.createElement("script");
scriptElement.src = aiConfig.url || "https://az416426.vo.msecnd.net/scripts/a/ai.0.js";
document.head.appendChild(scriptElement);
// collect global errors
if (!aiConfig.disableExceptionTracking) {
AppInsightsModule._createLazyMethod("_onerror");
var originalOnError = window["_onerror"];
window["_onerror"] = function (message, url, lineNumber, columnNumber, error) {
var handled = originalOnError && originalOnError(message, url, lineNumber, columnNumber, error);
if (handled !== true) {
aiObject["_onerror"](message, url, lineNumber, columnNumber, error);
}
return handled;
};
}
}
public static get appInsightsInstance(): Microsoft.ApplicationInsights.IAppInsights {
if (!window[AppInsightsModule.appInsightsName]) {
window[AppInsightsModule.appInsightsName] = {
downloadAndSetup: AppInsightsModule._download,
// exposing it for unit tests only, not part of interface
_defineLazyMethods: AppInsightsModule._defineLazyMethods
};
AppInsightsModule._defineLazyMethods();
}
return window[AppInsightsModule.appInsightsName];
}
}
export var AppInsights: Microsoft.ApplicationInsights.IAppInsights = AppInsightsModule.appInsightsInstance;