@amplitude/analytics-react-native 1.6.5 → 1.6.7
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.
- package/lib/commonjs/config.js +1 -0
- package/lib/commonjs/config.js.map +1 -1
- package/lib/commonjs/constants.js +14 -0
- package/lib/commonjs/constants.js.map +1 -0
- package/lib/commonjs/index.js +6 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/migration/remnant-data-migration.js.map +1 -1
- package/lib/commonjs/react-native-client.js +118 -9
- package/lib/commonjs/react-native-client.js.map +1 -1
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/module/config.js +1 -0
- package/lib/module/config.js.map +1 -1
- package/lib/module/constants.js +8 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/index.js +3 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/migration/remnant-data-migration.js.map +1 -1
- package/lib/module/react-native-client.js +120 -10
- package/lib/module/react-native-client.js.map +1 -1
- package/lib/module/types.js +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/typescript/config.d.ts +2 -1
- package/lib/typescript/config.d.ts.map +1 -1
- package/lib/typescript/constants.d.ts +8 -0
- package/lib/typescript/constants.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +1 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/migration/remnant-data-migration.d.ts.map +1 -1
- package/lib/typescript/react-native-client.d.ts +16 -1
- package/lib/typescript/react-native-client.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +1 -1
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +3 -2
- package/src/config.ts +4 -0
- package/src/constants.ts +9 -0
- package/src/index.ts +2 -0
- package/src/migration/remnant-data-migration.ts +1 -2
- package/src/react-native-client.ts +149 -7
- package/src/types.ts +1 -0
- package/src/version.ts +1 -1
|
@@ -23,25 +23,80 @@ import {
|
|
|
23
23
|
SpecialEventType,
|
|
24
24
|
AnalyticsClient,
|
|
25
25
|
OfflineDisabled,
|
|
26
|
+
ReactNativeAutocaptureOptions,
|
|
27
|
+
ReactNativeConfigAutocaptureBeta,
|
|
28
|
+
NavigationState,
|
|
29
|
+
NetworkTrackingOptions,
|
|
30
|
+
normalizeNetworkCaptureRules,
|
|
26
31
|
} from '@amplitude/analytics-core';
|
|
32
|
+
import { plugin as networkCapturePlugin } from '@amplitude/plugin-network-capture-browser';
|
|
27
33
|
import { CampaignTracker } from './campaign/campaign-tracker';
|
|
28
34
|
import { Context } from './plugins/context';
|
|
29
35
|
import { networkConnectivityCheckerPlugin } from './plugins/network-connectivity-checker';
|
|
30
36
|
import { useReactNativeConfig, createCookieStorage } from './config';
|
|
31
37
|
import { parseOldCookies } from './cookie-migration';
|
|
32
38
|
import { isNative } from './utils/platform';
|
|
39
|
+
import {
|
|
40
|
+
DEFAULT_APPLICATION_BACKGROUNDED_EVENT,
|
|
41
|
+
DEFAULT_APPLICATION_OPENED_EVENT,
|
|
42
|
+
DEFAULT_SCREEN_VIEWED_EVENT,
|
|
43
|
+
DEFAULT_SESSION_END_EVENT,
|
|
44
|
+
DEFAULT_SESSION_START_EVENT,
|
|
45
|
+
SCREEN_NAME,
|
|
46
|
+
} from './constants';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Walks React Navigation state to the focused leaf route.
|
|
50
|
+
* Nested navigators (tabs, stacks) store the visible screen in child `state`.
|
|
51
|
+
*/
|
|
52
|
+
const getActiveRouteName = (navigationState: NavigationState): string | undefined => {
|
|
53
|
+
let state: NavigationState | undefined = navigationState;
|
|
54
|
+
let routeName: string | undefined;
|
|
55
|
+
|
|
56
|
+
while (state?.routes) {
|
|
57
|
+
const route = state.routes[state.index] as { name: string; state?: NavigationState } | undefined;
|
|
58
|
+
if (!route?.name) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
routeName = route.name;
|
|
62
|
+
state = route.state;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return routeName;
|
|
66
|
+
};
|
|
33
67
|
|
|
34
|
-
const
|
|
35
|
-
|
|
68
|
+
const getNetworkTrackingConfig = (config: ReactNativeConfigAutocaptureBeta): NetworkTrackingOptions | undefined => {
|
|
69
|
+
let networkTrackingConfig;
|
|
70
|
+
if (typeof config.autocapture === 'object' && typeof config.autocapture.networkTracking === 'object') {
|
|
71
|
+
networkTrackingConfig = config.autocapture.networkTracking;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
...networkTrackingConfig,
|
|
75
|
+
captureRules: normalizeNetworkCaptureRules(networkTrackingConfig?.captureRules, config.loggerProvider),
|
|
76
|
+
};
|
|
77
|
+
};
|
|
36
78
|
|
|
37
79
|
export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeClient, AnalyticsClient {
|
|
38
80
|
appState: AppStateStatus = 'background';
|
|
81
|
+
/**
|
|
82
|
+
* True after a transition into `background` until the next `active`.
|
|
83
|
+
* Used so Application Opened only pairs with Application Backgrounded
|
|
84
|
+
* (e.g. skip iOS inactive→active from Control Center overlays).
|
|
85
|
+
*/
|
|
86
|
+
private wasBackgrounded = false;
|
|
87
|
+
/**
|
|
88
|
+
* Last focused leaf route name emitted by `trackScreenViewOnNavigationStateChange`.
|
|
89
|
+
* Dedupes React Navigation callbacks that only change params, history shape, or
|
|
90
|
+
* fire twice for the same visible screen (e.g. `onReady` + `onStateChange`, tabs).
|
|
91
|
+
*/
|
|
92
|
+
private lastNavigationScreenName: string | undefined;
|
|
39
93
|
private appStateChangeHandler: NativeEventSubscription | undefined;
|
|
40
94
|
explicitSessionId: number | undefined;
|
|
41
95
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
42
96
|
// @ts-ignore
|
|
43
97
|
config: ReactNativeConfig;
|
|
44
98
|
userProperties: { [key: string]: any } | undefined;
|
|
99
|
+
autocapture: ReactNativeAutocaptureOptions | null = null;
|
|
45
100
|
|
|
46
101
|
init(apiKey = '', userId?: string, options?: ReactNativeOptions) {
|
|
47
102
|
return returnWrapper(this._init({ ...options, userId, apiKey }));
|
|
@@ -68,6 +123,23 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
68
123
|
});
|
|
69
124
|
await super._init(reactNativeOptions);
|
|
70
125
|
|
|
126
|
+
// Step 2.1: parse autocapture config (always reset so re-init clears prior flags)
|
|
127
|
+
const autocaptureConfig = (this.config as ReactNativeConfigAutocaptureBeta).autocapture;
|
|
128
|
+
if (autocaptureConfig === true) {
|
|
129
|
+
this.autocapture = {
|
|
130
|
+
appLifecycles: true,
|
|
131
|
+
sessions: true,
|
|
132
|
+
};
|
|
133
|
+
} else if (autocaptureConfig && typeof autocaptureConfig === 'object') {
|
|
134
|
+
this.autocapture = autocaptureConfig;
|
|
135
|
+
} else {
|
|
136
|
+
this.autocapture = null;
|
|
137
|
+
}
|
|
138
|
+
// Drop any background seen before this init so Opened cannot fire unpaired.
|
|
139
|
+
this.wasBackgrounded = false;
|
|
140
|
+
// Allow the first navigation screen view after re-init.
|
|
141
|
+
this.lastNavigationScreenName = undefined;
|
|
142
|
+
|
|
71
143
|
// Set up the analytics connector to integrate with the experiment SDK.
|
|
72
144
|
// Send events from the experiment SDK and forward identifies to the
|
|
73
145
|
// identity store.
|
|
@@ -88,6 +160,11 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
88
160
|
await this.add(new Context()).promise;
|
|
89
161
|
await this.add(new IdentityEventSender()).promise;
|
|
90
162
|
|
|
163
|
+
if (this.autocapture?.networkTracking) {
|
|
164
|
+
this.config.loggerProvider.debug('Adding network tracking plugin');
|
|
165
|
+
await this.add(networkCapturePlugin(getNetworkTrackingConfig(this.config))).promise;
|
|
166
|
+
}
|
|
167
|
+
|
|
91
168
|
// Step 4: Manage session
|
|
92
169
|
this.appState = AppState.currentState;
|
|
93
170
|
const isNewSession = this.startNewSessionIfNeeded(this.currentTimeMillis());
|
|
@@ -100,7 +177,15 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
100
177
|
|
|
101
178
|
this.initializing = false;
|
|
102
179
|
|
|
103
|
-
// Step 5:
|
|
180
|
+
// Step 5: autocapture
|
|
181
|
+
|
|
182
|
+
// Step 5.1: track Application Opened when already foregrounded at init (cold start).
|
|
183
|
+
// Do not await — track().promise waits on flush/network and would block init.
|
|
184
|
+
if (this.autocapture?.appLifecycles === true && this.appState === 'active') {
|
|
185
|
+
this.track('[Amplitude] Application Opened');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Step 5.2: run attribution strategy
|
|
104
189
|
await this.runAttributionStrategy(options.attribution, isNewSession);
|
|
105
190
|
|
|
106
191
|
// Step 6: Run queued functions
|
|
@@ -173,6 +258,8 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
173
258
|
reset() {
|
|
174
259
|
this.setUserId(undefined);
|
|
175
260
|
this.setDeviceId(UUID());
|
|
261
|
+
// Allow the current focused route to emit again for the new identity.
|
|
262
|
+
this.lastNavigationScreenName = undefined;
|
|
176
263
|
}
|
|
177
264
|
|
|
178
265
|
getSessionId() {
|
|
@@ -209,6 +296,37 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
209
296
|
this.config.lastEventTime = this.currentTimeMillis();
|
|
210
297
|
}
|
|
211
298
|
|
|
299
|
+
trackScreenView(screenName: string, eventProperties?: Record<string, any>, eventOptions?: EventOptions) {
|
|
300
|
+
const isScreenViewTrackingDisabled = this.autocapture?.screenViews === false;
|
|
301
|
+
if (isScreenViewTrackingDisabled) {
|
|
302
|
+
return returnWrapper(Promise.resolve(undefined));
|
|
303
|
+
}
|
|
304
|
+
return this.track(
|
|
305
|
+
DEFAULT_SCREEN_VIEWED_EVENT,
|
|
306
|
+
{
|
|
307
|
+
[SCREEN_NAME]: screenName,
|
|
308
|
+
...eventProperties,
|
|
309
|
+
},
|
|
310
|
+
eventOptions,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
trackScreenViewOnNavigationStateChange(
|
|
315
|
+
navigationState: NavigationState | undefined,
|
|
316
|
+
eventProperties?: Record<string, any>,
|
|
317
|
+
eventOptions?: EventOptions,
|
|
318
|
+
) {
|
|
319
|
+
if (!navigationState) {
|
|
320
|
+
return returnWrapper(Promise.resolve(undefined));
|
|
321
|
+
}
|
|
322
|
+
const screenName = getActiveRouteName(navigationState);
|
|
323
|
+
if (!screenName || screenName === this.lastNavigationScreenName) {
|
|
324
|
+
return returnWrapper(Promise.resolve(undefined));
|
|
325
|
+
}
|
|
326
|
+
this.lastNavigationScreenName = screenName;
|
|
327
|
+
return this.trackScreenView(screenName, eventProperties, eventOptions);
|
|
328
|
+
}
|
|
329
|
+
|
|
212
330
|
private setSessionIdInternal(sessionId: number, eventTime: number) {
|
|
213
331
|
const previousSessionId = this.config.sessionId;
|
|
214
332
|
if (previousSessionId === sessionId) {
|
|
@@ -217,12 +335,12 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
217
335
|
|
|
218
336
|
this.config.sessionId = sessionId;
|
|
219
337
|
|
|
220
|
-
if (this.config.trackingSessionEvents) {
|
|
338
|
+
if (this.config.trackingSessionEvents || this.autocapture?.sessions) {
|
|
221
339
|
this.config.loggerProvider?.log(`SESSION_END event: previousSessionId = ${previousSessionId ?? 'undefined'}`);
|
|
222
340
|
|
|
223
341
|
if (previousSessionId !== undefined) {
|
|
224
342
|
const sessionEndEvent: Event = {
|
|
225
|
-
event_type:
|
|
343
|
+
event_type: DEFAULT_SESSION_END_EVENT,
|
|
226
344
|
time: this.config.lastEventTime !== undefined ? this.config.lastEventTime + 1 : sessionId, // increment lastEventTime to sort events properly in UI - session_end should be the last event in a session
|
|
227
345
|
session_id: previousSessionId,
|
|
228
346
|
};
|
|
@@ -231,7 +349,7 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
231
349
|
|
|
232
350
|
this.config.loggerProvider?.log(`SESSION_START event: sessionId = ${sessionId}`);
|
|
233
351
|
const sessionStartEvent: Event = {
|
|
234
|
-
event_type:
|
|
352
|
+
event_type: DEFAULT_SESSION_START_EVENT,
|
|
235
353
|
time: eventTime,
|
|
236
354
|
session_id: sessionId,
|
|
237
355
|
};
|
|
@@ -248,7 +366,8 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
248
366
|
event = { ...event, time: eventTime };
|
|
249
367
|
}
|
|
250
368
|
|
|
251
|
-
const isSessionEvent =
|
|
369
|
+
const isSessionEvent =
|
|
370
|
+
event.event_type === DEFAULT_SESSION_START_EVENT || event.event_type === DEFAULT_SESSION_END_EVENT;
|
|
252
371
|
const isCustomEventSessionId =
|
|
253
372
|
!isSessionEvent && event.session_id != undefined && event.session_id !== this.getSessionId();
|
|
254
373
|
if (!isCustomEventSessionId) {
|
|
@@ -324,11 +443,21 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
324
443
|
} else {
|
|
325
444
|
this.exitForeground(timestamp);
|
|
326
445
|
}
|
|
446
|
+
if (nextAppState == 'background' && this.autocapture?.appLifecycles === true) {
|
|
447
|
+
// Only remember background when we also emit Backgrounded, so Opened stays paired.
|
|
448
|
+
this.wasBackgrounded = true;
|
|
449
|
+
this.track(DEFAULT_APPLICATION_BACKGROUNDED_EVENT);
|
|
450
|
+
}
|
|
327
451
|
}
|
|
328
452
|
};
|
|
329
453
|
|
|
330
454
|
private enterForeground(timestamp: number) {
|
|
331
455
|
this.config.loggerProvider?.log('App Activated');
|
|
456
|
+
// Only emit Application Opened after a real background (not inactive→active).
|
|
457
|
+
if (this.autocapture?.appLifecycles === true && this.wasBackgrounded) {
|
|
458
|
+
this.track(DEFAULT_APPLICATION_OPENED_EVENT);
|
|
459
|
+
}
|
|
460
|
+
this.wasBackgrounded = false;
|
|
332
461
|
return this.startNewSessionIfNeeded(timestamp);
|
|
333
462
|
}
|
|
334
463
|
|
|
@@ -340,6 +469,19 @@ export class AmplitudeReactNative extends AmplitudeCore implements ReactNativeCl
|
|
|
340
469
|
export const createInstance = (): ReactNativeClient => {
|
|
341
470
|
const client = new AmplitudeReactNative();
|
|
342
471
|
return {
|
|
472
|
+
...client,
|
|
473
|
+
trackScreenView: debugWrapper(
|
|
474
|
+
client.trackScreenView.bind(client),
|
|
475
|
+
'trackScreenView',
|
|
476
|
+
getClientLogConfig(client),
|
|
477
|
+
getClientStates(client, ['config.apiKey', 'timeline.queue.length']),
|
|
478
|
+
),
|
|
479
|
+
trackScreenViewOnNavigationStateChange: debugWrapper(
|
|
480
|
+
client.trackScreenViewOnNavigationStateChange.bind(client),
|
|
481
|
+
'trackScreenViewOnNavigationStateChange',
|
|
482
|
+
getClientLogConfig(client),
|
|
483
|
+
getClientStates(client, ['config.apiKey', 'timeline.queue.length']),
|
|
484
|
+
),
|
|
343
485
|
init: debugWrapper(
|
|
344
486
|
client.init.bind(client),
|
|
345
487
|
'init',
|
package/src/types.ts
CHANGED
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '1.6.
|
|
1
|
+
export const VERSION = '1.6.7';
|