@amplitude/analytics-react-native 1.11.0-video-analytics-5.0 → 1.12.0

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 (62) hide show
  1. package/lib/commonjs/amp-capture.js +10 -1
  2. package/lib/commonjs/amp-capture.js.map +1 -1
  3. package/lib/commonjs/autocapture/element-interaction.js +22 -0
  4. package/lib/commonjs/autocapture/element-interaction.js.map +1 -0
  5. package/lib/commonjs/autocapture/error-click.js +136 -0
  6. package/lib/commonjs/autocapture/error-click.js.map +1 -0
  7. package/lib/commonjs/autocapture/rage-click.js +141 -0
  8. package/lib/commonjs/autocapture/rage-click.js.map +1 -0
  9. package/lib/commonjs/config/joined-config.js +1 -0
  10. package/lib/commonjs/config/joined-config.js.map +1 -1
  11. package/lib/commonjs/constants.js +3 -1
  12. package/lib/commonjs/constants.js.map +1 -1
  13. package/lib/commonjs/plugins/frustration-plugin.js +87 -0
  14. package/lib/commonjs/plugins/frustration-plugin.js.map +1 -0
  15. package/lib/commonjs/react-native-client.js +27 -23
  16. package/lib/commonjs/react-native-client.js.map +1 -1
  17. package/lib/commonjs/version.js +1 -1
  18. package/lib/commonjs/version.js.map +1 -1
  19. package/lib/module/amp-capture.js +10 -1
  20. package/lib/module/amp-capture.js.map +1 -1
  21. package/lib/module/autocapture/element-interaction.js +17 -0
  22. package/lib/module/autocapture/element-interaction.js.map +1 -0
  23. package/lib/module/autocapture/error-click.js +126 -0
  24. package/lib/module/autocapture/error-click.js.map +1 -0
  25. package/lib/module/autocapture/rage-click.js +133 -0
  26. package/lib/module/autocapture/rage-click.js.map +1 -0
  27. package/lib/module/config/joined-config.js +1 -0
  28. package/lib/module/config/joined-config.js.map +1 -1
  29. package/lib/module/constants.js +2 -0
  30. package/lib/module/constants.js.map +1 -1
  31. package/lib/module/plugins/frustration-plugin.js +79 -0
  32. package/lib/module/plugins/frustration-plugin.js.map +1 -0
  33. package/lib/module/react-native-client.js +28 -24
  34. package/lib/module/react-native-client.js.map +1 -1
  35. package/lib/module/version.js +1 -1
  36. package/lib/module/version.js.map +1 -1
  37. package/lib/typescript/amp-capture.d.ts +7 -1
  38. package/lib/typescript/amp-capture.d.ts.map +1 -1
  39. package/lib/typescript/autocapture/element-interaction.d.ts +7 -0
  40. package/lib/typescript/autocapture/element-interaction.d.ts.map +1 -0
  41. package/lib/typescript/autocapture/error-click.d.ts +30 -0
  42. package/lib/typescript/autocapture/error-click.d.ts.map +1 -0
  43. package/lib/typescript/autocapture/rage-click.d.ts +30 -0
  44. package/lib/typescript/autocapture/rage-click.d.ts.map +1 -0
  45. package/lib/typescript/config/joined-config.d.ts.map +1 -1
  46. package/lib/typescript/constants.d.ts +2 -0
  47. package/lib/typescript/constants.d.ts.map +1 -1
  48. package/lib/typescript/plugins/frustration-plugin.d.ts +18 -0
  49. package/lib/typescript/plugins/frustration-plugin.d.ts.map +1 -0
  50. package/lib/typescript/react-native-client.d.ts.map +1 -1
  51. package/lib/typescript/version.d.ts +1 -1
  52. package/lib/typescript/version.d.ts.map +1 -1
  53. package/package.json +3 -3
  54. package/src/amp-capture.ts +13 -3
  55. package/src/autocapture/element-interaction.ts +27 -0
  56. package/src/autocapture/error-click.ts +178 -0
  57. package/src/autocapture/rage-click.ts +172 -0
  58. package/src/config/joined-config.ts +1 -0
  59. package/src/constants.ts +2 -0
  60. package/src/plugins/frustration-plugin.ts +106 -0
  61. package/src/react-native-client.ts +19 -14
  62. package/src/version.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"react-native-client.d.ts","sourceRoot":"","sources":["../../src/react-native-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,cAAc,EAAoD,MAAM,cAAc,CAAC;AAC1G,OAAO,EACL,aAAa,EAOb,iBAAiB,EACjB,kBAAkB,EAClB,6BAA6B,IAAI,kBAAkB,EACnD,SAAS,EACT,YAAY,EACZ,KAAK,EACL,MAAM,EACN,iBAAiB,EAOjB,eAAe,EASf,6BAA6B,EAC7B,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,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAgBnC;IAEF;;;OAGG;YACW,uBAAuB;IAgDrC,OAAO,CAAC,eAAe;IAUvB,OAAO,CAAC,cAAc;CAGvB;AAED,eAAO,MAAM,cAAc,QAAO,iBAmIjC,CAAC;;AAEF,wBAAgC"}
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.11.0-video-analytics-5.0";
1
+ export declare const VERSION = "1.12.0";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,+BAA+B,CAAC"}
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,WAAW,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amplitude/analytics-react-native",
3
- "version": "1.11.0-video-analytics-5.0",
3
+ "version": "1.12.0",
4
4
  "description": "Official React Native SDK",
5
5
  "keywords": [
6
6
  "analytics",
@@ -42,8 +42,8 @@
42
42
  "@amplitude/ua-parser-js": "^0.7.31",
43
43
  "@react-native-async-storage/async-storage": "^1.17.11",
44
44
  "tslib": "^2.4.1",
45
- "@amplitude/analytics-core": "2.58.0-video-analytics-5.0",
46
- "@amplitude/plugin-network-capture-browser": "1.10.17-video-analytics-5.0"
45
+ "@amplitude/plugin-network-capture-browser": "1.10.20",
46
+ "@amplitude/analytics-core": "2.60.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/react": "^18.0.26",
@@ -6,9 +6,16 @@ export type AmpCaptureProperties = {
6
6
  testID?: string;
7
7
  };
8
8
 
9
- const callbacks: ((properties: AmpCaptureProperties) => void)[] = [];
9
+ export type AmpCaptureCoordinates = {
10
+ x: number;
11
+ y: number;
12
+ };
13
+
14
+ type AmpCaptureCallback = (properties: AmpCaptureProperties, coordinates?: AmpCaptureCoordinates) => void;
15
+
16
+ const callbacks: AmpCaptureCallback[] = [];
10
17
 
11
- export function subscribe(callback: (properties: AmpCaptureProperties) => void) {
18
+ export function subscribe(callback: AmpCaptureCallback) {
12
19
  callbacks.push(callback);
13
20
  return () => {
14
21
  const index = callbacks.indexOf(callback);
@@ -32,8 +39,11 @@ export function ampCapture<Args extends unknown[], Return>(
32
39
  // only call "callbacks" if not nested inside another ampCapture
33
40
  try {
34
41
  isAmpCapturing = true;
42
+ const event = args[0] as { nativeEvent?: { pageX?: unknown; pageY?: unknown } } | undefined;
43
+ const { pageX, pageY } = event?.nativeEvent ?? {};
44
+ const coordinates = typeof pageX === 'number' && typeof pageY === 'number' ? { x: pageX, y: pageY } : undefined;
35
45
  try {
36
- callbacks.forEach((callback) => callback(properties));
46
+ callbacks.forEach((callback) => (coordinates ? callback(properties, coordinates) : callback(properties)));
37
47
  } catch (error) {
38
48
  // swallow errors
39
49
  }
@@ -0,0 +1,27 @@
1
+ import type { AmpCaptureProperties } from '../amp-capture';
2
+ import {
3
+ SCREEN_NAME,
4
+ TARGET_ACCESSIBILITY_LABEL,
5
+ TARGET_ACTION,
6
+ TARGET_COMPONENT,
7
+ TARGET_ELEMENT,
8
+ TARGET_TEST_ID,
9
+ } from '../constants';
10
+
11
+ /**
12
+ * Map the properties captured by `ampCapture` to the `[Amplitude] ...` element event properties
13
+ * shared by `[Amplitude] Element Interacted` and the frustration events built on top of it.
14
+ */
15
+ export function getElementInteractionEventProperties(
16
+ properties: AmpCaptureProperties,
17
+ screenName: string | undefined,
18
+ ): Record<string, any> {
19
+ return {
20
+ [SCREEN_NAME]: screenName,
21
+ [TARGET_ACCESSIBILITY_LABEL]: properties.accessibilityLabel,
22
+ [TARGET_ACTION]: properties.action,
23
+ [TARGET_COMPONENT]: properties.component,
24
+ [TARGET_ELEMENT]: properties.element,
25
+ [TARGET_TEST_ID]: properties.testID,
26
+ };
27
+ }
@@ -0,0 +1,178 @@
1
+ import { getGlobalScope } from '@amplitude/analytics-core';
2
+
3
+ const ERROR_CLICK_TIMEOUT = 2_000;
4
+
5
+ export type ReactNativeErrorEvent = {
6
+ kind: 'error' | 'unhandledrejection' | 'console';
7
+ message?: string;
8
+ filename?: string;
9
+ lineNumber?: number;
10
+ columnNumber?: number;
11
+ stack?: string;
12
+ };
13
+
14
+ export type ErrorClickData = {
15
+ error: ReactNativeErrorEvent;
16
+ eventProperties: Record<string, any>;
17
+ };
18
+
19
+ export type ErrorClickEventProperties = {
20
+ '[Amplitude] Kind': ReactNativeErrorEvent['kind'];
21
+ '[Amplitude] Message'?: string;
22
+ '[Amplitude] Stack'?: string;
23
+ '[Amplitude] Filename'?: string;
24
+ '[Amplitude] Line Number'?: number;
25
+ '[Amplitude] Column Number'?: number;
26
+ };
27
+
28
+ type ErrorClickTracker = {
29
+ registerClickEvent: (eventProperties: Record<string, any>) => (() => void) | undefined;
30
+ registerErrorEvent: (error: ReactNativeErrorEvent) => void;
31
+ };
32
+
33
+ type ErrorUtilsGlobalHandler = (error: unknown, isFatal?: boolean) => void;
34
+
35
+ type ErrorUtilsLike = {
36
+ getGlobalHandler?: () => ErrorUtilsGlobalHandler | undefined;
37
+ setGlobalHandler?: (handler: ErrorUtilsGlobalHandler) => void;
38
+ };
39
+
40
+ type ErrorObserver = {
41
+ handler: (event: ReactNativeErrorEvent) => void;
42
+ };
43
+
44
+ type ErrorObserverState = {
45
+ observers: Set<ErrorObserver>;
46
+ previousHandler: ErrorUtilsGlobalHandler | undefined;
47
+ dispatcher: ErrorUtilsGlobalHandler;
48
+ };
49
+
50
+ const errorObserverStates = new WeakMap<ErrorUtilsLike, ErrorObserverState>();
51
+
52
+ export function getErrorClickEventProperties(
53
+ errorClickData: ErrorClickData,
54
+ ): ErrorClickEventProperties & Record<string, any> {
55
+ const { error, eventProperties } = errorClickData;
56
+ const errorEventProperties: ErrorClickEventProperties = {
57
+ '[Amplitude] Kind': error.kind,
58
+ '[Amplitude] Message': error.message,
59
+ '[Amplitude] Stack': error.stack,
60
+ '[Amplitude] Filename': error.filename,
61
+ '[Amplitude] Line Number': error.lineNumber,
62
+ '[Amplitude] Column Number': error.columnNumber,
63
+ };
64
+ return {
65
+ ...errorEventProperties,
66
+ ...eventProperties,
67
+ };
68
+ }
69
+
70
+ export function createErrorClickTracker(
71
+ errorClickHandler: (errorClickData: ErrorClickData) => void,
72
+ ): ErrorClickTracker {
73
+ let errorClickTimer: ReturnType<typeof setTimeout> | undefined;
74
+ let latestClickEventProperties: Record<string, any> | undefined;
75
+
76
+ function clearClickTimer() {
77
+ if (errorClickTimer) {
78
+ clearTimeout(errorClickTimer);
79
+ errorClickTimer = undefined;
80
+ }
81
+ latestClickEventProperties = undefined;
82
+ }
83
+
84
+ function registerClickEvent(eventProperties: Record<string, any>) {
85
+ clearClickTimer();
86
+ latestClickEventProperties = eventProperties;
87
+ errorClickTimer = setTimeout(clearClickTimer, ERROR_CLICK_TIMEOUT);
88
+ return clearClickTimer;
89
+ }
90
+
91
+ function registerErrorEvent(error: ReactNativeErrorEvent) {
92
+ if (!latestClickEventProperties) {
93
+ return;
94
+ }
95
+ errorClickHandler({
96
+ error,
97
+ eventProperties: latestClickEventProperties,
98
+ });
99
+ clearClickTimer();
100
+ }
101
+
102
+ return { registerClickEvent, registerErrorEvent };
103
+ }
104
+
105
+ const isDomException = (error: unknown): error is DOMException => {
106
+ return typeof DOMException !== 'undefined' && error instanceof DOMException;
107
+ };
108
+
109
+ const errorToEvent = (error: unknown): ReactNativeErrorEvent => {
110
+ if (error instanceof Error || isDomException(error)) {
111
+ return {
112
+ kind: 'error',
113
+ message: error.message,
114
+ stack: error.stack,
115
+ };
116
+ }
117
+ if (typeof error === 'string') {
118
+ return {
119
+ kind: 'error',
120
+ message: error,
121
+ };
122
+ }
123
+ return {
124
+ kind: 'error',
125
+ };
126
+ };
127
+
128
+ export const createUnhandledErrorObservable = (handler: (event: ReactNativeErrorEvent) => void): (() => void) => {
129
+ const errorUtils = (getGlobalScope() as { ErrorUtils?: ErrorUtilsLike } | undefined)?.ErrorUtils;
130
+
131
+ if (!errorUtils?.setGlobalHandler || !errorUtils?.getGlobalHandler) {
132
+ return () => undefined;
133
+ }
134
+
135
+ let state = errorObserverStates.get(errorUtils);
136
+ if (!state) {
137
+ const previousHandler = errorUtils.getGlobalHandler();
138
+ const observers = new Set<ErrorObserver>();
139
+ state = {
140
+ observers,
141
+ previousHandler: errorUtils.getGlobalHandler(),
142
+ dispatcher: (error, isFatal) => {
143
+ const event = errorToEvent(error);
144
+ for (const observer of Array.from(observers)) {
145
+ observer.handler(event);
146
+ }
147
+ previousHandler?.(error, isFatal);
148
+ },
149
+ };
150
+ errorObserverStates.set(errorUtils, state);
151
+ errorUtils.setGlobalHandler(state.dispatcher);
152
+ }
153
+
154
+ const observerState = state;
155
+ const observer = { handler };
156
+ observerState.observers.add(observer);
157
+ let isSubscribed = true;
158
+ return () => {
159
+ if (!isSubscribed) {
160
+ return;
161
+ }
162
+ isSubscribed = false;
163
+ observerState.observers.delete(observer);
164
+ if (observerState.observers.size === 0) {
165
+ if (errorUtils.getGlobalHandler?.() === observerState.dispatcher) {
166
+ errorUtils.setGlobalHandler?.(observerState.previousHandler ?? (() => undefined));
167
+ }
168
+ errorObserverStates.delete(errorUtils);
169
+ }
170
+ };
171
+ };
172
+
173
+ export const createErrorObservable = (handler: (event: ReactNativeErrorEvent) => void): (() => void) => {
174
+ const unsubscribeUnhandledError = createUnhandledErrorObservable(handler);
175
+ return () => {
176
+ unsubscribeUnhandledError();
177
+ };
178
+ };
@@ -0,0 +1,172 @@
1
+ import {
2
+ DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD,
3
+ DEFAULT_RAGE_CLICK_THRESHOLD,
4
+ DEFAULT_RAGE_CLICK_WINDOW_MS,
5
+ } from '@amplitude/analytics-core';
6
+ import type { AmpCaptureCoordinates } from '../amp-capture';
7
+
8
+ const CLICK_SIZE_LIMIT = 100;
9
+
10
+ // Captures Rage Clicks (when a button is pressed repeatedly within a certain time window)
11
+ type ClickEvent = {
12
+ coordinates: AmpCaptureCoordinates;
13
+ clickTimestamp: number;
14
+ eventProperties: Record<string, any>;
15
+ };
16
+
17
+ type ClickRegionBoundingBox = {
18
+ xMin: number;
19
+ xMax: number;
20
+ yMin: number;
21
+ yMax: number;
22
+ };
23
+
24
+ export type ClickProps = {
25
+ x: number;
26
+ y: number;
27
+ time: number;
28
+ };
29
+
30
+ export type RageClickData = {
31
+ clickCount: number;
32
+ clicks: ClickProps[];
33
+ eventProperties: Record<string, any>;
34
+ };
35
+
36
+ type RageClickTracker = {
37
+ registerClickEvent: (
38
+ coordinates: AmpCaptureCoordinates,
39
+ eventProperties: Record<string, any>,
40
+ ) => (() => void) | undefined;
41
+ };
42
+
43
+ export type RageClickMobileEventProperties = {
44
+ ['[Amplitude] Begin Time']: number;
45
+ ['[Amplitude] End Time']: number;
46
+ ['[Amplitude] Duration']: number;
47
+ ['[Amplitude] Click Count']: number;
48
+ ['[Amplitude] Clicks']: ClickProps[];
49
+ };
50
+
51
+ /**
52
+ * Build the event properties for a `[Amplitude] Rage Click` event from the tracker's click data.
53
+ * The element properties of the first click in the burst are merged in alongside the rage click properties.
54
+ */
55
+ export function getRageClickEventProperties(clickData: RageClickData): Record<string, any> {
56
+ const firstClickTime = clickData.clicks[0].time;
57
+ const lastClickTime = clickData.clicks[clickData.clicks.length - 1].time;
58
+ const rageClickProperties: RageClickMobileEventProperties = {
59
+ ['[Amplitude] Begin Time']: firstClickTime,
60
+ ['[Amplitude] End Time']: lastClickTime,
61
+ ['[Amplitude] Duration']: lastClickTime - firstClickTime,
62
+ ['[Amplitude] Click Count']: clickData.clickCount,
63
+ ['[Amplitude] Clicks']: clickData.clicks,
64
+ };
65
+ return { ...rageClickProperties, ...clickData.eventProperties };
66
+ }
67
+
68
+ export function getRageClickStartTime(clickData: RageClickData): number | undefined {
69
+ return clickData.clicks[0]?.time ?? undefined;
70
+ }
71
+
72
+ export function createRageClickTracker(rageClickHandler: (clickData: RageClickData) => void): RageClickTracker {
73
+ let clickWindow: ClickEvent[] = [];
74
+ let timeoutInterval: ReturnType<typeof setTimeout> | undefined;
75
+ let firstClickEvent: ClickEvent | undefined;
76
+ let clickCount = 0;
77
+ // Every click in the current burst (not trimmed like clickWindow) so the event can report all of them.
78
+ let clicks: ClickProps[] = [];
79
+ let pendingRageClickData: RageClickData | undefined;
80
+ let clickBoundingBox: ClickRegionBoundingBox | undefined;
81
+
82
+ function flush() {
83
+ if (pendingRageClickData) {
84
+ rageClickHandler(pendingRageClickData);
85
+ pendingRageClickData = undefined;
86
+ timeoutInterval && clearTimeout(timeoutInterval);
87
+ }
88
+ clickWindow = [];
89
+ clickCount = 0;
90
+ clicks = [];
91
+ firstClickEvent = undefined;
92
+ clickBoundingBox = undefined;
93
+ }
94
+
95
+ /**
96
+ * Register a click event
97
+ * @param coordinates - The page coordinates where the click occurred
98
+ * @returns
99
+ */
100
+ function registerClickEvent(coordinates: AmpCaptureCoordinates, eventProperties: Record<string, any>) {
101
+ const now = Date.now();
102
+
103
+ // If the click is outside of the rage click time window or 50px region, flush the window.
104
+ const firstClickInWindow = clickWindow.length > 0 ? clickWindow[0] : null;
105
+ const isOutsideWindow =
106
+ firstClickInWindow && now - firstClickInWindow.clickTimestamp > DEFAULT_RAGE_CLICK_WINDOW_MS;
107
+ const nextBoundingBox = clickBoundingBox
108
+ ? {
109
+ xMin: Math.min(clickBoundingBox.xMin, coordinates.x),
110
+ xMax: Math.max(clickBoundingBox.xMax, coordinates.x),
111
+ yMin: Math.min(clickBoundingBox.yMin, coordinates.y),
112
+ yMax: Math.max(clickBoundingBox.yMax, coordinates.y),
113
+ }
114
+ : { xMin: coordinates.x, xMax: coordinates.x, yMin: coordinates.y, yMax: coordinates.y };
115
+ const isOutsideBoundingBox =
116
+ nextBoundingBox.xMax - nextBoundingBox.xMin > DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD ||
117
+ nextBoundingBox.yMax - nextBoundingBox.yMin > DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD;
118
+ if (isOutsideBoundingBox || isOutsideWindow) {
119
+ flush();
120
+ }
121
+
122
+ const clickEvent: ClickEvent = {
123
+ coordinates,
124
+ clickTimestamp: now,
125
+ eventProperties,
126
+ };
127
+ if (clickWindow.length === 0) {
128
+ firstClickEvent = clickEvent;
129
+ clickBoundingBox = {
130
+ xMin: coordinates.x,
131
+ xMax: coordinates.x,
132
+ yMin: coordinates.y,
133
+ yMax: coordinates.y,
134
+ };
135
+ } else {
136
+ clickBoundingBox = nextBoundingBox;
137
+ }
138
+
139
+ // add this click to the window; only keep the 4 most recent clicks to save memory
140
+ clickWindow.push(clickEvent);
141
+ clickCount++;
142
+ clicks.push({ x: coordinates.x, y: coordinates.y, time: now });
143
+ if (clicks.length > CLICK_SIZE_LIMIT) {
144
+ clicks.shift();
145
+ }
146
+ if (clickWindow.length > DEFAULT_RAGE_CLICK_THRESHOLD) {
147
+ clickWindow.shift();
148
+ }
149
+
150
+ // if below threshold, return
151
+ if (clickWindow.length < DEFAULT_RAGE_CLICK_THRESHOLD) {
152
+ return;
153
+ }
154
+
155
+ // we found a rage click, save the callback data for later
156
+ pendingRageClickData = {
157
+ clickCount,
158
+ clicks: [...clicks],
159
+ eventProperties: firstClickEvent?.eventProperties ?? {},
160
+ };
161
+
162
+ // debounce: only send the rage click once there have been no clicks for a full window
163
+ timeoutInterval && clearTimeout(timeoutInterval);
164
+ timeoutInterval = setTimeout(flush, DEFAULT_RAGE_CLICK_WINDOW_MS);
165
+
166
+ return () => {
167
+ clearTimeout(timeoutInterval);
168
+ };
169
+ }
170
+
171
+ return { registerClickEvent };
172
+ }
@@ -78,6 +78,7 @@ export function updateReactNativeConfigWithRemoteConfig(
78
78
  appLifecycles: reactNativeConfig.autocapture,
79
79
  elementInteractions: reactNativeConfig.autocapture,
80
80
  networkTracking: reactNativeConfig.autocapture,
81
+ frustrationInteractions: reactNativeConfig.autocapture,
81
82
  ...transformedAutocaptureRemoteConfig,
82
83
  };
83
84
  }
package/src/constants.ts CHANGED
@@ -6,6 +6,8 @@ export const DEFAULT_APPLICATION_INSTALLED_EVENT = `${DEFAULT_EVENT_PREFIX} Appl
6
6
  export const DEFAULT_APPLICATION_UPDATED_EVENT = `${DEFAULT_EVENT_PREFIX} Application Updated`;
7
7
  export const DEFAULT_SCREEN_VIEWED_EVENT = `${DEFAULT_EVENT_PREFIX} Screen Viewed`;
8
8
  export const DEFAULT_ELEMENT_INTERACTED_EVENT = `${DEFAULT_EVENT_PREFIX} Element Interacted`;
9
+ export const DEFAULT_ELEMENT_RAGE_CLICKED_EVENT = `${DEFAULT_EVENT_PREFIX} Rage Click`;
10
+ export const DEFAULT_ELEMENT_ERROR_CLICKED_EVENT = `${DEFAULT_EVENT_PREFIX} Error Click`;
9
11
  export const SCREEN_NAME = `${DEFAULT_EVENT_PREFIX} Screen Name`;
10
12
  export const TARGET_ACCESSIBILITY_LABEL = `${DEFAULT_EVENT_PREFIX} Target Accessibility Label`;
11
13
  export const TARGET_ACTION = `${DEFAULT_EVENT_PREFIX} Action`;
@@ -0,0 +1,106 @@
1
+ import {
2
+ EnrichmentPlugin,
3
+ ReactNativeClient,
4
+ ReactNativeConfig,
5
+ ReactNativeFrustrationInteractionsOptions,
6
+ } from '@amplitude/analytics-core';
7
+ import * as Capture from '../amp-capture';
8
+ import {
9
+ createErrorClickTracker,
10
+ createErrorObservable,
11
+ getErrorClickEventProperties,
12
+ } from '../autocapture/error-click';
13
+ import { getElementInteractionEventProperties } from '../autocapture/element-interaction';
14
+ import { createRageClickTracker, getRageClickEventProperties, getRageClickStartTime } from '../autocapture/rage-click';
15
+ import { DEFAULT_ELEMENT_ERROR_CLICKED_EVENT, DEFAULT_ELEMENT_RAGE_CLICKED_EVENT } from '../constants';
16
+
17
+ export const FRUSTRATION_PLUGIN_NAME = '@amplitude/plugin-frustration-react-native';
18
+
19
+ type ReactNativeEnrichmentPlugin = EnrichmentPlugin<ReactNativeClient, ReactNativeConfig>;
20
+
21
+ export interface FrustrationPluginOptions extends ReactNativeFrustrationInteractionsOptions {
22
+ errorClick?: boolean;
23
+ /**
24
+ * Returns the name of the screen currently being viewed so it can be attached to
25
+ * frustration events as `[Amplitude] Screen Name`.
26
+ */
27
+ getScreenName?: () => string | undefined;
28
+ }
29
+
30
+ /**
31
+ * Tracks frustration interactions from presses
32
+ * captured through `ampCapture`.
33
+ */
34
+ export const frustrationPlugin = (options: FrustrationPluginOptions = {}): ReactNativeEnrichmentPlugin => {
35
+ const name = FRUSTRATION_PLUGIN_NAME;
36
+ const type = 'enrichment';
37
+
38
+ const rageClicksEnabled = options.rageClick !== false;
39
+ const errorClicksEnabled = options.errorClick !== false;
40
+
41
+ let subscriptions: (() => void)[] = [];
42
+
43
+ const setup: ReactNativeEnrichmentPlugin['setup'] = async (config, amplitude) => {
44
+ if (rageClicksEnabled) {
45
+ const rageClickTracker = createRageClickTracker((clickData) => {
46
+ const time = getRageClickStartTime(clickData);
47
+ const eventProperties = getRageClickEventProperties(clickData);
48
+ amplitude.track(DEFAULT_ELEMENT_RAGE_CLICKED_EVENT, eventProperties, { time });
49
+ });
50
+
51
+ let unregisterRageClickEvent: (() => void) | undefined;
52
+ const captureUnsubscribe = Capture.subscribe((properties, coordinates) => {
53
+ if (properties.action !== 'Press' || !coordinates) {
54
+ return;
55
+ }
56
+ unregisterRageClickEvent = rageClickTracker.registerClickEvent(
57
+ coordinates,
58
+ getElementInteractionEventProperties(properties, options.getScreenName?.()),
59
+ );
60
+ });
61
+
62
+ subscriptions.push(captureUnsubscribe, () => unregisterRageClickEvent?.());
63
+ }
64
+
65
+ if (errorClicksEnabled) {
66
+ const errorClickTracker = createErrorClickTracker((errorClickData) => {
67
+ amplitude.track(DEFAULT_ELEMENT_ERROR_CLICKED_EVENT, getErrorClickEventProperties(errorClickData));
68
+ });
69
+
70
+ let unregisterErrorClickEvent: (() => void) | undefined;
71
+ const captureUnsubscribe = Capture.subscribe((properties) => {
72
+ if (properties.action !== 'Press' && properties.action !== 'LongPress') {
73
+ return;
74
+ }
75
+ unregisterErrorClickEvent = errorClickTracker.registerClickEvent(
76
+ getElementInteractionEventProperties(properties, options.getScreenName?.()),
77
+ );
78
+ });
79
+ const errorUnsubscribe = createErrorObservable(errorClickTracker.registerErrorEvent);
80
+
81
+ subscriptions.push(captureUnsubscribe, errorUnsubscribe, () => unregisterErrorClickEvent?.());
82
+ }
83
+
84
+ /* istanbul ignore next */
85
+ config?.loggerProvider?.log(`${name} has been successfully added.`);
86
+ };
87
+
88
+ const execute: ReactNativeEnrichmentPlugin['execute'] = async (event) => {
89
+ return event;
90
+ };
91
+
92
+ const teardown = async () => {
93
+ for (const unsubscribe of subscriptions) {
94
+ unsubscribe();
95
+ }
96
+ subscriptions = [];
97
+ };
98
+
99
+ return {
100
+ name,
101
+ type,
102
+ setup,
103
+ execute,
104
+ teardown,
105
+ };
106
+ };
@@ -41,6 +41,7 @@ import { plugin as networkCapturePlugin } from '@amplitude/plugin-network-captur
41
41
  import { CampaignTracker } from './campaign/campaign-tracker';
42
42
  import { Context } from './plugins/context';
43
43
  import { networkConnectivityCheckerPlugin } from './plugins/network-connectivity-checker';
44
+ import { frustrationPlugin } from './plugins/frustration-plugin';
44
45
  import { ReactNativeDiagnosticsStorage } from './diagnostics/diagnostics-storage';
45
46
  import { LIBPREFIX } from './lib-prefix';
46
47
  import { VERSION } from './version';
@@ -50,6 +51,7 @@ import { parseOldCookies } from './cookie-migration';
50
51
  import { isNative } from './utils/platform';
51
52
  import { RemoteConfigCustomStorage } from './remote-config/remote-config-customstorage';
52
53
  import * as Capture from './amp-capture';
54
+ import { getElementInteractionEventProperties } from './autocapture/element-interaction';
53
55
  import {
54
56
  APP_BUILD,
55
57
  APP_VERSION,
@@ -64,11 +66,6 @@ import {
64
66
  PREVIOUS_BUILD,
65
67
  PREVIOUS_VERSION,
66
68
  SCREEN_NAME,
67
- TARGET_ACCESSIBILITY_LABEL,
68
- TARGET_ACTION,
69
- TARGET_COMPONENT,
70
- TARGET_ELEMENT,
71
- TARGET_TEST_ID,
72
69
  } from './constants';
73
70
 
74
71
  /**
@@ -300,6 +297,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
300
297
  screenViews: true,
301
298
  elementInteractions: true,
302
299
  networkTracking: true,
300
+ frustrationInteractions: true,
303
301
  };
304
302
  } else if (autocaptureConfig && typeof autocaptureConfig === 'object') {
305
303
  this.autocapture = autocaptureConfig;
@@ -342,17 +340,23 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
342
340
  await this.add(networkCapturePlugin(getNetworkTrackingConfig(this.config))).promise;
343
341
  }
344
342
 
343
+ const frustrationInteractions = this.autocapture?.frustrationInteractions;
344
+ if (frustrationInteractions) {
345
+ this.config.loggerProvider.debug('Adding frustration interactions plugin');
346
+ await this.add(
347
+ frustrationPlugin({
348
+ ...(typeof frustrationInteractions === 'object' ? frustrationInteractions : {}),
349
+ getScreenName: () => this.currentScreenName,
350
+ }),
351
+ ).promise;
352
+ }
353
+
345
354
  if (this.autocapture?.elementInteractions === true) {
346
355
  this.captureUnsubscribe = Capture.subscribe((properties) => {
347
- const analyticsProps = {
348
- [SCREEN_NAME]: this.currentScreenName,
349
- [TARGET_ACCESSIBILITY_LABEL]: properties.accessibilityLabel,
350
- [TARGET_ACTION]: properties.action,
351
- [TARGET_COMPONENT]: properties.component,
352
- [TARGET_ELEMENT]: properties.element,
353
- [TARGET_TEST_ID]: properties.testID,
354
- };
355
- this.track(DEFAULT_ELEMENT_INTERACTED_EVENT, analyticsProps);
356
+ this.track(
357
+ DEFAULT_ELEMENT_INTERACTED_EVENT,
358
+ getElementInteractionEventProperties(properties, this.currentScreenName),
359
+ );
356
360
  });
357
361
  }
358
362
 
@@ -396,6 +400,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
396
400
  shutdown() {
397
401
  this.appStateChangeHandler?.remove();
398
402
  this.captureUnsubscribe?.();
403
+ this.timeline.reset(this);
399
404
  }
400
405
 
401
406
  async runAttributionStrategy(attributionConfig?: AttributionOptions, isNewSession = false) {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.11.0-video-analytics-5.0';
1
+ export const VERSION = '1.12.0';