@microsoft/applicationinsights-react-js 3.3.0-beta.2203-10 → 3.3.0-beta.2203-11

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,13 +3,14 @@
3
3
  * @copyright Microsoft 2019
4
4
  */
5
5
 
6
+ import dynamicProto from "@microsoft/dynamicproto-js";
6
7
  import {
7
8
  IConfig, IPageViewTelemetry, IMetricTelemetry, IAppInsights, IEventTelemetry, IExceptionTelemetry, ITraceTelemetry
8
9
  } from "@microsoft/applicationinsights-common";
9
10
  import {
10
11
  IPlugin, IConfiguration, IAppInsightsCore,
11
12
  ITelemetryPlugin, BaseTelemetryPlugin, ITelemetryItem, IProcessTelemetryContext,
12
- ITelemetryPluginChain, _InternalMessageId, LoggingSeverity, ICustomProperties, safeGetCookieMgr, ICookieMgr, arrForEach
13
+ ITelemetryPluginChain, _InternalMessageId, LoggingSeverity, ICustomProperties, safeGetCookieMgr, ICookieMgr, arrForEach, proxyFunctions, IProcessTelemetryUnloadContext, ITelemetryUnloadState, isFunction, objDefineAccessors
13
14
  } from "@microsoft/applicationinsights-core-js";
14
15
  import { IReactExtensionConfig } from './Interfaces/IReactExtensionConfig';
15
16
  import { History, Location, Update } from "history";
@@ -18,43 +19,129 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
18
19
  public priority = 185;
19
20
  public identifier = 'ReactPlugin';
20
21
 
21
- private _analyticsPlugin: IAppInsights;
22
- private _extensionConfig: IReactExtensionConfig;
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
+ };
23
53
 
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;
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;
35
99
  }
100
+
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);
121
+ }
122
+
123
+ objDefineAccessors(_self, "_extensionConfig", () => _extensionConfig);
36
124
  });
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
- }
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
44
129
  }
45
130
 
46
131
  /**
47
132
  * Get the current cookie manager for this instance
48
133
  */
49
134
  getCookieMgr(): ICookieMgr {
50
- return safeGetCookieMgr(this.core);
135
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
136
+ return null;
51
137
  }
52
138
 
53
139
  /**
54
140
  * Get application insights instance.
55
141
  */
56
142
  getAppInsights(): IAppInsights {
57
- return this._analyticsPlugin;
143
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
144
+ return null;
58
145
  }
59
146
 
60
147
  /**
@@ -62,76 +149,26 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
62
149
  * @param event The event that needs to be processed
63
150
  */
64
151
  processTelemetry(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
65
- this.processNext(event, itemCtx);
152
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
66
153
  }
67
154
 
68
155
  trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties) {
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
- }
156
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
75
157
  }
76
158
 
77
159
  trackPageView(pageView: IPageViewTelemetry) {
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
- }
160
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
84
161
  }
85
162
 
86
163
  trackEvent(event: IEventTelemetry, customProperties?: ICustomProperties) {
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
- }
164
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
93
165
  }
94
166
 
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
- }
167
+ trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties) {
168
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
104
169
  }
105
170
 
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);
171
+ trackTrace(trace: ITraceTelemetry, customProperties?: ICustomProperties) {
172
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
136
173
  }
137
174
  }
@@ -1,5 +1,5 @@
1
1
  import { useEffect, useRef } from "react";
2
- import { dateNow } from "@microsoft/applicationinsights-core-js";
2
+ import { dateNow, ICustomProperties } from "@microsoft/applicationinsights-core-js";
3
3
  import ReactPlugin from "./ReactPlugin";
4
4
 
5
5
  interface ITrackedData {
@@ -24,7 +24,8 @@ function getEngagementTimeSeconds(trackedData: ITrackedData) {
24
24
 
25
25
  const useComponentTracking = (
26
26
  reactPlugin: ReactPlugin,
27
- componentName: string
27
+ componentName: string,
28
+ customProperties?: ICustomProperties
28
29
  ) => {
29
30
  const tracking = useRef<ITrackedData>({
30
31
  hookTimestamp: dateNow(),
@@ -76,7 +77,7 @@ const useComponentTracking = (
76
77
  sampleCount: 1
77
78
  };
78
79
 
79
- const additionalProperties = { "Component Name": componentName };
80
+ const additionalProperties = { "Component Name": componentName, ...customProperties };
80
81
  reactPlugin.trackMetric(metricData, additionalProperties);
81
82
  };
82
83
  }, []);
@@ -7,8 +7,7 @@ import { IPlugin, IConfiguration, IAppInsightsCore, BaseTelemetryPlugin, ITeleme
7
7
  export default class ReactPlugin extends BaseTelemetryPlugin {
8
8
  priority: number;
9
9
  identifier: string;
10
- private _analyticsPlugin;
11
- private _extensionConfig;
10
+ constructor();
12
11
  initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain): void;
13
12
  /**
14
13
  * Get the current cookie manager for this instance
@@ -26,11 +25,6 @@ export default class ReactPlugin extends BaseTelemetryPlugin {
26
25
  trackMetric(metric: IMetricTelemetry, customProperties: ICustomProperties): void;
27
26
  trackPageView(pageView: IPageViewTelemetry): void;
28
27
  trackEvent(event: IEventTelemetry, 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;
28
+ trackException(exception: IExceptionTelemetry, customProperties?: ICustomProperties): void;
29
+ trackTrace(trace: ITraceTelemetry, customProperties?: ICustomProperties): void;
36
30
  }
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.19.4"
8
+ "packageVersion": "7.19.5"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,3 +1,4 @@
1
+ import { ICustomProperties } from "@microsoft/applicationinsights-core-js";
1
2
  import ReactPlugin from "./ReactPlugin";
2
- declare const useComponentTracking: (reactPlugin: ReactPlugin, componentName: string) => () => void;
3
+ declare const useComponentTracking: (reactPlugin: ReactPlugin, componentName: string, customProperties?: ICustomProperties) => () => void;
3
4
  export default useComponentTracking;