@amplitude/analytics-react-native 1.3.3 → 1.3.5

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 (29) hide show
  1. package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativeModule.kt +101 -0
  2. package/android/src/main/java/com/amplitude/reactnative/LegacyDatabaseStorage.kt +313 -0
  3. package/ios/AmplitudeReactNative.m +3 -0
  4. package/ios/AmplitudeReactNative.swift +74 -0
  5. package/ios/AmplitudeReactNative.xcodeproj/project.pbxproj +9 -17
  6. package/ios/LegacyDatabaseStorage.swift +224 -0
  7. package/lib/commonjs/config.js +19 -7
  8. package/lib/commonjs/config.js.map +1 -1
  9. package/lib/commonjs/migration/remnant-data-migration.js +171 -0
  10. package/lib/commonjs/migration/remnant-data-migration.js.map +1 -0
  11. package/lib/commonjs/react-native-client.js +2 -2
  12. package/lib/commonjs/react-native-client.js.map +1 -1
  13. package/lib/commonjs/version.js +1 -1
  14. package/lib/module/config.js +18 -7
  15. package/lib/module/config.js.map +1 -1
  16. package/lib/module/migration/remnant-data-migration.js +164 -0
  17. package/lib/module/migration/remnant-data-migration.js.map +1 -0
  18. package/lib/module/react-native-client.js +2 -2
  19. package/lib/module/react-native-client.js.map +1 -1
  20. package/lib/module/version.js +1 -1
  21. package/lib/typescript/config.d.ts.map +1 -1
  22. package/lib/typescript/migration/remnant-data-migration.d.ts +20 -0
  23. package/lib/typescript/migration/remnant-data-migration.d.ts.map +1 -0
  24. package/lib/typescript/version.d.ts +1 -1
  25. package/package.json +5 -5
  26. package/src/config.ts +26 -7
  27. package/src/migration/remnant-data-migration.ts +169 -0
  28. package/src/react-native-client.ts +2 -2
  29. package/src/version.ts +1 -1
@@ -0,0 +1,169 @@
1
+ import { NativeModules } from 'react-native';
2
+ import { Event, Logger, Storage, UserSession } from '@amplitude/analytics-types';
3
+ import { STORAGE_PREFIX } from '@amplitude/analytics-core';
4
+
5
+ type LegacyEventKind = 'event' | 'identify' | 'interceptedIdentify';
6
+
7
+ interface AmplitudeReactNative {
8
+ getLegacySessionData(instanceName: string | undefined): Promise<Omit<UserSession, 'optOut'>>;
9
+ getLegacyEvents(instanceName: string | undefined, eventKind: LegacyEventKind): Promise<string[]>;
10
+ removeLegacyEvent(instanceName: string | undefined, eventKind: LegacyEventKind, eventId: number): void;
11
+ }
12
+
13
+ export default class RemnantDataMigration {
14
+ eventsStorageKey: string;
15
+ private readonly nativeModule: AmplitudeReactNative;
16
+
17
+ constructor(
18
+ private apiKey: string,
19
+ private instanceName: string | undefined,
20
+ private storage: Storage<Event[]> | undefined,
21
+ private firstRunSinceUpgrade: boolean,
22
+ private logger: Logger | undefined,
23
+ ) {
24
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
25
+ this.eventsStorageKey = `${STORAGE_PREFIX}_${this.apiKey.substring(0, 10)}`;
26
+ this.nativeModule = NativeModules.AmplitudeReactNative as AmplitudeReactNative;
27
+ }
28
+
29
+ async execute(): Promise<Omit<UserSession, 'optOut'>> {
30
+ if (!this.nativeModule) {
31
+ return {};
32
+ }
33
+
34
+ if (this.firstRunSinceUpgrade) {
35
+ await this.moveIdentifies();
36
+ await this.moveInterceptedIdentifies();
37
+ }
38
+ await this.moveEvents();
39
+
40
+ const sessionData = await this.callNativeFunction(() => this.nativeModule.getLegacySessionData(this.instanceName));
41
+ return sessionData ?? {};
42
+ }
43
+
44
+ private async moveEvents() {
45
+ await this.moveLegacyEvents('event');
46
+ }
47
+
48
+ private async moveIdentifies() {
49
+ await this.moveLegacyEvents('identify');
50
+ }
51
+
52
+ private async moveInterceptedIdentifies() {
53
+ await this.moveLegacyEvents('interceptedIdentify');
54
+ }
55
+
56
+ private async callNativeFunction<T>(action: () => Promise<T>): Promise<T | undefined> {
57
+ try {
58
+ return await action();
59
+ } catch (e) {
60
+ this.logger?.error(`can't call native function: ${String(e)}`);
61
+ return undefined;
62
+ }
63
+ }
64
+
65
+ private callNativeAction(action: () => void) {
66
+ try {
67
+ action();
68
+ } catch (e) {
69
+ this.logger?.error(`can't call native function: ${String(e)}`);
70
+ }
71
+ }
72
+
73
+ private async moveLegacyEvents(eventKind: LegacyEventKind) {
74
+ const legacyJsonEvents = await this.callNativeFunction(() =>
75
+ this.nativeModule.getLegacyEvents(this.instanceName, eventKind),
76
+ );
77
+ if (!this.storage || !legacyJsonEvents || legacyJsonEvents.length === 0) {
78
+ return;
79
+ }
80
+
81
+ const events = (await this.storage.get(this.eventsStorageKey)) ?? [];
82
+ const eventIds: number[] = [];
83
+
84
+ legacyJsonEvents.forEach((jsonEvent) => {
85
+ const event = this.convertLegacyEvent(jsonEvent);
86
+ if (event) {
87
+ events.push(event);
88
+ if (event.event_id !== undefined) {
89
+ eventIds.push(event.event_id);
90
+ }
91
+ }
92
+ });
93
+
94
+ await this.storage.set(this.eventsStorageKey, events);
95
+ eventIds.forEach((eventId) =>
96
+ this.callNativeAction(() => this.nativeModule.removeLegacyEvent(this.instanceName, eventKind, eventId)),
97
+ );
98
+ }
99
+
100
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
101
+ private convertLegacyEvent(legacyJsonEvent: string): Event | null {
102
+ try {
103
+ const event = JSON.parse(legacyJsonEvent) as Event;
104
+
105
+ const { library, timestamp, uuid, api_properties } = event as any;
106
+ if (library !== undefined) {
107
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
108
+ event.library = `${library.name}/${library.version}`;
109
+ }
110
+ if (timestamp !== undefined) {
111
+ event.time = timestamp;
112
+ }
113
+ if (uuid !== undefined) {
114
+ event.insert_id = uuid;
115
+ }
116
+
117
+ if (api_properties) {
118
+ const { androidADID, android_app_set_id, ios_idfa, ios_idfv, productId, quantity, price, location } =
119
+ api_properties;
120
+ if (androidADID !== undefined) {
121
+ event.adid = androidADID;
122
+ }
123
+ if (android_app_set_id !== undefined) {
124
+ event.android_app_set_id = android_app_set_id;
125
+ }
126
+ if (ios_idfa !== undefined) {
127
+ event.idfa = ios_idfa;
128
+ }
129
+ if (ios_idfv !== undefined) {
130
+ event.idfv = ios_idfv;
131
+ }
132
+ if (productId !== undefined) {
133
+ event.productId = productId;
134
+ }
135
+ if (quantity !== undefined) {
136
+ event.quantity = quantity;
137
+ }
138
+ if (price !== undefined) {
139
+ event.price = price;
140
+ }
141
+ if (location !== undefined) {
142
+ const { lat, lng } = location;
143
+ event.location_lat = lat;
144
+ event.location_lng = lng;
145
+ }
146
+ }
147
+
148
+ const { $productId: productId, $quantity: quantity, $price: price, $revenueType: revenueType } = event as any;
149
+ if (productId !== undefined) {
150
+ event.productId = productId;
151
+ }
152
+ if (quantity !== undefined) {
153
+ event.quantity = quantity;
154
+ }
155
+ if (price !== undefined) {
156
+ event.price = price;
157
+ }
158
+ if (revenueType !== undefined) {
159
+ event.revenueType = revenueType;
160
+ }
161
+
162
+ return event;
163
+ } catch {
164
+ // skip invalid events
165
+ return null;
166
+ }
167
+ }
168
+ // eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
169
+ }
@@ -59,11 +59,11 @@ export class AmplitudeReactNative extends AmplitudeCore {
59
59
  // Step 2: Create react native config
60
60
  const reactNativeOptions = await useReactNativeConfig(options.apiKey, {
61
61
  ...options,
62
- deviceId: oldCookies.deviceId ?? options.deviceId,
62
+ deviceId: options.deviceId ?? oldCookies.deviceId,
63
63
  sessionId: oldCookies.sessionId,
64
64
  optOut: options.optOut ?? oldCookies.optOut,
65
65
  lastEventTime: oldCookies.lastEventTime,
66
- userId: options.userId || oldCookies.userId,
66
+ userId: options.userId ?? oldCookies.userId,
67
67
  });
68
68
  await super._init(reactNativeOptions);
69
69
 
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.3.3';
1
+ export const VERSION = '1.3.5';