@amplitude/analytics-react-native 1.6.7 → 1.6.8

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 (56) hide show
  1. package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativeModule.kt +1 -0
  2. package/android/src/main/java/com/amplitude/reactnative/AndroidContextProvider.kt +19 -0
  3. package/ios/AmplitudeReactNative.swift +1 -0
  4. package/ios/AppleContextProvider.swift +5 -0
  5. package/lib/commonjs/amp-capture.js +42 -0
  6. package/lib/commonjs/amp-capture.js.map +1 -0
  7. package/lib/commonjs/config/joined-config.js +76 -0
  8. package/lib/commonjs/config/joined-config.js.map +1 -0
  9. package/lib/commonjs/config.js +48 -2
  10. package/lib/commonjs/config.js.map +1 -1
  11. package/lib/commonjs/constants.js +13 -1
  12. package/lib/commonjs/constants.js.map +1 -1
  13. package/lib/commonjs/index.js +7 -0
  14. package/lib/commonjs/index.js.map +1 -1
  15. package/lib/commonjs/plugins/context.js.map +1 -1
  16. package/lib/commonjs/react-native-client.js +159 -13
  17. package/lib/commonjs/react-native-client.js.map +1 -1
  18. package/lib/commonjs/version.js +1 -1
  19. package/lib/module/amp-capture.js +35 -0
  20. package/lib/module/amp-capture.js.map +1 -0
  21. package/lib/module/config/joined-config.js +71 -0
  22. package/lib/module/config/joined-config.js.map +1 -0
  23. package/lib/module/config.js +46 -1
  24. package/lib/module/config.js.map +1 -1
  25. package/lib/module/constants.js +12 -0
  26. package/lib/module/constants.js.map +1 -1
  27. package/lib/module/index.js +1 -0
  28. package/lib/module/index.js.map +1 -1
  29. package/lib/module/plugins/context.js.map +1 -1
  30. package/lib/module/react-native-client.js +162 -17
  31. package/lib/module/react-native-client.js.map +1 -1
  32. package/lib/module/version.js +1 -1
  33. package/lib/typescript/amp-capture.d.ts +10 -0
  34. package/lib/typescript/amp-capture.d.ts.map +1 -0
  35. package/lib/typescript/config/joined-config.d.ts +14 -0
  36. package/lib/typescript/config/joined-config.d.ts.map +1 -0
  37. package/lib/typescript/config.d.ts +19 -2
  38. package/lib/typescript/config.d.ts.map +1 -1
  39. package/lib/typescript/constants.d.ts +12 -0
  40. package/lib/typescript/constants.d.ts.map +1 -1
  41. package/lib/typescript/index.d.ts +1 -0
  42. package/lib/typescript/index.d.ts.map +1 -1
  43. package/lib/typescript/plugins/context.d.ts +1 -0
  44. package/lib/typescript/plugins/context.d.ts.map +1 -1
  45. package/lib/typescript/react-native-client.d.ts +7 -0
  46. package/lib/typescript/react-native-client.d.ts.map +1 -1
  47. package/lib/typescript/version.d.ts +1 -1
  48. package/package.json +3 -3
  49. package/src/amp-capture.ts +48 -0
  50. package/src/config/joined-config.ts +101 -0
  51. package/src/config.ts +48 -4
  52. package/src/constants.ts +12 -0
  53. package/src/index.ts +2 -0
  54. package/src/plugins/context.ts +1 -0
  55. package/src/react-native-client.ts +204 -5
  56. package/src/version.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { AppState, AppStateStatus, NativeEventSubscription } from 'react-native';
1
+ import { AppState, AppStateStatus, NativeEventSubscription, NativeModules, Platform } from 'react-native';
2
2
  import {
3
3
  AmplitudeCore,
4
4
  Destination,
@@ -23,26 +23,46 @@ import {
23
23
  SpecialEventType,
24
24
  AnalyticsClient,
25
25
  OfflineDisabled,
26
+ Logger,
27
+ LogLevel,
28
+ IRemoteConfigClient,
29
+ RemoteConfigClient,
30
+ RemoteConfig,
31
+ Source,
26
32
  ReactNativeAutocaptureOptions,
27
- ReactNativeConfigAutocaptureBeta,
28
33
  NavigationState,
29
34
  NetworkTrackingOptions,
30
35
  normalizeNetworkCaptureRules,
36
+ safeJsonStringify,
31
37
  } from '@amplitude/analytics-core';
32
38
  import { plugin as networkCapturePlugin } from '@amplitude/plugin-network-capture-browser';
33
39
  import { CampaignTracker } from './campaign/campaign-tracker';
34
40
  import { Context } from './plugins/context';
35
41
  import { networkConnectivityCheckerPlugin } from './plugins/network-connectivity-checker';
36
- import { useReactNativeConfig, createCookieStorage } from './config';
42
+ import { useReactNativeConfig, createCookieStorage, shouldFetchRemoteConfig } from './config';
43
+ import { updateReactNativeConfigWithRemoteConfig } from './config/joined-config';
37
44
  import { parseOldCookies } from './cookie-migration';
38
45
  import { isNative } from './utils/platform';
46
+ import * as Capture from './amp-capture';
39
47
  import {
48
+ APP_BUILD,
49
+ APP_VERSION,
40
50
  DEFAULT_APPLICATION_BACKGROUNDED_EVENT,
51
+ DEFAULT_APPLICATION_INSTALLED_EVENT,
41
52
  DEFAULT_APPLICATION_OPENED_EVENT,
53
+ DEFAULT_ELEMENT_INTERACTED_EVENT,
54
+ DEFAULT_APPLICATION_UPDATED_EVENT,
42
55
  DEFAULT_SCREEN_VIEWED_EVENT,
43
56
  DEFAULT_SESSION_END_EVENT,
44
57
  DEFAULT_SESSION_START_EVENT,
58
+ PREVIOUS_BUILD,
59
+ PREVIOUS_VERSION,
45
60
  SCREEN_NAME,
61
+ TARGET_ACCESSIBILITY_LABEL,
62
+ TARGET_ACTION,
63
+ TARGET_COMPONENT,
64
+ TARGET_ELEMENT,
65
+ TARGET_TEST_ID,
46
66
  } from './constants';
47
67
 
48
68
  /**
@@ -65,7 +85,9 @@ const getActiveRouteName = (navigationState: NavigationState): string | undefine
65
85
  return routeName;
66
86
  };
67
87
 
68
- const getNetworkTrackingConfig = (config: ReactNativeConfigAutocaptureBeta): NetworkTrackingOptions | undefined => {
88
+ // TODO: Remove IS_DIAGNOSTICS_CAPTURED when we're ready for diagnostics capture
89
+ const IS_DIAGNOSTICS_CAPTURED = false;
90
+ const getNetworkTrackingConfig = (config: ReactNativeConfig): NetworkTrackingOptions | undefined => {
69
91
  let networkTrackingConfig;
70
92
  if (typeof config.autocapture === 'object' && typeof config.autocapture.networkTracking === 'object') {
71
93
  networkTrackingConfig = config.autocapture.networkTracking;
@@ -91,12 +113,14 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
91
113
  */
92
114
  private lastNavigationScreenName: string | undefined;
93
115
  private appStateChangeHandler: NativeEventSubscription | undefined;
116
+ private currentScreenName: string | undefined;
94
117
  explicitSessionId: number | undefined;
95
118
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
96
119
  // @ts-ignore
97
120
  config: ReactNativeConfig;
98
121
  userProperties: { [key: string]: any } | undefined;
99
122
  autocapture: ReactNativeAutocaptureOptions | null = null;
123
+ captureUnsubscribe: (() => void) | undefined;
100
124
 
101
125
  init(apiKey = '', userId?: string, options?: ReactNativeOptions) {
102
126
  return returnWrapper(this._init({ ...options, userId, apiKey }));
@@ -109,26 +133,121 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
109
133
  this.initializing = true;
110
134
  this.explicitSessionId = options.sessionId;
111
135
 
136
+ // Step 0.1: Get remote config dependencies ready
137
+ const fetchRemoteConfig = shouldFetchRemoteConfig(options);
138
+ const loggerProvider = options.loggerProvider ?? new Logger();
139
+ if (!options.loggerProvider) {
140
+ loggerProvider.enable(options.logLevel ?? LogLevel.Warn);
141
+ }
142
+ const serverZone = options.serverZone ?? 'US';
143
+ let remoteConfigClient: IRemoteConfigClient | undefined;
144
+
145
+ // Step 0.2: Fetch diagnostics config
146
+ // let diagnosticsSampleRate: number;
147
+ // let enableDiagnostics: boolean = false;
148
+ if (fetchRemoteConfig) {
149
+ remoteConfigClient = new RemoteConfigClient(
150
+ options.apiKey,
151
+ loggerProvider,
152
+ serverZone,
153
+ /* istanbul ignore next */ options.remoteConfig?.serverUrl,
154
+ );
155
+ // Diagnostics capture is intentionally disabled for React Native until ready.
156
+ /* istanbul ignore if */
157
+ if (IS_DIAGNOSTICS_CAPTURED) {
158
+ await new Promise<void>((resolve) => {
159
+ remoteConfigClient?.subscribe(
160
+ 'configs.diagnostics.reactNativeSDK',
161
+ 'all',
162
+ (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => {
163
+ loggerProvider.debug(
164
+ 'Diagnostics remote configuration received:',
165
+ safeJsonStringify(
166
+ {
167
+ remoteConfig,
168
+ source,
169
+ lastFetch,
170
+ },
171
+ null,
172
+ 2,
173
+ ),
174
+ );
175
+ if (remoteConfig) {
176
+ // Validate and set sampleRate (must be a valid number)
177
+ // const sampleRate = remoteConfig.sampleRate as number;
178
+ // if (typeof sampleRate === 'number' && !isNaN(sampleRate)) {
179
+ // diagnosticsSampleRate = sampleRate;
180
+ // }
181
+ // // Validate and set enabled (must be a boolean)
182
+ // const enabled = remoteConfig.enabled as boolean;
183
+ // if (typeof enabled === 'boolean') {
184
+ // enableDiagnostics = enabled;
185
+ // }
186
+ }
187
+ resolve();
188
+ },
189
+ );
190
+ });
191
+ }
192
+ }
193
+
112
194
  // Step 1: Read cookies stored by old SDK
113
195
  const oldCookies = await parseOldCookies(options.apiKey, options);
114
196
 
115
197
  // Step 2: Create react native config
198
+ // Pass early loggerProvider/serverZone so RemoteConfigClient and final config share the same instances
199
+ // (mirrors browser earlyConfig).
116
200
  const reactNativeOptions = await useReactNativeConfig(options.apiKey, {
117
201
  ...options,
202
+ loggerProvider,
203
+ serverZone,
118
204
  deviceId: options.deviceId ?? oldCookies.deviceId,
119
205
  sessionId: oldCookies.sessionId,
120
206
  optOut: options.optOut ?? oldCookies.optOut,
121
207
  lastEventTime: oldCookies.lastEventTime,
122
208
  userId: options.userId ?? oldCookies.userId,
123
209
  });
210
+
211
+ // Step 2.1: Fetch remote config
212
+ if (fetchRemoteConfig && remoteConfigClient) {
213
+ await new Promise<void>((resolve) => {
214
+ remoteConfigClient?.subscribe(
215
+ 'configs.analyticsSDK.reactNativeSDK',
216
+ 'all',
217
+ (remoteConfig: RemoteConfig | null, source: Source, lastFetch: Date) => {
218
+ loggerProvider.debug(
219
+ 'Remote configuration received:',
220
+ safeJsonStringify(
221
+ {
222
+ remoteConfig,
223
+ source,
224
+ lastFetch,
225
+ },
226
+ null,
227
+ 2,
228
+ ),
229
+ );
230
+ if (remoteConfig) {
231
+ updateReactNativeConfigWithRemoteConfig(remoteConfig, reactNativeOptions);
232
+ }
233
+ resolve();
234
+ },
235
+ );
236
+ });
237
+ }
238
+
124
239
  await super._init(reactNativeOptions);
240
+ this.config.remoteConfigClient = remoteConfigClient;
125
241
 
126
242
  // Step 2.1: parse autocapture config (always reset so re-init clears prior flags)
127
- const autocaptureConfig = (this.config as ReactNativeConfigAutocaptureBeta).autocapture;
243
+ const autocaptureConfig = this.config.autocapture;
128
244
  if (autocaptureConfig === true) {
129
245
  this.autocapture = {
130
246
  appLifecycles: true,
131
247
  sessions: true,
248
+ screenViews: true,
249
+ elementInteractions: true,
250
+ networkTracking: true,
132
251
  };
133
252
  } else if (autocaptureConfig && typeof autocaptureConfig === 'object') {
134
253
  this.autocapture = autocaptureConfig;
@@ -139,6 +258,12 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
139
258
  this.wasBackgrounded = false;
140
259
  // Allow the first navigation screen view after re-init.
141
260
  this.lastNavigationScreenName = undefined;
261
+ this.currentScreenName = undefined;
262
+
263
+ if (typeof this.captureUnsubscribe === 'function') {
264
+ this.captureUnsubscribe();
265
+ this.captureUnsubscribe = undefined;
266
+ }
142
267
 
143
268
  // Set up the analytics connector to integrate with the experiment SDK.
144
269
  // Send events from the experiment SDK and forward identifies to the
@@ -165,6 +290,20 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
165
290
  await this.add(networkCapturePlugin(getNetworkTrackingConfig(this.config))).promise;
166
291
  }
167
292
 
293
+ if (this.autocapture?.elementInteractions === true) {
294
+ this.captureUnsubscribe = Capture.subscribe((properties) => {
295
+ const analyticsProps = {
296
+ [SCREEN_NAME]: this.currentScreenName,
297
+ [TARGET_ACCESSIBILITY_LABEL]: properties.accessibilityLabel,
298
+ [TARGET_ACTION]: properties.action,
299
+ [TARGET_COMPONENT]: properties.component,
300
+ [TARGET_ELEMENT]: properties.element,
301
+ [TARGET_TEST_ID]: properties.testID,
302
+ };
303
+ this.track(DEFAULT_ELEMENT_INTERACTED_EVENT, analyticsProps);
304
+ });
305
+ }
306
+
168
307
  // Step 4: Manage session
169
308
  this.appState = AppState.currentState;
170
309
  const isNewSession = this.startNewSessionIfNeeded(this.currentTimeMillis());
@@ -173,6 +312,9 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
173
312
  this.getSessionId() ?? 'undefined'
174
313
  }`,
175
314
  );
315
+ if (this.appStateChangeHandler) {
316
+ this.appStateChangeHandler.remove?.();
317
+ }
176
318
  this.appStateChangeHandler = AppState.addEventListener('change', this.handleAppStateChange);
177
319
 
178
320
  this.initializing = false;
@@ -185,6 +327,8 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
185
327
  this.track('[Amplitude] Application Opened');
186
328
  }
187
329
 
330
+ await this.trackAppInstallOrUpdate();
331
+
188
332
  // Step 5.2: run attribution strategy
189
333
  await this.runAttributionStrategy(options.attribution, isNewSession);
190
334
 
@@ -199,6 +343,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
199
343
 
200
344
  shutdown() {
201
345
  this.appStateChangeHandler?.remove();
346
+ this.captureUnsubscribe?.();
202
347
  }
203
348
 
204
349
  async runAttributionStrategy(attributionConfig?: AttributionOptions, isNewSession = false) {
@@ -260,6 +405,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
260
405
  this.setDeviceId(UUID());
261
406
  // Allow the current focused route to emit again for the new identity.
262
407
  this.lastNavigationScreenName = undefined;
408
+ this.currentScreenName = undefined;
263
409
  }
264
410
 
265
411
  getSessionId() {
@@ -301,6 +447,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
301
447
  if (isScreenViewTrackingDisabled) {
302
448
  return returnWrapper(Promise.resolve(undefined));
303
449
  }
450
+ this.currentScreenName = screenName;
304
451
  return this.track(
305
452
  DEFAULT_SCREEN_VIEWED_EVENT,
306
453
  {
@@ -451,6 +598,58 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
451
598
  }
452
599
  };
453
600
 
601
+ /**
602
+ * Tracks Application Installed / Updated from persisted version + build on UserSession.
603
+ * Only runs on iOS/Android; skips web and when the native module is unavailable.
604
+ */
605
+ private async trackAppInstallOrUpdate() {
606
+ if (Platform.OS !== 'ios' && Platform.OS !== 'android') {
607
+ return;
608
+ }
609
+
610
+ const nativeModule = NativeModules.AmplitudeReactNative as
611
+ | { getApplicationContext: (options: unknown) => Promise<Record<string, string> | undefined> }
612
+ | undefined;
613
+ if (this.autocapture?.appLifecycles !== true || !nativeModule?.getApplicationContext) {
614
+ return;
615
+ }
616
+
617
+ const nativeContext = await nativeModule.getApplicationContext(this.config.trackingOptions);
618
+ const version = nativeContext?.version;
619
+ const build = nativeContext?.build;
620
+ const previousVersion = this.config.persistedAppVersion;
621
+ const previousBuild = this.config.persistedAppBuild;
622
+ // iOS (Amplitude-Swift): missing previous version or build => install; either changing => update.
623
+ // Android (Amplitude-Kotlin): build-only — missing previous build => install; build change => update.
624
+ const isIOS = Platform.OS === 'ios';
625
+ const isInstall = isIOS
626
+ ? previousBuild === undefined || previousVersion === undefined
627
+ : previousBuild === undefined;
628
+ const isUpdate = isIOS
629
+ ? (build !== undefined && previousBuild !== build) || (version !== undefined && previousVersion !== version)
630
+ : build !== undefined && previousBuild !== build;
631
+
632
+ if (isInstall) {
633
+ this.track(DEFAULT_APPLICATION_INSTALLED_EVENT, {
634
+ [APP_VERSION]: version,
635
+ [APP_BUILD]: build,
636
+ });
637
+ } else if (isUpdate) {
638
+ this.track(DEFAULT_APPLICATION_UPDATED_EVENT, {
639
+ [APP_VERSION]: version,
640
+ [APP_BUILD]: build,
641
+ [PREVIOUS_VERSION]: previousVersion,
642
+ [PREVIOUS_BUILD]: previousBuild,
643
+ });
644
+ }
645
+ if (version !== undefined) {
646
+ this.config.persistedAppVersion = version;
647
+ }
648
+ if (build !== undefined) {
649
+ this.config.persistedAppBuild = build;
650
+ }
651
+ }
652
+
454
653
  private enterForeground(timestamp: number) {
455
654
  this.config.loggerProvider?.log('App Activated');
456
655
  // Only emit Application Opened after a real background (not inactive→active).
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.6.7';
1
+ export const VERSION = '1.6.8';