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