-
Notifications
You must be signed in to change notification settings - Fork 236
/
ReactPlugin.ts
110 lines (97 loc) · 4.5 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
/**
* ReactPlugin.ts
* @copyright Microsoft 2019
*/
import {
IConfig, IPageViewTelemetry, IMetricTelemetry, IAppInsights, IEventTelemetry, IExceptionTelemetry
} from "@microsoft/applicationinsights-common";
import {
IPlugin, IConfiguration, IAppInsightsCore, IDiagnosticLogger,
ITelemetryPlugin, BaseTelemetryPlugin, ITelemetryItem, IProcessTelemetryContext,
ITelemetryPluginChain, _InternalMessageId, LoggingSeverity, ICustomProperties, safeGetCookieMgr, ICookieMgr, arrForEach
} from "@microsoft/applicationinsights-core-js";
import { IReactExtensionConfig } from './Interfaces/IReactExtensionConfig';
import { History, LocationListener, Location, Action } from "history";
export default class ReactPlugin extends BaseTelemetryPlugin {
public priority = 185;
public identifier = 'ReactPlugin';
private _analyticsPlugin: IAppInsights;
private _extensionConfig: IReactExtensionConfig;
initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) {
super.initialize(config, core, extensions, pluginChain);
this._extensionConfig =
config.extensionConfig && config.extensionConfig[this.identifier]
? (config.extensionConfig[this.identifier] as IReactExtensionConfig)
: { history: null };
arrForEach(extensions, ext => {
const identifier = (ext as ITelemetryPlugin).identifier;
if (identifier === 'ApplicationInsightsAnalytics') {
this._analyticsPlugin = (ext as any) as IAppInsights;
}
});
if (this._extensionConfig.history) {
this.addHistoryListener(this._extensionConfig.history);
const pageViewTelemetry: IPageViewTelemetry = {
uri: this._extensionConfig.history.location.pathname
};
this.trackPageView(pageViewTelemetry);
}
}
/**
* Get the current cookie manager for this instance
*/
getCookieMgr(): ICookieMgr {
return safeGetCookieMgr(this.core);
}
/**
* Add Part A fields to the event
* @param event The event that needs to be processed
*/
processTelemetry(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
this.processNext(event, itemCtx);
}
trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties) {
if (this._analyticsPlugin) {
this._analyticsPlugin.trackMetric(metric, customProperties);
} else {
this.diagLog().throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
}
}
trackPageView(pageView: IPageViewTelemetry) {
if (this._analyticsPlugin) {
this._analyticsPlugin.trackPageView(pageView);
} else {
this.diagLog().throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
}
}
trackEvent(event: IEventTelemetry, customProperties?: ICustomProperties) {
if (this._analyticsPlugin) {
this._analyticsPlugin.trackEvent(event, customProperties);
} else {
this.diagLog().throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
}
}
trackException(exception: IExceptionTelemetry, customProperties?: {
[key: string]: any;
}) {
if (this._analyticsPlugin) {
this._analyticsPlugin.trackException(exception, customProperties);
} else {
this.diagLog().throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
}
};
private addHistoryListener(history: History): void {
const locationListener: LocationListener = (location: Location, action: Action): void => {
// Timeout to ensure any changes to the DOM made by route changes get included in pageView telemetry
setTimeout(() => {
const pageViewTelemetry: IPageViewTelemetry = { uri: location.pathname };
this.trackPageView(pageViewTelemetry);
}, 500);
};
history.listen(locationListener);
}
}