/
ReactPlugin.ts
186 lines (154 loc) · 7.45 KB
/
ReactPlugin.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
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
/**
* ReactPlugin.ts
* @copyright Microsoft 2019
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import {
AnalyticsPluginIdentifier,
IAppInsights, IConfig, IEventTelemetry, IExceptionTelemetry, IMetricTelemetry, IPageViewTelemetry, ITraceTelemetry
} from "@microsoft/applicationinsights-common";
import {
BaseTelemetryPlugin, IAppInsightsCore, IConfiguration, ICookieMgr, ICustomProperties, IPlugin, IProcessTelemetryContext,
IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryPlugin, ITelemetryPluginChain, ITelemetryUnloadState, _eInternalMessageId,
_throwInternal, arrForEach, eLoggingSeverity, isFunction, proxyFunctions, safeGetCookieMgr, IConfigDefaults, onConfigChange, objDefineAccessors
} from "@microsoft/applicationinsights-core-js";
import {objDeepFreeze} from "@nevware21/ts-utils";
import { History, Location, Update } from "history";
import { IReactExtensionConfig } from './Interfaces/IReactExtensionConfig';
const defaultReactExtensionConfig: IConfigDefaults<IReactExtensionConfig> = objDeepFreeze({
history: { blkVal: true, v: undefined }
});
export default class ReactPlugin extends BaseTelemetryPlugin {
public priority = 185;
public identifier = 'ReactPlugin';
constructor() {
super();
let _analyticsPlugin: IAppInsights;
let _extensionConfig: IReactExtensionConfig;
let _unlisten: any;
let _pageViewTimer: any;
let _pageViewTracked:boolean;
dynamicProto(ReactPlugin, this, (_self, _base) => {
_initDefaults();
_self.initialize = (config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) => {
super.initialize(config, core, extensions, pluginChain);
_self._addHook(onConfigChange(config, (details) => {
let ctx = _self._getTelCtx();
_extensionConfig = ctx.getExtCfg<IReactExtensionConfig>(this.identifier, defaultReactExtensionConfig);
_analyticsPlugin = core.getPlugin<any>(AnalyticsPluginIdentifier)?.plugin as IAppInsights;
if (isFunction(_unlisten)) {
_unlisten();
_unlisten = null;
}
if (_extensionConfig.history) {
_addHistoryListener(_extensionConfig.history);
if (!_pageViewTracked){
const pageViewTelemetry: IPageViewTelemetry = {
uri: _extensionConfig.history.location.pathname
};
_self.trackPageView(pageViewTelemetry);
_pageViewTracked = true;
}
}
}));
};
_self.getCookieMgr = (): ICookieMgr => {
return safeGetCookieMgr(_self.core);
};
_self.getAppInsights = _getAnalytics;
_self.processTelemetry = (event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
_self.processNext(event, itemCtx);
};
_self._doTeardown = (unloadCtx?: IProcessTelemetryUnloadContext, unloadState?: ITelemetryUnloadState, asyncCallback?: () => void): void | boolean => {
if (isFunction(_unlisten)) {
_unlisten();
}
if (_pageViewTimer) {
clearTimeout(_pageViewTimer);
}
_initDefaults();
};
// Proxy the analytics functions
proxyFunctions(_self, _getAnalytics, [
"trackMetric",
"trackPageView",
"trackEvent",
"trackException",
"trackTrace",
]);
function _initDefaults() {
_analyticsPlugin = null;
_extensionConfig = null;
_unlisten = null;
_pageViewTimer = null;
_pageViewTracked = false;
}
function _getAnalytics() {
if (!_analyticsPlugin) {
_throwInternal(_self.diagLog(),
eLoggingSeverity.CRITICAL, _eInternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
}
return _analyticsPlugin;
}
function _addHistoryListener(history: History): void {
const locationListener = (arg: Location | Update): void => {
// v4 of the history API passes "location" as the first argument, while v5 passes an object that contains location and action
let locn: Location = null;
if ("location" in arg) {
// Looks like v5
locn = arg["location"];
} else {
locn = arg as Location;
}
// Timeout to ensure any changes to the DOM made by route changes get included in pageView telemetry
_pageViewTimer = setTimeout(() => {
_pageViewTimer = null;
const pageViewTelemetry: IPageViewTelemetry = { uri: locn.pathname };
_self.trackPageView(pageViewTelemetry);
}, 500);
};
_unlisten = history.listen(locationListener);
}
objDefineAccessors(_self, "_extensionConfig", () => _extensionConfig);
});
}
initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
/**
* Get the current cookie manager for this instance
*/
getCookieMgr(): ICookieMgr {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
}
/**
* Get application insights instance.
*/
getAppInsights(): IAppInsights {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
}
/**
* Add Part A fields to the event
* @param event The event that needs to be processed
*/
processTelemetry(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
trackPageView(pageView: IPageViewTelemetry) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
trackEvent(event: IEventTelemetry, customProperties?: ICustomProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
trackTrace(trace: ITraceTelemetry, customProperties?: ICustomProperties) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
}