-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationInsights.js
More file actions
393 lines (393 loc) · 20.3 KB
/
ApplicationInsights.js
File metadata and controls
393 lines (393 loc) · 20.3 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
var _a;
import { __extends } from "tslib";
/**
* ApplicationInsights.ts
* @author Abhilash Panwar (abpanwar) and Hector Hernandez (hectorh)
* @copyright Microsoft 2018
* Main class containing all the APIs.
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import { AppInsightsCore, _throwInternal, arrIndexOf, createDynamicConfig, doPerf, dumpObj, isArray, isFunction, isNullOrUndefined, isString, objForEachKey, proxyAssign, proxyFunctions, throwError } from "@microsoft/1ds-core-js";
import { PostChannel } from "@microsoft/1ds-post-js";
import { PropertiesPlugin } from "@microsoft/1ds-properties-js";
import { ApplicationInsights as WebAnalytics } from "@microsoft/1ds-wa-js";
import { arrAppend, objDeepFreeze, objDefineProps } from "@nevware21/ts-utils";
/**
* The default settings for the config.
* WE MUST include all defaults here to ensure that the config is created with all of the properties
* defined as dynamic.
*/
var defaultConfig = objDeepFreeze({
cookieCfg: { ref: true, v: {} },
extensions: { rdOnly: true, ref: true, v: [] },
channels: { rdOnly: true, ref: true, v: [] },
featureOptIn: (_a = {},
_a["zipPayload"] = { mode: 1 },
_a),
extensionConfig: { ref: true, v: {} }
});
// This is an exclude list of properties that should not be updated during initialization
// They include a combination of private and internal property names and properties
var _ignoreUpdateSnippetProperties = [
"snippet", "_webAnalytics", "_postChannel", "_propertyManager", "_extensions"
];
// This is an exclude list of properties that may exist on both the snippet and the instance that
// should not be updated during definition assignment
var _ignoreUpdateDefineSnippetProperties = [
"queue", "extensions", "version", "sv"
];
var ApplicationInsights = /** @class */ (function (_super) {
__extends(ApplicationInsights, _super);
function ApplicationInsights() {
var _this = _super.call(this) || this;
var _snippetVersion;
var _webAnalytics;
var _postChannel;
var _propertyManager;
dynamicProto(ApplicationInsights, _this, function (_self, _base) {
_initDefaults();
_self.initialize = function (config, extensions) {
doPerf(_self, function () { return "ApplicationInsights:initialize"; }, function () {
config = createDynamicConfig(config, defaultConfig, _self.logger, false).cfg;
var plugins = [_propertyManager, _webAnalytics];
if (extensions) {
plugins = plugins.concat(extensions);
}
if (!config) {
throwError("You must provide a config object!");
}
if (config.channels && config.channels.length > 0) {
// Add post channel to first fork if not available
var postFound = false;
for (var j = 0; j < config.channels[0].length; j++) {
if (config.channels[0][j].identifier === _postChannel.identifier) {
postFound = true;
break;
}
}
if (!postFound) {
arrAppend(config.channels[0], _postChannel);
}
}
else {
// Push the subsequent channels
config.channels.push([_postChannel]);
}
// Add configurations
var extConfig = config.extensionConfig = config.extensionConfig || [];
extConfig[_postChannel.identifier] = extConfig[_postChannel.identifier] || (config && config.channelConfiguration) || {};
extConfig[_propertyManager.identifier] = extConfig[_propertyManager.identifier] || (config && config.propertyConfiguration) || {};
extConfig[_webAnalytics.identifier] = extConfig[_webAnalytics.identifier] || (config && config.webAnalyticsConfiguration) || {};
try {
_base.initialize(config, plugins);
if (_self.isInitialized()) {
// Update the shared config to map the channelConfiguration and propertyConfiguration to return the extensionConfig values
objDefineProps(config, {
channelConfiguration: { g: function () { return config.extensionConfig[_postChannel.identifier]; } },
propertyConfiguration: { g: function () { return config.extensionConfig[_propertyManager.identifier]; } },
webAnalyticsConfiguration: { g: function () { return config.extensionConfig[_webAnalytics.identifier]; } }
});
}
}
catch (error) {
_throwInternal(_self.logger, 1 /* eLoggingSeverity.CRITICAL */, 514 /* _eExtendedInternalMessageId.FailedToInitializeSDK */, "Failed to initialize SDK." + dumpObj(error));
}
}, function () { return ({ config: config, extensions: extensions }); });
};
_self.getPropertyManager = function () {
return _propertyManager;
};
_self.getPostChannel = function () {
return _postChannel;
};
_self.getWebAnalyticsExtension = function () {
return _webAnalytics;
};
// Expose these _webAnalytics functions directly on self
proxyFunctions(_self, function () { return _webAnalytics; }, [
"trackEvent",
"trackPageView",
"trackPageAction",
"trackContentUpdate",
"trackPageUnload",
"trackException",
"trackPageViewPerformance",
"capturePageView",
"capturePageViewPerformance",
"capturePageAction",
"captureContentUpdate",
"capturePageUnload",
"_onerror"
]);
_self.emptySnippetQueue = function (snippet) {
function _updateSnippetProperties() {
if (snippet) {
var snippetVer = "";
if (!isNullOrUndefined(_snippetVersion)) {
snippetVer += _snippetVersion;
}
// TODO (newylie): Need somewhere in Common Schema to put this value
// let propManager = _self.getPropertyManager();
// if (propManager) {
// let context = propManager.getPropertiesContext();
// if (_self.context && _self.context.internal) {
// _self.context.internal.snippetVer = snippetVer || "-";
// }
// }
// apply updated properties to the global instance (snippet)
objForEachKey(_self, function (field, value) {
if (isString(field) &&
!isFunction(value) &&
field && field[0] !== "_" && // Don't copy "internal" values
arrIndexOf(_ignoreUpdateSnippetProperties, field) === -1) {
try {
snippet[field] = value;
}
catch (error) {
// Unable to set the property -- so just ignore as it's probably a setter
_throwInternal(_self.logger, 2 /* eLoggingSeverity.WARNING */, 514 /* _eExtendedInternalMessageId.FailedToInitializeSDK */, "Failed to set [" + field + "] during initialization." + dumpObj(error));
}
}
});
}
}
// call functions that were queued before the main script was loaded
try {
_updateSnippetProperties();
if (isArray(snippet.queue)) {
// note: do not check length in the for-loop conditional in case something goes wrong and the stub methods are not overridden.
var length = snippet.queue.length;
for (var i = 0; i < length; i++) {
var call = snippet.queue[i];
call();
}
snippet.queue = undefined;
delete snippet.queue;
}
}
catch (exception) {
var properties = {};
if (exception && isFunction(exception.toString)) {
properties.exception = exception.toString();
}
}
};
/**
* Overwrite the lazy loaded fields of global window snippet to contain the
* actual initialized API methods
* @param snippet
*/
_self.updateSnippetDefinitions = function (snippet) {
var _self = _this;
// Assign the snippet to this instance
_self.snippet = snippet;
_snippetVersion = "" + (snippet.sv || snippet.version || "");
// The config (may) exist on both the snippet definition and the current instance, which by default would cause the
// snippet version to be overwritten (even with a value of null or undefined - which is now the default to properly
// support unloading
var snipCfg = snippet.config;
if (snipCfg) {
// Update/Merge the existing config (if present)
_self.updateCfg(snippet.config, true);
}
// apply full appInsights to the global instance
// Note: This will be called BEFORE this instance has been initialized
proxyAssign(snippet, _self, function (name) {
// Not excluding names prefixed with "_" as we need to proxy some functions like _onError
return name && arrIndexOf(_ignoreUpdateSnippetProperties, name) === -1 && arrIndexOf(_ignoreUpdateDefineSnippetProperties, name) === -1;
});
};
_self.unload = function (isAsync, unloadComplete, cbTimeout) {
if (isAsync === void 0) { isAsync = true; }
return _base.unload(isAsync, function (unloadState) {
_initDefaults();
unloadComplete && unloadComplete(unloadState);
}, cbTimeout);
};
});
function _initDefaults() {
// Initialize plugins
_postChannel = new PostChannel();
_propertyManager = new PropertiesPlugin();
_webAnalytics = new WebAnalytics();
}
return _this;
}
/**
* Initialize the SKU.
* @param config - SKU configuration.
* @param extensions - An array of extensions that are to be used by the core.
*/
ApplicationInsights.prototype.initialize = function (config, extensions) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* Gets the property manager to set custom properties and system properties (part A), that should be applied
* to all events or events with a specific instrumentation key.
* @returns {PropertiesPlugin} The property manager object.
*/
ApplicationInsights.prototype.getPropertyManager = function () {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
};
/**
* Gets the post channel to configure and set the transmission profiles.
* @returns {PostChannel} The post channel object.
*/
ApplicationInsights.prototype.getPostChannel = function () {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
};
/**
* Gets the Web Analytics extension.
* @returns {WebAnalytics} The Web Analytics extension.
*/
ApplicationInsights.prototype.getWebAnalyticsExtension = function () {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
};
/**
* API to send custom event
* @param event - Custom event
* @param properties - Custom event properties (part C)
*/
ApplicationInsights.prototype.trackEvent = function (event, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send pageView event
* @param pageViewEvent - PageView event
* @param properties - PageView properties (part C)
*/
ApplicationInsights.prototype.trackPageView = function (pageViewEvent, pageViewProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send pageAction event
* @param pageActionEvent - PageAction event
* @param properties - PageAction properties(Part C)
*/
ApplicationInsights.prototype.trackPageAction = function (pageActionEvent, pageActionProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send ContentUpdate event
* @param contentUpdateEvent - ContentUpdate event
* @param properties - ContentUpdate properties(Part C)
*/
ApplicationInsights.prototype.trackContentUpdate = function (contentUpdateEvent, properties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send PageUnload event
* @param pageUnloadEvent - PageUnload event
* @param properties - PageUnload properties(Part C)
*/
ApplicationInsights.prototype.trackPageUnload = function (pageUnloadEvent, properties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send Exception event
* @param exception - Exception event
* @param customProperties - Exception properties (part C)
*/
ApplicationInsights.prototype.trackException = function (exception, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to send PageViewPerformance event
* @param pageViewPerformance - PageViewPerformance event
* @param customProperties - PageViewPerformance properties (part C)
*/
ApplicationInsights.prototype.trackPageViewPerformance = function (pageViewPerformance, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to create and send a populated PageView event
* @param overrideValues - Override values
* @param customProperties - Custom properties(Part C)
*/
ApplicationInsights.prototype.capturePageView = function (overrideValues, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to create and send a populated PageViewPerformance event
* @param pageViewPerformance - PageViewPerformance event
* @param customProperties - Custom properties(Part C)
*/
ApplicationInsights.prototype.capturePageViewPerformance = function (overrideValues, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to create and send a populated PageAction event
* @param element - DOM element
* @param overrideValues - PageAction overrides
* @param customProperties - Custom properties(Part C)
* @param isRightClick - Flag for mouse right clicks
*/
ApplicationInsights.prototype.capturePageAction = function (element, overrideValues, customProperties, isRightClick) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to create and send a populated ContentUpdate event
* @param overrideValues - ContentUpdate overrides
* @param customProperties - Custom properties(Part C)
*/
ApplicationInsights.prototype.captureContentUpdate = function (overrideValues, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* API to create and send a populated PageUnload event
* @param overrideValues - PageUnload overrides
* @param customProperties - Custom properties(Part C)
*/
ApplicationInsights.prototype.capturePageUnload = function (overrideValues, customProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* @description Custom error handler for Application Insights Analytics
* @param exception
*/
ApplicationInsights.prototype._onerror = function (exception) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* Call any functions that were queued before the main script was loaded
* @param snippet
*/
ApplicationInsights.prototype.emptySnippetQueue = function (snippet) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* Overwrite the lazy loaded fields of global window snippet to contain the
* actual initialized API methods
* @param snippet
*/
ApplicationInsights.prototype.updateSnippetDefinitions = function (snippet) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
/**
* Unload and Tear down the SDK and any initialized plugins, after calling this the SDK will be considered
* to be un-initialized and non-operational, re-initializing the SDK should only be attempted if the previous
* unload call return `true` stating that all plugins reported that they also unloaded, the recommended
* approach is to create a new instance and initialize that instance.
* This is due to possible unexpected side effects caused by plugins not supporting unload / teardown, unable
* to successfully remove any global references or they may just be completing the unload process asynchronously.
* If you pass isAsync as `true` (also the default) and DO NOT pass a callback function then an [IPromise](https://nevware21.github.io/ts-async/typedoc/interfaces/IPromise.html)
* will be returned which will resolve once the unload is complete. The actual implementation of the `IPromise`
* will be a native Promise (if supported) or the default as supplied by [ts-async library](https://github.com/nevware21/ts-async)
* @param isAsync - Can the unload be performed asynchronously (default)
* @param unloadComplete - An optional callback that will be called once the unload has completed
* @param cbTimeout - An optional timeout to wait for any flush operations to complete before proceeding with the
* unload. Defaults to 5 seconds.
* @return Nothing or if occurring asynchronously a [IPromise](https://nevware21.github.io/ts-async/typedoc/interfaces/IPromise.html)
* which will be resolved once the unload is complete, the [IPromise](https://nevware21.github.io/ts-async/typedoc/interfaces/IPromise.html)
* will only be returned when no callback is provided and isAsync is true
*/
ApplicationInsights.prototype.unload = function (isAsync, unloadComplete, cbTimeout) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
};
return ApplicationInsights;
}(AppInsightsCore));
export { ApplicationInsights };
//# sourceMappingURL=ApplicationInsights.js.map