posthog-react-native 2.1.4 → 2.2.0-alpha1

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/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # 2.1.4 - 2022-10-28
2
+
3
+ Also include the fix in the compiled `lib` folder.
4
+
5
+ # 2.1.3 - 2022-10-27
6
+
7
+ Actually include the fix.
8
+
9
+ # 2.1.2 - 2022-10-27
10
+
11
+ Fix bug where all values set while stored data was being loaded would get overwritten once the data was done loading.
12
+
13
+ # 2.1.1 - 2022-09-09
14
+
15
+ Support for bootstrapping feature flags and distinctIDs. This allows you to initialise the library with a set of feature flags and distinctID that are immediately available.
16
+
17
+ # 2.1.0 - 2022-09-02
18
+
19
+ PosthogProvider `autocapture` can be configured with `captureLifecycleEvents: false` and `captureScreens: false` if you want do disable these autocapture elements. Both of these default to `true`
package/lib/index.cjs.js CHANGED
@@ -3,36 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var reactNative = require('react-native');
6
- var ExpoApplication = require('expo-application');
7
- var ExpoDevice = require('expo-device');
8
- var ExpoLocalization = require('expo-localization');
9
- var FileSystem = require('expo-file-system');
10
6
  var React = require('react');
11
7
 
12
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
9
 
14
- function _interopNamespace(e) {
15
- if (e && e.__esModule) return e;
16
- var n = Object.create(null);
17
- if (e) {
18
- Object.keys(e).forEach(function (k) {
19
- if (k !== 'default') {
20
- var d = Object.getOwnPropertyDescriptor(e, k);
21
- Object.defineProperty(n, k, d.get ? d : {
22
- enumerable: true,
23
- get: function () { return e[k]; }
24
- });
25
- }
26
- });
27
- }
28
- n["default"] = e;
29
- return Object.freeze(n);
30
- }
31
-
32
- var ExpoApplication__namespace = /*#__PURE__*/_interopNamespace(ExpoApplication);
33
- var ExpoDevice__namespace = /*#__PURE__*/_interopNamespace(ExpoDevice);
34
- var ExpoLocalization__namespace = /*#__PURE__*/_interopNamespace(ExpoLocalization);
35
- var FileSystem__namespace = /*#__PURE__*/_interopNamespace(FileSystem);
36
10
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
37
11
 
38
12
  /******************************************************************************
@@ -1277,6 +1251,22 @@ var PostHogMemoryStorage = /** @class */ (function () {
1277
1251
  return PostHogMemoryStorage;
1278
1252
  }());
1279
1253
 
1254
+ var _OptionalExpoApplication = undefined;
1255
+
1256
+ try {
1257
+ _OptionalExpoApplication = require('expo-application');
1258
+ } catch (e) {}
1259
+
1260
+ var OptionalExpoApplication = _OptionalExpoApplication;
1261
+
1262
+ var _OptionalExpoFileSystem = undefined;
1263
+
1264
+ try {
1265
+ _OptionalExpoFileSystem = require('expo-file-system');
1266
+ } catch (e) {}
1267
+
1268
+ var OptionalExpoFileSystem = _OptionalExpoFileSystem;
1269
+
1280
1270
  var getLegacyValues = function () {
1281
1271
  return __awaiter(void 0, void 0, void 0, function () {
1282
1272
  var posthogFileDirectory, posthogDistinctIdFile, posthogAnonymousIdFile, res, _a, _b, _c, _d, _e, _f;
@@ -1284,10 +1274,18 @@ var getLegacyValues = function () {
1284
1274
  return __generator(this, function (_g) {
1285
1275
  switch (_g.label) {
1286
1276
  case 0:
1277
+ // NOTE: The old react-native lib stored data in files on the filesystem.
1278
+ // This function takes care of pulling the legacy IDs to ensure we are using them if already present
1279
+ if (!OptionalExpoFileSystem || !OptionalExpoApplication) {
1280
+ return [2
1281
+ /*return*/
1282
+ ];
1283
+ }
1284
+
1287
1285
  if (!(reactNative.Platform.OS === 'ios')) return [3
1288
1286
  /*break*/
1289
1287
  , 8];
1290
- posthogFileDirectory = "".concat(FileSystem__namespace.documentDirectory, "../Library/Application%20Support/").concat(ExpoApplication__namespace.applicationId, "/");
1288
+ posthogFileDirectory = "".concat(OptionalExpoFileSystem.documentDirectory, "../Library/Application%20Support/").concat(OptionalExpoApplication.applicationId, "/");
1291
1289
  posthogDistinctIdFile = posthogFileDirectory + 'posthog.distinctId';
1292
1290
  posthogAnonymousIdFile = posthogFileDirectory + 'posthog.anonymousId';
1293
1291
  res = {
@@ -1303,7 +1301,7 @@ var getLegacyValues = function () {
1303
1301
  _c = (_b = JSON).parse;
1304
1302
  return [4
1305
1303
  /*yield*/
1306
- , FileSystem__namespace.readAsStringAsync(posthogDistinctIdFile)];
1304
+ , OptionalExpoFileSystem.readAsStringAsync(posthogDistinctIdFile)];
1307
1305
 
1308
1306
  case 2:
1309
1307
  _a.distinctId = _c.apply(_b, [_g.sent()])['posthog.distinctId'];
@@ -1324,7 +1322,7 @@ var getLegacyValues = function () {
1324
1322
  _f = (_e = JSON).parse;
1325
1323
  return [4
1326
1324
  /*yield*/
1327
- , FileSystem__namespace.readAsStringAsync(posthogAnonymousIdFile)];
1325
+ , OptionalExpoFileSystem.readAsStringAsync(posthogAnonymousIdFile)];
1328
1326
 
1329
1327
  case 5:
1330
1328
  _d.anonymousId = _f.apply(_e, [_g.sent()])['posthog.anonymousId'];
@@ -1353,115 +1351,222 @@ var getLegacyValues = function () {
1353
1351
  };
1354
1352
 
1355
1353
  var POSTHOG_STORAGE_KEY = '.posthog-rn.json';
1356
- var POSTHOG_STORAGE_VERSION = 'v1';
1354
+ var POSTHOG_STORAGE_VERSION = 'v1'; // NOTE: The core prefers a synchronous storage so we mimic this by pre-loading all keys
1357
1355
 
1358
- var loadStorageAsync = function () {
1359
- return __awaiter(void 0, void 0, void 0, function () {
1360
- var uri, stringContent;
1361
- return __generator(this, function (_a) {
1362
- switch (_a.label) {
1363
- case 0:
1364
- uri = (FileSystem__namespace.documentDirectory || '') + POSTHOG_STORAGE_KEY;
1365
- _a.label = 1;
1366
-
1367
- case 1:
1368
- _a.trys.push([1, 3,, 4]);
1356
+ var SemiAsyncStorage =
1357
+ /** @class */
1358
+ function () {
1359
+ function SemiAsyncStorage(asyncStorage) {
1360
+ this._memoryCache = {};
1361
+ this._asyncStorage = asyncStorage;
1362
+ }
1369
1363
 
1370
- return [4
1371
- /*yield*/
1372
- , FileSystem__namespace.readAsStringAsync(uri)];
1364
+ SemiAsyncStorage.prototype.preloadAsync = function () {
1365
+ var _this = this;
1373
1366
 
1374
- case 2:
1375
- stringContent = _a.sent();
1376
- return [2
1377
- /*return*/
1378
- , JSON.parse(stringContent).content];
1367
+ if (this._preloadSemiAsyncStoragePromise) {
1368
+ return this._preloadSemiAsyncStoragePromise;
1369
+ }
1379
1370
 
1380
- case 3:
1381
- _a.sent();
1382
- return [2
1383
- /*return*/
1384
- , {}];
1371
+ this._preloadSemiAsyncStoragePromise = this._asyncStorage.getItem(POSTHOG_STORAGE_KEY).then(function (res) {
1372
+ try {
1373
+ var data = res ? JSON.parse(res).content : {};
1385
1374
 
1386
- case 4:
1387
- return [2
1388
- /*return*/
1389
- ];
1375
+ for (var key in data) {
1376
+ _this._memoryCache[key] = _this._memoryCache[key] === undefined ? data[key] : _this._memoryCache[key];
1377
+ }
1378
+ } catch (e) {
1379
+ console.warn("PostHog failed to load persisted data from storage. This is likely because the storage format is. We'll reset the storage.", e);
1390
1380
  }
1391
1381
  });
1392
- });
1393
- };
1382
+ return this._preloadSemiAsyncStoragePromise;
1383
+ };
1394
1384
 
1395
- var persistStorageAsync = function (content) {
1396
- return __awaiter(void 0, void 0, void 0, function () {
1397
- var uri, data;
1398
- return __generator(this, function (_a) {
1399
- switch (_a.label) {
1400
- case 0:
1401
- uri = (FileSystem__namespace.documentDirectory || '') + POSTHOG_STORAGE_KEY;
1402
- data = {
1403
- version: POSTHOG_STORAGE_VERSION,
1404
- content: content
1405
- };
1406
- return [4
1407
- /*yield*/
1408
- , FileSystem__namespace.writeAsStringAsync(uri, JSON.stringify(data)).catch(function (e) {
1409
- console.error('PostHog failed to persist data to storage', e);
1410
- })];
1385
+ SemiAsyncStorage.prototype.persist = function () {
1386
+ var payload = {
1387
+ version: POSTHOG_STORAGE_VERSION,
1388
+ content: this._memoryCache
1389
+ };
1390
+ void this._asyncStorage.setItem(POSTHOG_STORAGE_KEY, JSON.stringify(payload));
1391
+ };
1411
1392
 
1412
- case 1:
1413
- _a.sent();
1393
+ SemiAsyncStorage.prototype.getItem = function (key) {
1394
+ return this._memoryCache[key];
1395
+ };
1414
1396
 
1415
- return [2
1416
- /*return*/
1417
- ];
1418
- }
1419
- });
1420
- });
1421
- }; // NOTE: The core prefers a synchronous storage so we mimic this by pre-loading all keys
1422
-
1423
-
1424
- var _memoryCache = {};
1425
- var SemiAsyncStorage = {
1426
- getItem: function (key) {
1427
- return _memoryCache[key];
1428
- },
1429
- setItem: function (key, value) {
1430
- _memoryCache[key] = value;
1431
- void persistStorageAsync(_memoryCache);
1432
- },
1433
- removeItem: function (key) {
1434
- delete _memoryCache[key];
1435
- void persistStorageAsync(_memoryCache);
1436
- },
1437
- clear: function () {
1438
- for (var key in _memoryCache) {
1439
- delete _memoryCache[key];
1397
+ SemiAsyncStorage.prototype.setItem = function (key, value) {
1398
+ this._memoryCache[key] = value;
1399
+ this.persist();
1400
+ };
1401
+
1402
+ SemiAsyncStorage.prototype.removeItem = function (key) {
1403
+ delete this._memoryCache[key];
1404
+ this.persist();
1405
+ };
1406
+
1407
+ SemiAsyncStorage.prototype.clear = function () {
1408
+ for (var key in this._memoryCache) {
1409
+ delete this._memoryCache[key];
1440
1410
  }
1441
1411
 
1442
- void persistStorageAsync(_memoryCache);
1443
- },
1444
- getAllKeys: function () {
1445
- return Object.keys(_memoryCache);
1446
- }
1412
+ this.persist();
1413
+ };
1414
+
1415
+ SemiAsyncStorage.prototype.getAllKeys = function () {
1416
+ return Object.keys(this._memoryCache);
1417
+ };
1418
+
1419
+ return SemiAsyncStorage;
1420
+ }();
1421
+
1422
+ var version = "2.2.0-alpha1";
1423
+
1424
+ var warn = function (name) {
1425
+ console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1447
1426
  };
1448
1427
 
1449
- var _preloadSemiAsyncStoragePromise;
1428
+ var _OptionalAsyncStorage = undefined;
1450
1429
 
1451
- var preloadSemiAsyncStorage = function () {
1452
- if (_preloadSemiAsyncStoragePromise) {
1453
- return _preloadSemiAsyncStoragePromise;
1430
+ try {
1431
+ _OptionalAsyncStorage = require('@react-native-async-storage/async-storage').default;
1432
+ } catch (e) {
1433
+ warn('@react-native-async-storage/async-storage');
1434
+ }
1435
+
1436
+ var OptionalAsyncStorage = _OptionalAsyncStorage;
1437
+
1438
+ var _OptionalExpoDevice = undefined;
1439
+
1440
+ try {
1441
+ _OptionalExpoDevice = require('expo-device');
1442
+ } catch (e) {}
1443
+
1444
+ var OptionalExpoDevice = _OptionalExpoDevice;
1445
+
1446
+ var _OptionalExpoLocalization = undefined;
1447
+
1448
+ try {
1449
+ _OptionalExpoLocalization = require('expo-localization');
1450
+ } catch (e) {}
1451
+
1452
+ var OptionalExpoLocalization = _OptionalExpoLocalization;
1453
+
1454
+ var _OptionalReactNativeDeviceInfo = undefined;
1455
+
1456
+ try {
1457
+ _OptionalReactNativeDeviceInfo = require('react-native-device-info');
1458
+ } catch (e) {
1459
+ warn('react-native-device-info');
1460
+ }
1461
+
1462
+ var OptionalReactNativeDeviceInfo = _OptionalReactNativeDeviceInfo;
1463
+
1464
+ var getAppProperties = function () {
1465
+ var properties = {};
1466
+
1467
+ if (OptionalExpoApplication) {
1468
+ properties.$app_build = OptionalExpoApplication.nativeBuildVersion;
1469
+ properties.$app_name = OptionalExpoApplication.applicationName;
1470
+ properties.$app_namespace = OptionalExpoApplication.applicationId;
1471
+ properties.$app_version = OptionalExpoApplication.nativeApplicationVersion;
1454
1472
  }
1455
1473
 
1456
- _preloadSemiAsyncStoragePromise = loadStorageAsync().then(function (res) {
1457
- for (var key in res) {
1458
- _memoryCache[key] = _memoryCache[key] === undefined ? res[key] : _memoryCache[key];
1459
- }
1460
- });
1461
- return _preloadSemiAsyncStoragePromise;
1474
+ if (OptionalExpoDevice) {
1475
+ properties.$device_manufacturer = OptionalExpoDevice.manufacturer;
1476
+ properties.$device_name = OptionalExpoDevice.modelName;
1477
+ properties.$os_name = OptionalExpoDevice.osName;
1478
+ properties.$os_version = OptionalExpoDevice.osVersion;
1479
+ }
1480
+
1481
+ if (OptionalExpoLocalization) {
1482
+ properties.$locale = OptionalExpoLocalization.locale;
1483
+ properties.$timezone = OptionalExpoLocalization.timezone;
1484
+ }
1485
+
1486
+ if (OptionalReactNativeDeviceInfo) {
1487
+ properties.$app_build = OptionalReactNativeDeviceInfo.getBuildIdSync();
1488
+ properties.$app_name = OptionalReactNativeDeviceInfo.getApplicationName();
1489
+ properties.$app_namespace = OptionalReactNativeDeviceInfo.getBundleId();
1490
+ properties.$app_version = OptionalReactNativeDeviceInfo.getVersion();
1491
+ properties.$device_manufacturer = OptionalReactNativeDeviceInfo.getManufacturerSync();
1492
+ properties.$device_name = OptionalReactNativeDeviceInfo.getDeviceNameSync();
1493
+ properties.$os_name = OptionalReactNativeDeviceInfo.getSystemName();
1494
+ properties.$os_version = OptionalReactNativeDeviceInfo.getSystemVersion();
1495
+ }
1496
+
1497
+ return properties;
1462
1498
  };
1499
+ var buildOptimisiticAsyncStorage = function () {
1500
+ if (OptionalExpoFileSystem) {
1501
+ var filesystem_1 = OptionalExpoFileSystem;
1502
+ return {
1503
+ getItem: function (key) {
1504
+ return __awaiter(this, void 0, void 0, function () {
1505
+ var uri, stringContent;
1506
+ return __generator(this, function (_a) {
1507
+ switch (_a.label) {
1508
+ case 0:
1509
+ uri = (filesystem_1.documentDirectory || '') + key;
1510
+ _a.label = 1;
1511
+
1512
+ case 1:
1513
+ _a.trys.push([1, 3,, 4]);
1514
+
1515
+ return [4
1516
+ /*yield*/
1517
+ , filesystem_1.readAsStringAsync(uri)];
1518
+
1519
+ case 2:
1520
+ stringContent = _a.sent();
1521
+ return [2
1522
+ /*return*/
1523
+ , stringContent];
1524
+
1525
+ case 3:
1526
+ _a.sent();
1527
+ return [2
1528
+ /*return*/
1529
+ , null];
1530
+
1531
+ case 4:
1532
+ return [2
1533
+ /*return*/
1534
+ ];
1535
+ }
1536
+ });
1537
+ });
1538
+ },
1539
+ setItem: function (key, value) {
1540
+ return __awaiter(this, void 0, void 0, function () {
1541
+ var uri;
1542
+ return __generator(this, function (_a) {
1543
+ switch (_a.label) {
1544
+ case 0:
1545
+ uri = (filesystem_1.documentDirectory || '') + key;
1546
+ return [4
1547
+ /*yield*/
1548
+ , filesystem_1.writeAsStringAsync(uri, value)];
1549
+
1550
+ case 1:
1551
+ _a.sent();
1552
+
1553
+ return [2
1554
+ /*return*/
1555
+ ];
1556
+ }
1557
+ });
1558
+ });
1559
+ }
1560
+ };
1561
+ }
1463
1562
 
1464
- var version = "2.1.4";
1563
+ if (OptionalAsyncStorage) {
1564
+ return OptionalAsyncStorage;
1565
+ }
1566
+
1567
+ console.log(OptionalAsyncStorage);
1568
+ throw new Error('PostHog: No storage available. Please install expo-filesystem or react-native-async-storage OR implement a custom storage provider.');
1569
+ };
1465
1570
 
1466
1571
  var PostHog =
1467
1572
  /** @class */
@@ -1472,29 +1577,51 @@ function (_super) {
1472
1577
  var _this = _super.call(this, apiKey, options) || this;
1473
1578
 
1474
1579
  _this._memoryStorage = new PostHogMemoryStorage();
1580
+ _this._appProperties = {};
1475
1581
  _this._persistence = options === null || options === void 0 ? void 0 : options.persistence;
1582
+ _this._appProperties = (options === null || options === void 0 ? void 0 : options.customAppProperties) || getAppProperties();
1583
+ _this._semiAsyncStorage = new SemiAsyncStorage((options === null || options === void 0 ? void 0 : options.customAsyncStorage) || buildOptimisiticAsyncStorage());
1476
1584
  reactNative.AppState.addEventListener('change', function () {
1477
1585
  _this.flush();
1478
1586
  }); // Ensure the async storage has been preloaded (this call is cached)
1479
1587
  // It is possible that the old library was used so we try to get the legacy distinctID
1480
1588
 
1481
- void preloadSemiAsyncStorage().then(function () {
1589
+ void _this._semiAsyncStorage.preloadAsync().then(function () {
1482
1590
  _this.setupBootstrap(options);
1483
1591
 
1484
- if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1592
+ if (!_this._semiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1485
1593
  getLegacyValues().then(function (legacyValues) {
1486
1594
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
1487
- SemiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1488
- SemiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1595
+ _this._semiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1596
+
1597
+ _this._semiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1489
1598
  }
1490
1599
  });
1491
1600
  }
1492
1601
  });
1493
1602
  return _this;
1494
1603
  }
1604
+ /** Await this method to ensure that all state has been loaded from the async provider */
1605
+
1495
1606
 
1496
- PostHog.initAsync = function () {
1497
- return preloadSemiAsyncStorage();
1607
+ PostHog.prototype.initAsync = function () {
1608
+ return __awaiter(this, void 0, void 0, function () {
1609
+ return __generator(this, function (_a) {
1610
+ switch (_a.label) {
1611
+ case 0:
1612
+ return [4
1613
+ /*yield*/
1614
+ , this._semiAsyncStorage];
1615
+
1616
+ case 1:
1617
+ _a.sent();
1618
+
1619
+ return [2
1620
+ /*return*/
1621
+ ];
1622
+ }
1623
+ });
1624
+ });
1498
1625
  };
1499
1626
 
1500
1627
  PostHog.prototype.getPersistedProperty = function (key) {
@@ -1502,7 +1629,7 @@ function (_super) {
1502
1629
  return this._memoryStorage.getProperty(key);
1503
1630
  }
1504
1631
 
1505
- return SemiAsyncStorage.getItem(key) || undefined;
1632
+ return this._semiAsyncStorage.getItem(key) || undefined;
1506
1633
  };
1507
1634
 
1508
1635
  PostHog.prototype.setPersistedProperty = function (key, value) {
@@ -1510,7 +1637,7 @@ function (_super) {
1510
1637
  return this._memoryStorage.setProperty(key, value);
1511
1638
  }
1512
1639
 
1513
- return value !== null ? SemiAsyncStorage.setItem(key, value) : SemiAsyncStorage.removeItem(key);
1640
+ return value !== null ? this._semiAsyncStorage.setItem(key, value) : this._semiAsyncStorage.removeItem(key);
1514
1641
  };
1515
1642
 
1516
1643
  PostHog.prototype.fetch = function (url, options) {
@@ -1530,20 +1657,10 @@ function (_super) {
1530
1657
  };
1531
1658
 
1532
1659
  PostHog.prototype.getCommonEventProperties = function () {
1533
- return __assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), {
1534
- $app_build: '1',
1535
- $app_name: ExpoApplication__namespace.applicationName,
1536
- $app_namespace: ExpoApplication__namespace.applicationId,
1537
- $app_version: ExpoApplication__namespace.nativeApplicationVersion,
1538
- $device_manufacturer: ExpoDevice__namespace.manufacturer,
1539
- $device_name: ExpoDevice__namespace.modelName,
1660
+ return __assign(__assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), this._appProperties), {
1540
1661
  $device_type: reactNative.Platform.OS,
1541
- $locale: ExpoLocalization__namespace.locale,
1542
- $os_name: ExpoDevice__namespace.osName,
1543
- $os_version: ExpoDevice__namespace.osVersion,
1544
1662
  $screen_height: reactNative.Dimensions.get('screen').height,
1545
- $screen_width: reactNative.Dimensions.get('screen').width,
1546
- $timezone: ExpoLocalization__namespace.timezone
1663
+ $screen_width: reactNative.Dimensions.get('screen').width
1547
1664
  });
1548
1665
  }; // Custom methods
1549
1666
 
@@ -1556,9 +1673,6 @@ function (_super) {
1556
1673
 
1557
1674
  return PostHog;
1558
1675
  }(PostHogCore);
1559
- // is used
1560
-
1561
- void PostHog.initAsync();
1562
1676
 
1563
1677
  var PostHogContext = /*#__PURE__*/React__default["default"].createContext({
1564
1678
  client: undefined
@@ -1603,10 +1717,6 @@ function useLifecycleTracker(client) {
1603
1717
 
1604
1718
  var _OptionalReactNativeNavigation = undefined;
1605
1719
 
1606
- var warn = function (name) {
1607
- console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1608
- };
1609
-
1610
1720
  try {
1611
1721
  _OptionalReactNativeNavigation = require('@react-navigation/native');
1612
1722
  } catch (e) {