-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAnalyticsPlugin.js
More file actions
529 lines (529 loc) · 25.1 KB
/
WebAnalyticsPlugin.js
File metadata and controls
529 lines (529 loc) · 25.1 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import { __extends } from "tslib";
/**
* WebAnalyticsPlugin.ts
* @author Ram Thiru (ramthi) and Hector Hernandez (hectorh)
* @copyright Microsoft 2018
* File containing the interfaces for WebAnalytics SDK.
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import { _throwInternal, createGuid, createProcessTelemetryContext, extend, isDocumentObjectAvailable, isValueAssigned, objForEachKey, onConfigChange, setProcessTelemetryTimings } from "@microsoft/1ds-core-js";
import { AnalyticsPlugin } from "@microsoft/applicationinsights-analytics-js";
import { cfgDfFunc, cfgDfMerge } from "@microsoft/applicationinsights-core-js";
import { objDeepFreeze } from "@nevware21/ts-utils";
import { onDomLoaded } from "./DataCollector";
import { Id } from "./Id";
import { Timespan } from "./Timespan";
import { _isClickTelemetryAllowed } from "./common/Utils";
import { ContentUpdate } from "./events/ContentUpdate";
import { PageAction } from "./events/PageAction";
import { PageUnload } from "./events/PageUnload";
import { PageView } from "./events/PageView";
import { PageViewPerformance } from "./events/PageViewPerformance";
import { AutoCaptureHandler } from "./handlers/AutoCaptureHandler";
import { DomContentHandler } from "./handlers/DomContentHandler";
var defaultConfig = objDeepFreeze({
// General library settings
useDefaultContentName: true,
useShortNameForContentBlob: true,
debounceMs: cfgDfMerge({
scroll: 600,
resize: 3000
}),
biBlobAttributeTag: "data-m",
isLoggedIn: false,
shareAuthStatus: false,
cookiesToCollect: ["MSFPC", "ANON"],
autoCapture: cfgDfMerge({
pageView: true,
onLoad: true,
onUnload: true,
click: true,
scroll: false,
resize: false,
lineage: false,
jsError: true,
msTags: true
}),
callback: cfgDfMerge({
pageName: cfgDfFunc(),
pageActionPageTags: cfgDfFunc(),
pageViewPageTags: cfgDfFunc(),
contentUpdatePageTags: cfgDfFunc(),
pageActionContentTags: cfgDfFunc(),
signedinStatus: cfgDfFunc()
}),
// overrideValues to use instead of collecting automatically
coreData: cfgDfMerge({
referrerUri: isDocumentObjectAvailable ? document.referrer : "",
requestUri: "",
pageName: "",
pageType: "",
product: "",
market: "",
pageTags: {}
}),
autoPopulateParentIdAndParentName: false,
syncMuid: false,
muidDomain: "microsoft.com",
mscomCookies: false,
manageCv: false,
urlCollectHash: false,
urlCollectQuery: false,
manualPageUnload: false,
syncPageActionNavClick: true,
syncUnloadAction: true,
});
var ApplicationInsights = /** @class */ (function (_super) {
__extends(ApplicationInsights, _super);
/**
* @constructor
* @param WebAnalytics module configuration object.
*/
function ApplicationInsights() {
var _this = _super.call(this) || this;
_this.identifier = "WebAnalyticsPlugin";
_this.version = '4.3.12';
var _pageView;
var _pageAction;
var _contentUpdate;
var _pageUnload;
var _pageViewPerformance;
var _cvPlugin;
var _theConfig;
var _maxScroll;
var _isPageUnloadFired = false;
var _timespan;
var _contentHandler;
var _autoCaptureHandler;
var _autoCaptureConfig;
var _syncMuid;
var _muidDomain;
var _userSetContentHandler; // the value set from _self.setContentHandler, this will overwrite any dynamic config changes
dynamicProto(ApplicationInsights, _this, function (_self, _base) {
_initDefaults();
_self.updateCoreDataConfig = function (coreData) {
_theConfig.coreData = extend(true, _theConfig.coreData, coreData);
};
_self.refreshMetadata = function () {
var metaTags = _contentHandler.getMetadata();
_pageView.metaTags = metaTags;
_contentUpdate.metaTags = metaTags;
_pageAction.metaTags = metaTags;
_pageViewPerformance.metaTags = metaTags;
};
_self.initialize = function (coreConfig, core, extensions) {
_base.initialize(coreConfig, core, extensions);
_populateDefaults(coreConfig, extensions);
};
_self.processTelemetry = function (evt, itemCtx) {
setProcessTelemetryTimings(evt, _self.identifier);
var event = evt;
if (event.baseType === "PageviewData") {
event.name = "Ms.Web.PageView";
event.latency = 3 /* EventLatencyValue.RealTime */;
}
else if (event.baseType === "ExceptionData") {
event.name = "Ms.Web.ClientError";
event.latency = 1 /* EventLatencyValue.Normal */;
// Remove extra AI properties
delete (event.baseData["aiDataContract"]);
}
else if (event.baseType === "PageviewPerformanceData") {
event.name = "Ms.Web.PageViewPerformance";
event.latency = 1 /* EventLatencyValue.Normal */;
// Remove extra AI properties
delete (event.baseData["isValid"]);
delete (event.baseData["durationMs"]);
}
// Correlation
var cv = null;
if (event.baseType !== "PageviewData") {
// If automatic cV management is desired and cV plugin is available
if (_theConfig.manageCv) {
cv = _cvPlugin.getCv();
if (cv) {
cv.increment();
}
}
}
else {
if (_theConfig.manageCv) {
cv = _cvPlugin.getCv();
// Seed a new cV for each event
if (!cv) {
cv = _cvPlugin.getCv();
}
else {
cv.seed();
}
}
}
_base.processTelemetry(event, itemCtx);
};
_self.trackEvent = function (event, customProperties) {
event.latency = event.latency || 1 /* EventLatencyValue.Normal */;
event.baseData = event.baseData || {};
event.data = event.data || {};
// Add extra Part C
if (isValueAssigned(customProperties)) {
objForEachKey(customProperties, function (prop, value) {
event.data[prop] = value;
});
}
_self.core.track(event);
};
_self.trackPageView = function (pageViewEvent, properties) {
_resetPageUnloadProperties();
// Initialize IDs to be used as parent and trace IDs
_self.id.initializeIds();
pageViewEvent.id = _self.id.getLastPageViewId();
_base.sendPageViewInternal(pageViewEvent, properties, _getSystemProperties(pageViewEvent));
};
_self.capturePageView = function (overrideValues, customProperties) {
_pageView.capturePageView(overrideValues, customProperties);
};
_self.trackPageViewPerformance = function (pageViewPerformance, customProperties) {
_base.sendPageViewPerformanceInternal(pageViewPerformance, customProperties, _getSystemProperties(pageViewPerformance));
};
_self.capturePageViewPerformance = function (overrideValues, customProperties) {
_pageViewPerformance.capturePageViewPerformance(overrideValues, customProperties);
};
_self.trackException = function (exception, customProperties) {
exception.id = exception.id || createGuid();
_base.sendExceptionInternal(exception, customProperties, _getSystemProperties(exception));
};
_self.trackPageAction = function (pageActionEvent, pageActionProperties) {
_pageAction.trackPageAction(pageActionEvent, pageActionProperties);
};
_self.capturePageAction = function (element, overrideValues, customProperties, isRightClick) {
if (_isClickTelemetryAllowed(element, overrideValues)) {
_pageAction.capturePageAction(element, overrideValues, customProperties, isRightClick);
}
};
_self.trackContentUpdate = function (contentUpdateEvent, properties) {
_contentUpdate.trackContentUpdate(contentUpdateEvent, properties);
};
_self.captureContentUpdate = function (overrideValues, customProperties) {
_contentUpdate.captureContentUpdate(overrideValues, customProperties);
};
_self.trackPageUnload = function (pageUnloadEvent, properties) {
if (!_isPageUnloadFired) {
_isPageUnloadFired = true;
_pageUnload.trackPageUnload(pageUnloadEvent, properties);
}
};
_self.capturePageUnload = function (overrideValues, customProperties) {
if (!_isPageUnloadFired) {
_isPageUnloadFired = true;
_pageUnload.capturePageUnload(overrideValues, customProperties);
}
};
_self._populatePageViewPerformance = function (pageViewPerformance) {
var perfManager = _self._pageViewPerformanceManager;
if (perfManager) {
perfManager.populatePageViewPerformanceEvent(pageViewPerformance);
}
};
_self.setContentHandler = function (contentHandler) {
_contentHandler = _userSetContentHandler = contentHandler; // be sure to set handler before initialization
};
_self.setAutoCaptureHandler = function (autoCaptureHandler) {
if (_autoCaptureHandler !== autoCaptureHandler) {
// Make sure it removes any event handlers
_autoCaptureHandler && _autoCaptureHandler.teardown();
_autoCaptureHandler = autoCaptureHandler;
_setupAutoCapture(false);
}
};
_self._doTeardown = function (unloadCtx, unloadState) {
_autoCaptureHandler && _autoCaptureHandler.teardown(unloadCtx, unloadState);
_base._doTeardown(unloadCtx, unloadState);
_initDefaults();
};
_self["_getDbgPlgTargets"] = function () {
return [_theConfig];
};
function _populateDefaults(coreConfig, extensions) {
var core = _self.core;
var logger = _self.diagLog();
_self.id = new Id(core);
_timespan = new Timespan();
// Default to DOM content handler
_autoCaptureHandler = _autoCaptureHandler ? _autoCaptureHandler : new AutoCaptureHandler(_self, logger);
_self._addHook(onConfigChange(coreConfig, function () {
var ctx = createProcessTelemetryContext(null, coreConfig, core);
var extConfig = ctx.getExtCfg(_self.identifier, defaultConfig);
_theConfig = extConfig;
_autoCaptureConfig = _theConfig.autoCapture;
var existingGetWParamMethod = core.getWParam;
core.getWParam = function () {
var wparam = 0;
if (_theConfig.mscomCookies) {
wparam = wparam | 1;
}
return wparam | existingGetWParamMethod.call(core);
};
_theConfig.disableExceptionTracking = coreConfig.extensionConfig[_self.identifier].disableExceptionTracking = !_autoCaptureConfig.jsError;
if (_theConfig.manageCv) {
for (var i = 0; i < extensions.length; i++) {
if ((extensions[i]).identifier === "CorrelationVectorPlugin") {
_theConfig.manageCv = true;
_cvPlugin = extensions[i];
break;
}
}
if (!_cvPlugin) {
_throwInternal(_self.diagLog(), 2 /* eLoggingSeverity.WARNING */, 508 /* _eExtendedInternalMessageId.CVPluginNotAvailable */, "Automatic Cv management is set to \"true\" in config. However, cv plugin is not available. Disabling automatic Cv management");
_theConfig.manageCv = false;
}
}
_contentHandler = _userSetContentHandler || new DomContentHandler(_theConfig, logger);
var callback = _theConfig.callback;
var metaTags = _contentHandler.getMetadata();
var id = _self.id;
_contentUpdate = new ContentUpdate(_self, _theConfig, _contentHandler, id, callback.contentUpdatePageTags, metaTags, logger);
_pageView = new PageView(_self, _theConfig, _contentHandler, id, callback.pageViewPageTags, metaTags, logger);
_pageAction = new PageAction(_self, _theConfig, _contentHandler, id, callback.pageActionPageTags, metaTags, logger);
_contentUpdate = new ContentUpdate(_self, _theConfig, _contentHandler, id, callback.contentUpdatePageTags, metaTags, logger);
_pageUnload = new PageUnload(_self, _theConfig, id, logger, _timespan, _maxScroll);
_pageViewPerformance = new PageViewPerformance(_self, _theConfig, _contentHandler, id, callback.pageViewPageTags, metaTags, logger);
_updateMuid();
_syncMuid = !!_theConfig.syncMuid;
_muidDomain = _theConfig.muidDomain;
}));
_setupAutoCapture(true);
}
function _updateMuid() {
var syncMuid = !!_theConfig.syncMuid;
var shouldUpdate = !_syncMuid || (_muidDomain !== _theConfig.muidDomain);
// Note: PageView is sent as soon as init is called (i.e. right after the Web Analytics script is loaded).
// No Muid Sync will happen as we wait to send PV as soon as possible while Muid Sync requires document ready to happen.
// This matches WEDCS in way of when they send PV without Muid Sync.
if (syncMuid && shouldUpdate) {
onDomLoaded(function () {
var muidDomain = _self.id.getMuidHost(_theConfig.muidDomain);
_self.id.syncMuid(muidDomain);
}, _self._evtNamespace);
}
// if syncMuid is set to false currently, do nothing
}
function _initDefaults() {
_pageView = null;
_pageAction = null;
_contentUpdate = null;
_pageUnload = null;
_pageViewPerformance = null;
_cvPlugin = null;
_theConfig = null;
_maxScroll = { h: 0, v: 0 };
_isPageUnloadFired = false;
_timespan = null;
_contentHandler = null;
_autoCaptureHandler = null;
_autoCaptureConfig = null;
_syncMuid = false;
_muidDomain = null;
}
function _setupAutoCapture(isInitialize) {
if (_autoCaptureHandler) {
if (isInitialize) {
// Initialize only events
if (_autoCaptureConfig.pageView) {
_autoCaptureHandler.pageView();
}
if (_autoCaptureConfig.onLoad) {
_autoCaptureHandler.onLoad();
}
}
// handle automatic event firing on user click
if (_autoCaptureConfig.click) {
_autoCaptureHandler.click();
}
// handle automatic event firing on user scroll
if (_autoCaptureConfig.scroll) {
_autoCaptureHandler.scroll(_theConfig.debounceMs);
}
// handle automatic event firing on user resize
if (_autoCaptureConfig.resize) {
_autoCaptureHandler.resize(_theConfig.debounceMs);
}
// measure maxScroll
if (_autoCaptureConfig.onUnload || _theConfig.manualPageUnload) {
_autoCaptureHandler.maxScroll(_maxScroll);
}
if (_autoCaptureConfig.onUnload) {
_autoCaptureHandler.onUnload();
}
}
}
function _getSystemProperties(event) {
var ext = {};
if (event.isManual !== undefined) {
ext["web"] = {};
ext["web"]["isManual"] = event.isManual !== undefined ? event.isManual : true;
delete (event.isManual);
}
return ext;
}
/**
* @ignore
* Resets the values used for pageUnload.
*/
function _resetPageUnloadProperties() {
_timespan._recordTimeSpan("dwellTime", false);
_maxScroll.v = 0;
_isPageUnloadFired = false;
}
});
return _this;
}
/**
* Update coreData configuration
* @param coreData
*/
ApplicationInsights.prototype.updateCoreDataConfig = function (coreData) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* Refresh metadata reference traversing the DOM again
*/
ApplicationInsights.prototype.refreshMetadata = function () {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* Starts the WebAnalytics plugin
* @param config The core configuration.
*/
ApplicationInsights.prototype.initialize = function (coreConfig, core, extensions) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* Process a given event.
* Hydrate with appropriate PartB and PartC data
* @param evt - The event to be hydrated.
* @param itemCtx - This is the context for the current request, ITelemetryPlugin instances
* can optionally use this to access the current core instance or define / pass additional information
* to later plugins (vs appending items to the telemetry item)
*/
ApplicationInsights.prototype.processTelemetry = function (evt, itemCtx) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* 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, properties) {
// @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 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 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 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 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 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 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 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 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 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
};
ApplicationInsights.prototype._populatePageViewPerformance = function (pageViewPerformance) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* Set custom content handler, need to be set before initialization
* @param contentHandler - Content handler instance
*/
ApplicationInsights.prototype.setContentHandler = function (contentHandler) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
/**
* Set custom auto capture handler, need to be set before initialization
* @param autoCaptureHandler - Content handler instance
*/
ApplicationInsights.prototype.setAutoCaptureHandler = function (autoCaptureHandler) {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
};
return ApplicationInsights;
}(AnalyticsPlugin));
export { ApplicationInsights };
//# sourceMappingURL=WebAnalyticsPlugin.js.map