@microsoft/applicationinsights-react-js 3.3.2 → 3.3.4-nightly.2205-04

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.
Files changed (30) hide show
  1. package/browser/applicationinsights-react-js.js +89 -454
  2. package/browser/applicationinsights-react-js.js.map +1 -1
  3. package/browser/applicationinsights-react-js.min.js +2 -2
  4. package/browser/applicationinsights-react-js.min.js.map +1 -1
  5. package/dist/applicationinsights-react-js.api.json +491 -2
  6. package/dist/applicationinsights-react-js.api.md +32 -1
  7. package/dist/applicationinsights-react-js.d.ts +25 -2
  8. package/dist/applicationinsights-react-js.js +89 -454
  9. package/dist/applicationinsights-react-js.js.map +1 -1
  10. package/dist/applicationinsights-react-js.min.js +2 -2
  11. package/dist/applicationinsights-react-js.min.js.map +1 -1
  12. package/dist/applicationinsights-react-js.rollup.d.ts +25 -2
  13. package/dist-esm/AppInsightsContext.js +1 -1
  14. package/dist-esm/AppInsightsErrorBoundary.js +1 -1
  15. package/dist-esm/Interfaces/IReactExtensionConfig.js +1 -1
  16. package/dist-esm/ReactPlugin.js +1 -1
  17. package/dist-esm/applicationinsights-react-js.js +3 -3
  18. package/dist-esm/applicationinsights-react-js.js.map +1 -1
  19. package/dist-esm/useTrackEvent.js +1 -1
  20. package/dist-esm/useTrackMetric.js +1 -1
  21. package/dist-esm/withAITracking.js +78 -64
  22. package/dist-esm/withAITracking.js.map +1 -1
  23. package/package.json +74 -71
  24. package/src/AppInsightsErrorBoundary.tsx +1 -0
  25. package/src/applicationinsights-react-js.ts +3 -2
  26. package/src/withAITracking.tsx +90 -70
  27. package/types/AppInsightsErrorBoundary.d.ts +2 -1
  28. package/types/applicationinsights-react-js.d.ts +2 -2
  29. package/types/tsdoc-metadata.json +1 -1
  30. package/types/withAITracking.d.ts +21 -0
@@ -2,10 +2,92 @@
2
2
  // Licensed under the MIT License.
3
3
 
4
4
  import { IMetricTelemetry } from '@microsoft/applicationinsights-common';
5
- import { CoreUtils } from '@microsoft/applicationinsights-core-js';
5
+ import { dateNow } from '@microsoft/applicationinsights-core-js';
6
6
  import * as React from 'react';
7
7
  import ReactPlugin from './ReactPlugin';
8
8
 
9
+ /**
10
+ * Higher-order component base class to hook Application Insights tracking
11
+ * in a React component's lifecycle.
12
+ */
13
+ export abstract class AITrackedComponentBase<P> extends React.Component<P> {
14
+ protected _mountTimestamp: number = 0;
15
+ protected _firstActiveTimestamp: number = 0;
16
+ protected _idleStartTimestamp: number = 0;
17
+ protected _lastActiveTimestamp: number = 0;
18
+ protected _totalIdleTime: number = 0;
19
+ protected _idleCount: number = 0;
20
+ protected _idleTimeout: number = 5000;
21
+ protected _intervalId?: any;
22
+ protected _componentName: string;
23
+ protected _reactPlugin: ReactPlugin;
24
+
25
+ public constructor(props: P, reactPlugin: ReactPlugin, componentName: string) {
26
+ super(props);
27
+
28
+ this._reactPlugin = reactPlugin;
29
+ this._componentName = componentName;
30
+ }
31
+
32
+ public componentDidMount() {
33
+ this._mountTimestamp = dateNow();
34
+ this._firstActiveTimestamp = 0;
35
+ this._totalIdleTime = 0;
36
+ this._lastActiveTimestamp = 0;
37
+ this._idleStartTimestamp = 0;
38
+ this._idleCount = 0;
39
+
40
+ this._intervalId = setInterval(() => {
41
+ if (this._lastActiveTimestamp > 0 && this._idleStartTimestamp === 0 && dateNow() - this._lastActiveTimestamp >= this._idleTimeout) {
42
+ this._idleStartTimestamp = dateNow();
43
+ this._idleCount++;
44
+ }
45
+ }, 100);
46
+ }
47
+
48
+ public componentWillUnmount() {
49
+ if (this._mountTimestamp === 0) {
50
+ throw new Error('withAITracking:componentWillUnmount: mountTimestamp is not initialized.');
51
+ }
52
+ if (this._intervalId) {
53
+ clearInterval(this._intervalId);
54
+ }
55
+
56
+ if (this._firstActiveTimestamp === 0) {
57
+ return;
58
+ }
59
+
60
+ const engagementTime = this.getEngagementTimeSeconds();
61
+ const metricData: IMetricTelemetry = {
62
+ average: engagementTime,
63
+ name: 'React Component Engaged Time (seconds)',
64
+ sampleCount: 1
65
+ };
66
+
67
+ const additionalProperties: { [key: string]: any } = { 'Component Name': this._componentName };
68
+ this._reactPlugin.trackMetric(metricData, additionalProperties);
69
+ }
70
+
71
+ protected trackActivity = (e: React.SyntheticEvent<any>): void => {
72
+ if (this._firstActiveTimestamp === 0) {
73
+ this._firstActiveTimestamp = dateNow();
74
+ this._lastActiveTimestamp = this._firstActiveTimestamp;
75
+ } else {
76
+ this._lastActiveTimestamp = dateNow();
77
+ }
78
+
79
+ if (this._idleStartTimestamp > 0) {
80
+ const lastIdleTime = this._lastActiveTimestamp - this._idleStartTimestamp;
81
+ this._totalIdleTime += lastIdleTime;
82
+ this._idleStartTimestamp = 0;
83
+ }
84
+ }
85
+
86
+ private getEngagementTimeSeconds(): number {
87
+ return (dateNow() - this._firstActiveTimestamp - this._totalIdleTime - this._idleCount * this._idleTimeout) / 1000;
88
+ }
89
+ }
90
+
9
91
  /**
10
92
  * Higher-order component function to hook Application Insights tracking
11
93
  * in a React component's lifecycle.
@@ -18,63 +100,20 @@ import ReactPlugin from './ReactPlugin';
18
100
  export default function withAITracking<P>(reactPlugin: ReactPlugin, Component: React.ComponentType<P>, componentName?: string, className?: string): React.ComponentClass<P> {
19
101
 
20
102
  if (componentName === undefined || componentName === null || typeof componentName !== 'string') {
21
- componentName = Component.prototype &&
22
- Component.prototype.constructor &&
23
- Component.prototype.constructor.name ||
24
- 'Unknown';
103
+ componentName = Component.prototype &&
104
+ Component.prototype.constructor &&
105
+ Component.prototype.constructor.name ||
106
+ 'Unknown';
25
107
  }
26
108
 
27
109
  if (className === undefined || className === null || typeof className !== 'string') {
28
110
  className = '';
29
111
  }
30
112
 
31
- return class extends React.Component<P> {
32
- private _mountTimestamp: number = 0;
33
- private _firstActiveTimestamp: number = 0;
34
- private _idleStartTimestamp: number = 0;
35
- private _lastActiveTimestamp: number = 0;
36
- private _totalIdleTime: number = 0;
37
- private _idleCount: number = 0;
38
- private _idleTimeout: number = 5000;
39
- private _intervalId?: any;
40
-
41
- public componentDidMount() {
42
- this._mountTimestamp = CoreUtils.dateNow();
43
- this._firstActiveTimestamp = 0;
44
- this._totalIdleTime = 0;
45
- this._lastActiveTimestamp = 0;
46
- this._idleStartTimestamp = 0;
47
- this._idleCount = 0;
48
-
49
- this._intervalId = setInterval(() => {
50
- if (this._lastActiveTimestamp > 0 && this._idleStartTimestamp === 0 && CoreUtils.dateNow() - this._lastActiveTimestamp >= this._idleTimeout) {
51
- this._idleStartTimestamp = CoreUtils.dateNow();
52
- this._idleCount++;
53
- }
54
- }, 100);
55
- }
56
-
57
- public componentWillUnmount() {
58
- if (this._mountTimestamp === 0) {
59
- throw new Error('withAITracking:componentWillUnmount: mountTimestamp is not initialized.');
60
- }
61
- if (this._intervalId) {
62
- clearInterval(this._intervalId);
63
- }
113
+ return class extends AITrackedComponentBase<P> {
64
114
 
65
- if (this._firstActiveTimestamp === 0) {
66
- return;
67
- }
68
-
69
- const engagementTime = this.getEngagementTimeSeconds();
70
- const metricData: IMetricTelemetry = {
71
- average: engagementTime,
72
- name: 'React Component Engaged Time (seconds)',
73
- sampleCount: 1
74
- };
75
-
76
- const additionalProperties: { [key: string]: any } = { 'Component Name': componentName };
77
- reactPlugin.trackMetric(metricData, additionalProperties);
115
+ public constructor(props: P) {
116
+ super(props, reactPlugin, componentName);
78
117
  }
79
118
 
80
119
  public render() {
@@ -92,24 +131,5 @@ export default function withAITracking<P>(reactPlugin: ReactPlugin, Component: R
92
131
  </div>
93
132
  );
94
133
  }
95
-
96
- private trackActivity = (e: React.SyntheticEvent<any>): void => {
97
- if (this._firstActiveTimestamp === 0) {
98
- this._firstActiveTimestamp = CoreUtils.dateNow();
99
- this._lastActiveTimestamp = this._firstActiveTimestamp;
100
- } else {
101
- this._lastActiveTimestamp = CoreUtils.dateNow();
102
- }
103
-
104
- if (this._idleStartTimestamp > 0) {
105
- const lastIdleTime = this._lastActiveTimestamp - this._idleStartTimestamp;
106
- this._totalIdleTime += lastIdleTime;
107
- this._idleStartTimestamp = 0;
108
- }
109
- }
110
-
111
- private getEngagementTimeSeconds(): number {
112
- return (CoreUtils.dateNow() - this._firstActiveTimestamp - this._totalIdleTime - this._idleCount * this._idleTimeout) / 1000;
113
- }
114
134
  }
115
135
  }
@@ -3,6 +3,7 @@ import ReactPlugin from "./ReactPlugin";
3
3
  export interface IAppInsightsErrorBoundaryProps {
4
4
  appInsights: ReactPlugin;
5
5
  onError: React.ComponentType<any>;
6
+ children: React.ReactElement;
6
7
  }
7
8
  export interface IAppInsightsErrorBoundaryState {
8
9
  hasError: boolean;
@@ -12,5 +13,5 @@ export default class AppInsightsErrorBoundary extends React.Component<IAppInsigh
12
13
  hasError: boolean;
13
14
  };
14
15
  componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
15
- render(): React.ReactNode;
16
+ render(): React.ReactElement<any, string | React.JSXElementConstructor<any>> & React.ReactNode;
16
17
  }
@@ -1,8 +1,8 @@
1
1
  import { IReactExtensionConfig } from "./Interfaces/IReactExtensionConfig";
2
2
  import ReactPlugin from "./ReactPlugin";
3
- import withAITracking from "./withAITracking";
3
+ import withAITracking, { AITrackedComponentBase } from "./withAITracking";
4
4
  import AppInsightsErrorBoundary from "./AppInsightsErrorBoundary";
5
5
  import { AppInsightsContext, useAppInsightsContext } from "./AppInsightsContext";
6
6
  import useTrackEvent from "./useTrackEvent";
7
7
  import useTrackMetric from "./useTrackMetric";
8
- export { ReactPlugin, IReactExtensionConfig, withAITracking, AppInsightsErrorBoundary, AppInsightsContext, useAppInsightsContext, useTrackEvent, useTrackMetric };
8
+ export { ReactPlugin, IReactExtensionConfig, withAITracking, AppInsightsErrorBoundary, AppInsightsContext, useAppInsightsContext, useTrackEvent, useTrackMetric, AITrackedComponentBase };
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.23.0"
8
+ "packageVersion": "7.23.1"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,5 +1,26 @@
1
1
  import * as React from 'react';
2
2
  import ReactPlugin from './ReactPlugin';
3
+ /**
4
+ * Higher-order component base class to hook Application Insights tracking
5
+ * in a React component's lifecycle.
6
+ */
7
+ export declare abstract class AITrackedComponentBase<P> extends React.Component<P> {
8
+ protected _mountTimestamp: number;
9
+ protected _firstActiveTimestamp: number;
10
+ protected _idleStartTimestamp: number;
11
+ protected _lastActiveTimestamp: number;
12
+ protected _totalIdleTime: number;
13
+ protected _idleCount: number;
14
+ protected _idleTimeout: number;
15
+ protected _intervalId?: any;
16
+ protected _componentName: string;
17
+ protected _reactPlugin: ReactPlugin;
18
+ constructor(props: P, reactPlugin: ReactPlugin, componentName: string);
19
+ componentDidMount(): void;
20
+ componentWillUnmount(): void;
21
+ protected trackActivity: (e: React.SyntheticEvent<any>) => void;
22
+ private getEngagementTimeSeconds;
23
+ }
3
24
  /**
4
25
  * Higher-order component function to hook Application Insights tracking
5
26
  * in a React component's lifecycle.