@amplitude/analytics-react-native 0.2.4 → 0.3.0

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 (35) hide show
  1. package/lib/commonjs/config.js +12 -16
  2. package/lib/commonjs/config.js.map +1 -1
  3. package/lib/commonjs/index.js +45 -106
  4. package/lib/commonjs/index.js.map +1 -1
  5. package/lib/commonjs/react-native-client.js +40 -228
  6. package/lib/commonjs/react-native-client.js.map +1 -1
  7. package/lib/commonjs/utils/query-params.js +18 -6
  8. package/lib/commonjs/utils/query-params.js.map +1 -1
  9. package/lib/commonjs/version.js +1 -1
  10. package/lib/commonjs/version.js.map +1 -1
  11. package/lib/module/config.js +12 -16
  12. package/lib/module/config.js.map +1 -1
  13. package/lib/module/index.js +23 -1
  14. package/lib/module/index.js.map +1 -1
  15. package/lib/module/react-native-client.js +34 -210
  16. package/lib/module/react-native-client.js.map +1 -1
  17. package/lib/module/utils/query-params.js +14 -4
  18. package/lib/module/utils/query-params.js.map +1 -1
  19. package/lib/module/version.js +1 -1
  20. package/lib/module/version.js.map +1 -1
  21. package/lib/typescript/config.d.ts +4 -34
  22. package/lib/typescript/config.d.ts.map +1 -1
  23. package/lib/typescript/index.d.ts +2 -1
  24. package/lib/typescript/index.d.ts.map +1 -1
  25. package/lib/typescript/react-native-client.d.ts +4 -190
  26. package/lib/typescript/react-native-client.d.ts.map +1 -1
  27. package/lib/typescript/utils/query-params.d.ts +1 -0
  28. package/lib/typescript/utils/query-params.d.ts.map +1 -1
  29. package/lib/typescript/version.d.ts +1 -1
  30. package/package.json +4 -4
  31. package/src/config.ts +14 -18
  32. package/src/index.ts +5 -2
  33. package/src/react-native-client.ts +35 -208
  34. package/src/utils/query-params.ts +13 -5
  35. package/src/version.ts +1 -1
@@ -1,10 +1,11 @@
1
- import { AmplitudeCore, Destination, returnWrapper, UUID } from '@amplitude/analytics-core';
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 client = new AmplitudeReactNative();
147
-
148
- /**
149
- * Initializes the Amplitude SDK with your apiKey, userId and optional configurations.
150
- * This method must be called before any other operations.
151
- *
152
- * ```typescript
153
- * await init(API_KEY, USER_ID, options).promise;
154
- * ```
155
- */
156
- export const init = returnWrapper(client.init.bind(client));
157
-
158
- /**
159
- * Adds a new plugin.
160
- *
161
- * ```typescript
162
- * const plugin = {...};
163
- * amplitude.add(plugin);
164
- * ```
165
- */
166
- export const add = returnWrapper(client.add.bind(client));
167
-
168
- /**
169
- * Removes a plugin.
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();
@@ -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 (isNative() || typeof window === 'undefined') {
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 [key, value = ''] = curr.split('=', 2);
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[decodeURIComponent(key)] = decodeURIComponent(value);
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.2.4';
1
+ export const VERSION = '0.3.0';