@amplitude/analytics-react-native 0.2.3 → 0.3.1
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 +12 -15
- package/lib/commonjs/config.js.map +1 -1
- package/lib/commonjs/index.js +45 -106
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/react-native-client.js +40 -228
- package/lib/commonjs/react-native-client.js.map +1 -1
- package/lib/commonjs/storage/cookie.js +1 -1
- package/lib/commonjs/storage/cookie.js.map +1 -1
- package/lib/commonjs/utils/query-params.js +18 -6
- package/lib/commonjs/utils/query-params.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/config.js +12 -15
- package/lib/module/config.js.map +1 -1
- package/lib/module/index.js +23 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/react-native-client.js +34 -210
- package/lib/module/react-native-client.js.map +1 -1
- package/lib/module/storage/cookie.js +1 -1
- package/lib/module/storage/cookie.js.map +1 -1
- package/lib/module/utils/query-params.js +14 -4
- package/lib/module/utils/query-params.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/attribution/campaign-tracker.d.ts +3 -0
- package/lib/typescript/attribution/campaign-tracker.d.ts.map +1 -1
- package/lib/typescript/config.d.ts +4 -32
- package/lib/typescript/config.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +2 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/react-native-client.d.ts +4 -190
- package/lib/typescript/react-native-client.d.ts.map +1 -1
- package/lib/typescript/utils/query-params.d.ts +1 -0
- package/lib/typescript/utils/query-params.d.ts.map +1 -1
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +4 -4
- package/src/config.ts +14 -17
- package/src/index.ts +5 -2
- package/src/react-native-client.ts +35 -208
- package/src/storage/cookie.ts +1 -1
- package/src/utils/query-params.ts +13 -5
- package/src/version.ts +1 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { AmplitudeCore, Destination,
|
|
1
|
+
import { AmplitudeCore, Destination, UUID, returnWrapper } from '@amplitude/analytics-core';
|
|
2
2
|
import {
|
|
3
3
|
ReactNativeConfig,
|
|
4
4
|
Campaign,
|
|
5
5
|
ReactNativeOptions,
|
|
6
6
|
AdditionalReactNativeOptions,
|
|
7
7
|
AttributionReactNativeOptions,
|
|
8
|
+
ReactNativeClient,
|
|
8
9
|
} from '@amplitude/analytics-types';
|
|
9
10
|
import { Context } from './plugins/context';
|
|
10
11
|
import { useReactNativeConfig, createFlexibleStorage } from './config';
|
|
@@ -16,6 +17,12 @@ import { getAnalyticsConnector } from './utils/analytics-connector';
|
|
|
16
17
|
|
|
17
18
|
export class AmplitudeReactNative extends AmplitudeCore<ReactNativeConfig> {
|
|
18
19
|
async init(apiKey: string, userId?: string, options?: ReactNativeOptions & AdditionalReactNativeOptions) {
|
|
20
|
+
// Step 0: Block concurrent initialization
|
|
21
|
+
if (this.initializing) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
this.initializing = true;
|
|
25
|
+
|
|
19
26
|
// Step 1: Read cookies stored by old SDK
|
|
20
27
|
const oldCookies = await parseOldCookies(apiKey, options);
|
|
21
28
|
|
|
@@ -60,6 +67,8 @@ export class AmplitudeReactNative extends AmplitudeCore<ReactNativeConfig> {
|
|
|
60
67
|
await this.add(new IdentityEventSender());
|
|
61
68
|
await this.add(new Destination());
|
|
62
69
|
|
|
70
|
+
this.initializing = false;
|
|
71
|
+
|
|
63
72
|
// Step 5: Set timeline ready for processing events
|
|
64
73
|
// Send existing events, which might be collected by track before init
|
|
65
74
|
this.timeline.isReady = true;
|
|
@@ -143,210 +152,28 @@ export class AmplitudeReactNative extends AmplitudeCore<ReactNativeConfig> {
|
|
|
143
152
|
}
|
|
144
153
|
}
|
|
145
154
|
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
* ```typescript
|
|
172
|
-
* amplitude.remove('myPlugin');
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
|
-
export const remove = returnWrapper(client.remove.bind(client));
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Tracks user-defined event, with specified type, optional event properties and optional overwrites.
|
|
179
|
-
*
|
|
180
|
-
* ```typescript
|
|
181
|
-
* // event tracking with event type only
|
|
182
|
-
* track('Page Load');
|
|
183
|
-
*
|
|
184
|
-
* // event tracking with event type and additional event properties
|
|
185
|
-
* track('Page Load', { loadTime: 1000 });
|
|
186
|
-
*
|
|
187
|
-
* // event tracking with event type, additional event properties, and overwritten event options
|
|
188
|
-
* track('Page Load', { loadTime: 1000 }, { sessionId: -1 });
|
|
189
|
-
*
|
|
190
|
-
* // alternatively, this tracking method is awaitable
|
|
191
|
-
* const result = await track('Page Load').promise;
|
|
192
|
-
* console.log(result.event); // {...}
|
|
193
|
-
* console.log(result.code); // 200
|
|
194
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
export const track = returnWrapper(client.track.bind(client));
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Alias for track()
|
|
201
|
-
*/
|
|
202
|
-
export const logEvent = returnWrapper(client.logEvent.bind(client));
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Sends an identify event containing user property operations
|
|
206
|
-
*
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const id = new Identify();
|
|
209
|
-
* id.set('colors', ['rose', 'gold']);
|
|
210
|
-
* identify(id);
|
|
211
|
-
*
|
|
212
|
-
* // alternatively, this tracking method is awaitable
|
|
213
|
-
* const result = await identify(id).promise;
|
|
214
|
-
* console.log(result.event); // {...}
|
|
215
|
-
* console.log(result.code); // 200
|
|
216
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
export const identify = returnWrapper(client.identify.bind(client));
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Sends a group identify event containing group property operations.
|
|
223
|
-
*
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const id = new Identify();
|
|
226
|
-
* id.set('skills', ['js', 'ts']);
|
|
227
|
-
* const groupType = 'org';
|
|
228
|
-
* const groupName = 'engineering';
|
|
229
|
-
* groupIdentify(groupType, groupName, id);
|
|
230
|
-
*
|
|
231
|
-
* // alternatively, this tracking method is awaitable
|
|
232
|
-
* const result = await groupIdentify(groupType, groupName, id).promise;
|
|
233
|
-
* console.log(result.event); // {...}
|
|
234
|
-
* console.log(result.code); // 200
|
|
235
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
236
|
-
* ```
|
|
237
|
-
*/
|
|
238
|
-
export const groupIdentify = returnWrapper(client.groupIdentify.bind(client));
|
|
239
|
-
export const setGroup = returnWrapper(client.setGroup.bind(client));
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Sends a revenue event containing revenue property operations.
|
|
243
|
-
*
|
|
244
|
-
* ```typescript
|
|
245
|
-
* const rev = new Revenue();
|
|
246
|
-
* rev.setRevenue(100);
|
|
247
|
-
* revenue(rev);
|
|
248
|
-
*
|
|
249
|
-
* // alternatively, this tracking method is awaitable
|
|
250
|
-
* const result = await revenue(rev).promise;
|
|
251
|
-
* console.log(result.event); // {...}
|
|
252
|
-
* console.log(result.code); // 200
|
|
253
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
256
|
-
export const revenue = returnWrapper(client.revenue.bind(client));
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Returns current user ID.
|
|
260
|
-
*
|
|
261
|
-
* ```typescript
|
|
262
|
-
* const userId = getUserId();
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
export const getUserId = client.getUserId.bind(client);
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Sets a new user ID.
|
|
269
|
-
*
|
|
270
|
-
* ```typescript
|
|
271
|
-
* setUserId('userId');
|
|
272
|
-
* ```
|
|
273
|
-
*/
|
|
274
|
-
export const setUserId = client.setUserId.bind(client);
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Returns current device ID.
|
|
278
|
-
*
|
|
279
|
-
* ```typescript
|
|
280
|
-
* const deviceId = getDeviceId();
|
|
281
|
-
* ```
|
|
282
|
-
*/
|
|
283
|
-
export const getDeviceId = client.getDeviceId.bind(client);
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Sets a new device ID.
|
|
287
|
-
* When setting a custom device ID, make sure the value is sufficiently unique.
|
|
288
|
-
* A uuid is recommended.
|
|
289
|
-
*
|
|
290
|
-
* ```typescript
|
|
291
|
-
* setDeviceId('deviceId');
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
|
-
export const setDeviceId = client.setDeviceId.bind(client);
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* reset is a shortcut to anonymize users after they log out, by:
|
|
298
|
-
* - setting userId to `undefined`
|
|
299
|
-
* - regenerating a new random deviceId
|
|
300
|
-
*
|
|
301
|
-
* With an `undefined` userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
|
|
302
|
-
*
|
|
303
|
-
* ```typescript
|
|
304
|
-
* reset();
|
|
305
|
-
* ```
|
|
306
|
-
*/
|
|
307
|
-
export const reset = client.reset.bind(client);
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Returns current session ID.
|
|
311
|
-
*
|
|
312
|
-
* ```typescript
|
|
313
|
-
* const sessionId = getSessionId();
|
|
314
|
-
* ```
|
|
315
|
-
*/
|
|
316
|
-
export const getSessionId = client.getSessionId.bind(client);
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Sets a new session ID.
|
|
320
|
-
* When settign a custom session ID, make sure the value is in milliseconds since epoch (Unix Timestamp).
|
|
321
|
-
*
|
|
322
|
-
* ```typescript
|
|
323
|
-
* setSessionId(Date.now());
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
|
-
export const setSessionId = client.setSessionId.bind(client);
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Sets a new optOut config value. This toggles event tracking on/off.
|
|
330
|
-
*
|
|
331
|
-
*```typescript
|
|
332
|
-
* // Stops tracking
|
|
333
|
-
* setOptOut(true);
|
|
334
|
-
*
|
|
335
|
-
* // Starts/resumes tracking
|
|
336
|
-
* setOptOut(false);
|
|
337
|
-
* ```
|
|
338
|
-
*/
|
|
339
|
-
export const setOptOut = client.setOptOut.bind(client);
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Flush and send all the events which haven't been sent.
|
|
343
|
-
*
|
|
344
|
-
*```typescript
|
|
345
|
-
* // Send all the unsent events
|
|
346
|
-
* flush();
|
|
347
|
-
*
|
|
348
|
-
* // alternatively, this tracking method is awaitable
|
|
349
|
-
* await flush().promise;
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
|
-
export const flush = returnWrapper(client.flush.bind(client));
|
|
155
|
+
export const createInstance = (): ReactNativeClient => {
|
|
156
|
+
const client = new AmplitudeReactNative();
|
|
157
|
+
return {
|
|
158
|
+
init: returnWrapper(client.init.bind(client)),
|
|
159
|
+
add: returnWrapper(client.add.bind(client)),
|
|
160
|
+
remove: returnWrapper(client.remove.bind(client)),
|
|
161
|
+
track: returnWrapper(client.track.bind(client)),
|
|
162
|
+
logEvent: returnWrapper(client.logEvent.bind(client)),
|
|
163
|
+
identify: returnWrapper(client.identify.bind(client)),
|
|
164
|
+
groupIdentify: returnWrapper(client.groupIdentify.bind(client)),
|
|
165
|
+
setGroup: returnWrapper(client.setGroup.bind(client)),
|
|
166
|
+
revenue: returnWrapper(client.revenue.bind(client)),
|
|
167
|
+
flush: returnWrapper(client.flush.bind(client)),
|
|
168
|
+
getUserId: client.getUserId.bind(client),
|
|
169
|
+
setUserId: client.setUserId.bind(client),
|
|
170
|
+
getDeviceId: client.getDeviceId.bind(client),
|
|
171
|
+
setDeviceId: client.setDeviceId.bind(client),
|
|
172
|
+
reset: client.reset.bind(client),
|
|
173
|
+
getSessionId: client.getSessionId.bind(client),
|
|
174
|
+
setSessionId: client.setSessionId.bind(client),
|
|
175
|
+
setOptOut: client.setOptOut.bind(client),
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export default createInstance();
|
package/src/storage/cookie.ts
CHANGED
|
@@ -15,7 +15,7 @@ export class CookieStorage<T> implements Storage<T> {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
const random = String(Date.now());
|
|
18
|
-
const testStrorage = new CookieStorage<string>();
|
|
18
|
+
const testStrorage = new CookieStorage<string>(this.options);
|
|
19
19
|
const testKey = 'AMP_TEST';
|
|
20
20
|
try {
|
|
21
21
|
await testStrorage.set(testKey, random);
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { isNative } from './platform';
|
|
2
|
-
|
|
3
1
|
export const getQueryParams = (): Record<string, string | undefined> => {
|
|
4
2
|
/* istanbul ignore if */
|
|
5
|
-
if (
|
|
3
|
+
if (typeof window === 'undefined') {
|
|
6
4
|
return {};
|
|
7
5
|
}
|
|
8
6
|
const pairs = window.location.search.substring(1).split('&').filter(Boolean);
|
|
9
7
|
const params = pairs.reduce<Record<string, string | undefined>>((acc, curr) => {
|
|
10
|
-
const
|
|
8
|
+
const query = curr.split('=', 2);
|
|
9
|
+
const key = tryDecodeURIComponent(query[0]);
|
|
10
|
+
const value = tryDecodeURIComponent(query[1]);
|
|
11
11
|
if (!value) {
|
|
12
12
|
return acc;
|
|
13
13
|
}
|
|
14
|
-
acc[
|
|
14
|
+
acc[key] = value;
|
|
15
15
|
return acc;
|
|
16
16
|
}, {});
|
|
17
17
|
return params;
|
|
18
18
|
};
|
|
19
|
+
|
|
20
|
+
export const tryDecodeURIComponent = (value = '') => {
|
|
21
|
+
try {
|
|
22
|
+
return decodeURIComponent(value);
|
|
23
|
+
} catch {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
};
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.3.1';
|