@microsoft/applicationinsights-react-js 3.2.5-nightly.2203-03 → 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,14 +3,13 @@
3
3
  * @copyright Microsoft 2019
4
4
  */
5
5
 
6
- import dynamicProto from "@microsoft/dynamicproto-js";
7
6
  import {
8
7
  IConfig, IPageViewTelemetry, IMetricTelemetry, IAppInsights, IEventTelemetry, IExceptionTelemetry, ITraceTelemetry
9
8
  } from "@microsoft/applicationinsights-common";
10
9
  import {
11
10
  IPlugin, IConfiguration, IAppInsightsCore,
12
11
  ITelemetryPlugin, BaseTelemetryPlugin, ITelemetryItem, IProcessTelemetryContext,
13
- ITelemetryPluginChain, _InternalMessageId, LoggingSeverity, ICustomProperties, safeGetCookieMgr, ICookieMgr, arrForEach, proxyFunctions, IProcessTelemetryUnloadContext, ITelemetryUnloadState, isFunction, objDefineAccessors
12
+ ITelemetryPluginChain, _InternalMessageId, LoggingSeverity, ICustomProperties, safeGetCookieMgr, ICookieMgr, arrForEach
14
13
  } from "@microsoft/applicationinsights-core-js";
15
14
  import { IReactExtensionConfig } from './Interfaces/IReactExtensionConfig';
16
15
  import { History, Location, Update } from "history";
@@ -19,129 +18,43 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
19
18
  public priority = 185;
20
19
  public identifier = 'ReactPlugin';
21
20
 
22
- constructor() {
23
- super();
24
- let _analyticsPlugin: IAppInsights;
25
- let _extensionConfig: IReactExtensionConfig;
26
- let _unlisten: any;
27
- let _pageViewTimer: any;
28
-
29
- dynamicProto(ReactPlugin, this, (_self, _base) => {
30
- _initDefaults();
31
-
32
- _self.initialize = (config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) => {
33
- super.initialize(config, core, extensions, pluginChain);
34
- _extensionConfig =
35
- config.extensionConfig && config.extensionConfig[_self.identifier]
36
- ? (config.extensionConfig[_self.identifier] as IReactExtensionConfig)
37
- : { history: null };
38
-
39
- arrForEach(extensions, ext => {
40
- const identifier = (ext as ITelemetryPlugin).identifier;
41
- if (identifier === 'ApplicationInsightsAnalytics') {
42
- _analyticsPlugin = (ext as any) as IAppInsights;
43
- }
44
- });
45
- if (_extensionConfig.history) {
46
- _addHistoryListener(_extensionConfig.history);
47
- const pageViewTelemetry: IPageViewTelemetry = {
48
- uri: _extensionConfig.history.location.pathname
49
- };
50
- _self.trackPageView(pageViewTelemetry);
51
- }
52
- };
53
-
54
- _self.getCookieMgr = (): ICookieMgr => {
55
- return safeGetCookieMgr(_self.core);
56
- };
57
-
58
- _self.getAppInsights = _getAnalytics;
59
-
60
- _self.processTelemetry = (event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
61
- _self.processNext(event, itemCtx);
62
- };
63
-
64
- _self._doTeardown = (unloadCtx?: IProcessTelemetryUnloadContext, unloadState?: ITelemetryUnloadState, asyncCallback?: () => void): void | boolean => {
65
- if (isFunction(_unlisten)) {
66
- _unlisten();
67
- }
68
-
69
- if (_pageViewTimer) {
70
- clearTimeout(_pageViewTimer);
71
- }
72
-
73
- _initDefaults();
74
- };
75
-
76
- // Proxy the analytics functions
77
- proxyFunctions(_self, _getAnalytics, [
78
- "trackMetric",
79
- "trackPageView",
80
- "trackEvent",
81
- "trackException",
82
- "trackTrace",
83
- ]);
84
-
85
- function _initDefaults() {
86
- _analyticsPlugin = null;
87
- _extensionConfig = null;
88
- _unlisten = null;
89
- _pageViewTimer = null;
90
- }
91
-
92
- function _getAnalytics() {
93
- if (!_analyticsPlugin) {
94
- _self.diagLog().throwInternal(
95
- LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
96
- }
97
-
98
- return _analyticsPlugin;
99
- }
21
+ private _analyticsPlugin: IAppInsights;
22
+ private _extensionConfig: IReactExtensionConfig;
100
23
 
101
- function _addHistoryListener(history: History): void {
102
- const locationListener = (arg: Location | Update): void => {
103
- // v4 of the history API passes "location" as the first argument, while v5 passes an object that contains location and action
104
- let locn: Location = null;
105
- if ("location" in arg) {
106
- // Looks like v5
107
- locn = arg["location"];
108
- } else {
109
- locn = arg as Location;
110
- }
111
-
112
- // Timeout to ensure any changes to the DOM made by route changes get included in pageView telemetry
113
- _pageViewTimer = setTimeout(() => {
114
- _pageViewTimer = null;
115
- const pageViewTelemetry: IPageViewTelemetry = { uri: locn.pathname };
116
- _self.trackPageView(pageViewTelemetry);
117
- }, 500);
118
- };
119
-
120
- _unlisten = history.listen(locationListener);
24
+ initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) {
25
+ super.initialize(config, core, extensions, pluginChain);
26
+ this._extensionConfig =
27
+ config.extensionConfig && config.extensionConfig[this.identifier]
28
+ ? (config.extensionConfig[this.identifier] as IReactExtensionConfig)
29
+ : { history: null };
30
+
31
+ arrForEach(extensions, ext => {
32
+ const identifier = (ext as ITelemetryPlugin).identifier;
33
+ if (identifier === 'ApplicationInsightsAnalytics') {
34
+ this._analyticsPlugin = (ext as any) as IAppInsights;
121
35
  }
122
-
123
- objDefineAccessors(_self, "_extensionConfig", () => _extensionConfig);
124
36
  });
125
- }
126
-
127
- initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) {
128
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
37
+ if (this._extensionConfig.history) {
38
+ this.addHistoryListener(this._extensionConfig.history);
39
+ const pageViewTelemetry: IPageViewTelemetry = {
40
+ uri: this._extensionConfig.history.location.pathname
41
+ };
42
+ this.trackPageView(pageViewTelemetry);
43
+ }
129
44
  }
130
45
 
131
46
  /**
132
47
  * Get the current cookie manager for this instance
133
48
  */
134
49
  getCookieMgr(): ICookieMgr {
135
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
136
- return null;
50
+ return safeGetCookieMgr(this.core);
137
51
  }
138
52
 
139
53
  /**
140
54
  * Get application insights instance.
141
55
  */
142
56
  getAppInsights(): IAppInsights {
143
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
144
- return null;
57
+ return this._analyticsPlugin;
145
58
  }
146
59
 
147
60
  /**
@@ -149,26 +62,76 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
149
62
  * @param event The event that needs to be processed
150
63
  */
151
64
  processTelemetry(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
152
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
65
+ this.processNext(event, itemCtx);
153
66
  }
154
67
 
155
68
  trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties) {
156
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
69
+ if (this._analyticsPlugin) {
70
+ this._analyticsPlugin.trackMetric(metric, customProperties);
71
+ } else {
72
+ this.diagLog().throwInternal(
73
+ LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
74
+ }
157
75
  }
158
76
 
159
77
  trackPageView(pageView: IPageViewTelemetry) {
160
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
78
+ if (this._analyticsPlugin) {
79
+ this._analyticsPlugin.trackPageView(pageView);
80
+ } else {
81
+ this.diagLog().throwInternal(
82
+ LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
83
+ }
161
84
  }
162
85
 
163
86
  trackEvent(event: IEventTelemetry, customProperties?: ICustomProperties) {
164
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
87
+ if (this._analyticsPlugin) {
88
+ this._analyticsPlugin.trackEvent(event, customProperties);
89
+ } else {
90
+ this.diagLog().throwInternal(
91
+ LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
92
+ }
165
93
  }
166
94
 
167
- trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties) {
168
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
95
+ trackException(exception: IExceptionTelemetry, customProperties?: {
96
+ [key: string]: any;
97
+ }) {
98
+ if (this._analyticsPlugin) {
99
+ this._analyticsPlugin.trackException(exception, customProperties);
100
+ } else {
101
+ this.diagLog().throwInternal(
102
+ LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
103
+ }
169
104
  }
170
105
 
171
- trackTrace(trace: ITraceTelemetry, customProperties?: ICustomProperties) {
172
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
106
+ trackTrace(trace: ITraceTelemetry, customProperties?: {
107
+ [key: string]: any;
108
+ }) {
109
+ if (this._analyticsPlugin) {
110
+ this._analyticsPlugin.trackTrace(trace, customProperties);
111
+ } else {
112
+ this.diagLog().throwInternal(
113
+ LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, React plugin telemetry will not be sent: ");
114
+ }
115
+ }
116
+
117
+
118
+ private addHistoryListener(history: History): void {
119
+ const locationListener = (arg: Location | Update): void => {
120
+ // v4 of the history API passes "location" as the first argument, while v5 passes an object that contains location and action
121
+ let locn: Location = null;
122
+ if ("location" in arg) {
123
+ // Looks like v5
124
+ locn = arg["location"];
125
+ } else {
126
+ locn = arg as Location;
127
+ }
128
+
129
+ // Timeout to ensure any changes to the DOM made by route changes get included in pageView telemetry
130
+ setTimeout(() => {
131
+ const pageViewTelemetry: IPageViewTelemetry = { uri: locn.pathname };
132
+ this.trackPageView(pageViewTelemetry);
133
+ }, 500);
134
+ };
135
+ history.listen(locationListener);
173
136
  }
174
137
  }
@@ -1,5 +1,5 @@
1
1
  import { useEffect, useRef } from "react";
2
- import { dateNow, ICustomProperties } from "@microsoft/applicationinsights-core-js";
2
+ import { dateNow } from "@microsoft/applicationinsights-core-js";
3
3
  import ReactPlugin from "./ReactPlugin";
4
4
 
5
5
  interface ITrackedData {
@@ -24,8 +24,7 @@ function getEngagementTimeSeconds(trackedData: ITrackedData) {
24
24
 
25
25
  const useComponentTracking = (
26
26
  reactPlugin: ReactPlugin,
27
- componentName: string,
28
- customProperties?: ICustomProperties
27
+ componentName: string
29
28
  ) => {
30
29
  const tracking = useRef<ITrackedData>({
31
30
  hookTimestamp: dateNow(),
@@ -77,7 +76,7 @@ const useComponentTracking = (
77
76
  sampleCount: 1
78
77
  };
79
78
 
80
- const additionalProperties = { "Component Name": componentName, ...customProperties };
79
+ const additionalProperties = { "Component Name": componentName };
81
80
  reactPlugin.trackMetric(metricData, additionalProperties);
82
81
  };
83
82
  }, []);
@@ -7,7 +7,8 @@ import { IPlugin, IConfiguration, IAppInsightsCore, BaseTelemetryPlugin, ITeleme
7
7
  export default class ReactPlugin extends BaseTelemetryPlugin {
8
8
  priority: number;
9
9
  identifier: string;
10
- constructor();
10
+ private _analyticsPlugin;
11
+ private _extensionConfig;
11
12
  initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain): void;
12
13
  /**
13
14
  * Get the current cookie manager for this instance
@@ -25,6 +26,11 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
25
26
  trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties): void;
26
27
  trackPageView(pageView: IPageViewTelemetry): void;
27
28
  trackEvent(event: IEventTelemetry, customProperties?: ICustomProperties): void;
28
- trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties): void;
29
- trackTrace(trace: ITraceTelemetry, customProperties?: ICustomProperties): void;
29
+ trackException(exception: IExceptionTelemetry, customProperties?: {
30
+ [key: string]: any;
31
+ }): void;
32
+ trackTrace(trace: ITraceTelemetry, customProperties?: {
33
+ [key: string]: any;
34
+ }): void;
35
+ private addHistoryListener;
30
36
  }
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.19.5"
8
+ "packageVersion": "7.23.0"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,4 +1,3 @@
1
- import { ICustomProperties } from "@microsoft/applicationinsights-core-js";
2
1
  import ReactPlugin from "./ReactPlugin";
3
- declare const useComponentTracking: (reactPlugin: ReactPlugin, componentName: string, customProperties?: ICustomProperties) => () => void;
2
+ declare const useComponentTracking: (reactPlugin: ReactPlugin, componentName: string) => () => void;
4
3
  export default useComponentTracking;