posthog-react-native 2.0.0-alpha10 → 2.0.0-alpha13

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/index.cjs.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var reactNative = require('react-native');
6
6
  var ExpoApplication = require('expo-application');
7
7
  var ExpoDevice = require('expo-device');
8
+ var ExpoLocalization = require('expo-localization');
8
9
  var FileSystem = require('expo-file-system');
9
10
  var React = require('react');
10
11
 
@@ -30,6 +31,7 @@ function _interopNamespace(e) {
30
31
 
31
32
  var ExpoApplication__namespace = /*#__PURE__*/_interopNamespace(ExpoApplication);
32
33
  var ExpoDevice__namespace = /*#__PURE__*/_interopNamespace(ExpoDevice);
34
+ var ExpoLocalization__namespace = /*#__PURE__*/_interopNamespace(ExpoLocalization);
33
35
  var FileSystem__namespace = /*#__PURE__*/_interopNamespace(FileSystem);
34
36
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
35
37
 
@@ -166,6 +168,7 @@ function __generator(thisArg, body) {
166
168
 
167
169
  var PostHogPersistedProperty;
168
170
  (function (PostHogPersistedProperty) {
171
+ PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
169
172
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
170
173
  PostHogPersistedProperty["Props"] = "props";
171
174
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
@@ -720,7 +723,7 @@ var SimpleEventEmitter = /** @class */ (function () {
720
723
  var PostHogCore = /** @class */ (function () {
721
724
  function PostHogCore(apiKey, options) {
722
725
  var _this = this;
723
- var _a, _b, _c, _d, _e, _f;
726
+ var _a, _b, _c, _d, _e;
724
727
  this.flagCallReported = {};
725
728
  // internal
726
729
  this._events = new SimpleEventEmitter();
@@ -731,14 +734,13 @@ var PostHogCore = /** @class */ (function () {
731
734
  this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
732
735
  this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
733
736
  this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
734
- this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
735
737
  // If enable is explicitly set to false we override the optout
736
738
  this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
737
739
  this._retryOptions = {
738
- retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3,
739
- retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000,
740
+ retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
741
+ retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
740
742
  };
741
- this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
743
+ this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes
742
744
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
743
745
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
744
746
  safeSetTimeout(function () {
@@ -790,7 +792,6 @@ var PostHogCore = /** @class */ (function () {
790
792
  for (var key in PostHogPersistedProperty) {
791
793
  this.setPersistedProperty(PostHogPersistedProperty[key], null);
792
794
  }
793
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
794
795
  };
795
796
  PostHogCore.prototype.debug = function (enabled) {
796
797
  var _a;
@@ -820,13 +821,16 @@ var PostHogCore = /** @class */ (function () {
820
821
  PostHogCore.prototype.resetSessionId = function () {
821
822
  this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
822
823
  };
823
- PostHogCore.prototype.getDistinctId = function () {
824
- var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
825
- if (!distinctId) {
826
- distinctId = generateUUID(globalThis);
827
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
824
+ PostHogCore.prototype.getAnonymousId = function () {
825
+ var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
826
+ if (!anonId) {
827
+ anonId = generateUUID(globalThis);
828
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
828
829
  }
829
- return distinctId;
830
+ return anonId;
831
+ };
832
+ PostHogCore.prototype.getDistinctId = function () {
833
+ return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
830
834
  };
831
835
  PostHogCore.prototype.register = function (properties) {
832
836
  this.props = __assign(__assign({}, this.props), properties);
@@ -840,27 +844,28 @@ var PostHogCore = /** @class */ (function () {
840
844
  *** TRACKING
841
845
  ***/
842
846
  PostHogCore.prototype.identify = function (distinctId, properties) {
843
- distinctId = distinctId || this.getDistinctId();
847
+ var previousDistinctId = this.getDistinctId();
848
+ distinctId = distinctId || previousDistinctId;
844
849
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
845
850
  this.groups(properties.$groups);
846
851
  }
847
852
  var payload = __assign(__assign({}, this.buildPayload({
848
853
  distinct_id: distinctId,
849
854
  event: '$identify',
850
- properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }),
855
+ properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
851
856
  })), { $set: properties });
852
- if (distinctId !== this.getDistinctId()) {
857
+ if (distinctId !== previousDistinctId) {
858
+ // We keep the AnonymousId to be used by decide calls and identify to link the previousId
859
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
853
860
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
861
+ if (this.getFeatureFlags()) {
862
+ void this.reloadFeatureFlagsAsync();
863
+ }
854
864
  }
855
865
  this.enqueue('identify', payload);
856
866
  return this;
857
867
  };
858
868
  PostHogCore.prototype.capture = function (event, properties) {
859
- // NOTE: Legacy nodejs implementation uses groups
860
- if (properties && properties['groups']) {
861
- properties.$groups = properties.groups;
862
- delete properties.groups;
863
- }
864
869
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
865
870
  this.groups(properties.$groups);
866
871
  }
@@ -899,7 +904,7 @@ var PostHogCore = /** @class */ (function () {
899
904
  this.register({
900
905
  $groups: __assign(__assign({}, existingGroups), groups),
901
906
  });
902
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) {
907
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
903
908
  void this.reloadFeatureFlagsAsync();
904
909
  }
905
910
  return this;
@@ -943,16 +948,24 @@ var PostHogCore = /** @class */ (function () {
943
948
  fetchOptions = {
944
949
  method: 'POST',
945
950
  headers: { 'Content-Type': 'application/json' },
946
- body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }),
951
+ body: JSON.stringify({
952
+ token: this.apiKey,
953
+ distinct_id: distinctId,
954
+ $anon_distinct_id: this.getAnonymousId(),
955
+ groups: groups,
956
+ }),
947
957
  };
948
958
  this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
949
959
  .then(function (r) { return r.json(); })
950
960
  .then(function (res) {
951
961
  if (res.featureFlags) {
952
962
  _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
963
+ _this._events.emit('featureflags', res.featureFlags);
953
964
  }
954
- _this._events.emit('featureflags', res.featureFlags);
955
965
  return res;
966
+ })
967
+ .finally(function () {
968
+ _this._decideResponsePromise = undefined;
956
969
  });
957
970
  return [2 /*return*/, this._decideResponsePromise];
958
971
  });
@@ -1001,27 +1014,16 @@ var PostHogCore = /** @class */ (function () {
1001
1014
  };
1002
1015
  PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
1003
1016
  return __awaiter(this, void 0, void 0, function () {
1004
- var _this = this;
1005
1017
  return __generator(this, function (_a) {
1006
1018
  switch (_a.label) {
1007
- case 0:
1008
- clearTimeout(this._decideTimer);
1009
- if (this._decidePollInterval) {
1010
- this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
1011
- }
1012
- this._decideResponsePromise = undefined;
1013
- return [4 /*yield*/, this.decideAsync()];
1019
+ case 0: return [4 /*yield*/, this.decideAsync()];
1014
1020
  case 1: return [2 /*return*/, (_a.sent()).featureFlags];
1015
1021
  }
1016
1022
  });
1017
1023
  });
1018
1024
  };
1019
- // When listening to feature flags polling is active
1020
1025
  PostHogCore.prototype.onFeatureFlags = function (cb) {
1021
1026
  var _this = this;
1022
- if (!this._decideTimer) {
1023
- void this.reloadFeatureFlagsAsync();
1024
- }
1025
1027
  return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
1026
1028
  var flags;
1027
1029
  return __generator(this, function (_a) {
@@ -1033,6 +1035,19 @@ var PostHogCore = /** @class */ (function () {
1033
1035
  });
1034
1036
  }); });
1035
1037
  };
1038
+ PostHogCore.prototype.onFeatureFlag = function (key, cb) {
1039
+ var _this = this;
1040
+ return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
1041
+ var flagResponse;
1042
+ return __generator(this, function (_a) {
1043
+ flagResponse = this.getFeatureFlag(key);
1044
+ if (flagResponse !== undefined) {
1045
+ cb(flagResponse);
1046
+ }
1047
+ return [2 /*return*/];
1048
+ });
1049
+ }); });
1050
+ };
1036
1051
  PostHogCore.prototype.overrideFeatureFlag = function (flags) {
1037
1052
  if (flags === null) {
1038
1053
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
@@ -1140,7 +1155,6 @@ var PostHogCore = /** @class */ (function () {
1140
1155
  return __generator(this, function (_a) {
1141
1156
  switch (_a.label) {
1142
1157
  case 0:
1143
- clearTimeout(this._decideTimer);
1144
1158
  clearTimeout(this._flushTimer);
1145
1159
  return [4 /*yield*/, this.flushAsync()];
1146
1160
  case 1:
@@ -1156,6 +1170,19 @@ var PostHogCore = /** @class */ (function () {
1156
1170
  return PostHogCore;
1157
1171
  }());
1158
1172
 
1173
+ var PostHogMemoryStorage = /** @class */ (function () {
1174
+ function PostHogMemoryStorage() {
1175
+ this._memoryStorage = {};
1176
+ }
1177
+ PostHogMemoryStorage.prototype.getProperty = function (key) {
1178
+ return this._memoryStorage[key];
1179
+ };
1180
+ PostHogMemoryStorage.prototype.setProperty = function (key, value) {
1181
+ this._memoryStorage[key] = value !== null ? value : undefined;
1182
+ };
1183
+ return PostHogMemoryStorage;
1184
+ }());
1185
+
1159
1186
  var getLegacyValues = function () {
1160
1187
  return __awaiter(void 0, void 0, void 0, function () {
1161
1188
  var posthogFileDirectory, posthogDistinctIdFile, posthogAnonymousIdFile, res, _a, _b, _c, _d, _e, _f;
@@ -1340,36 +1367,7 @@ var preloadSemiAsyncStorage = function () {
1340
1367
  return _preloadSemiAsyncStoragePromise;
1341
1368
  };
1342
1369
 
1343
- var _OptionalReactNativeNavigation = undefined;
1344
- var _OptionalExpoLocalization = undefined;
1345
- var _OptionalExpoNetwork = undefined;
1346
-
1347
- var warn = function (name) {
1348
- console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1349
- };
1350
-
1351
- try {
1352
- _OptionalReactNativeNavigation = require('@react-navigation/native');
1353
- } catch (e) {
1354
- warn('@react-navigation/native');
1355
- }
1356
-
1357
- try {
1358
- _OptionalExpoLocalization = require('expo-localization');
1359
- } catch (e) {
1360
- warn('expo-localization');
1361
- }
1362
-
1363
- try {
1364
- _OptionalExpoNetwork = require('expo-network');
1365
- } catch (e) {
1366
- warn('expo-network');
1367
- }
1368
-
1369
- var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1370
- var OptionalExpoLocalization = _OptionalExpoLocalization;
1371
-
1372
- var version = "2.0.0-alpha10";
1370
+ var version = "2.0.0-alpha13";
1373
1371
 
1374
1372
  var PostHog =
1375
1373
  /** @class */
@@ -1379,16 +1377,19 @@ function (_super) {
1379
1377
  function PostHog(apiKey, options) {
1380
1378
  var _this = _super.call(this, apiKey, options) || this;
1381
1379
 
1380
+ _this._memoryStorage = new PostHogMemoryStorage();
1381
+ _this._persistence = options === null || options === void 0 ? void 0 : options.persistence;
1382
1382
  reactNative.AppState.addEventListener('change', function () {
1383
1383
  _this.flush();
1384
1384
  }); // Ensure the async storage has been preloaded (this call is cached)
1385
1385
  // It is possible that the old library was used so we try to get the legacy distinctID
1386
1386
 
1387
1387
  void preloadSemiAsyncStorage().then(function () {
1388
- if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.DistinctId)) {
1388
+ if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1389
1389
  getLegacyValues().then(function (legacyValues) {
1390
1390
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
1391
1391
  SemiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1392
+ SemiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1392
1393
  }
1393
1394
  });
1394
1395
  }
@@ -1401,10 +1402,18 @@ function (_super) {
1401
1402
  };
1402
1403
 
1403
1404
  PostHog.prototype.getPersistedProperty = function (key) {
1405
+ if (this._persistence === 'memory') {
1406
+ return this._memoryStorage.getProperty(key);
1407
+ }
1408
+
1404
1409
  return SemiAsyncStorage.getItem(key) || undefined;
1405
1410
  };
1406
1411
 
1407
1412
  PostHog.prototype.setPersistedProperty = function (key, value) {
1413
+ if (this._persistence === 'memory') {
1414
+ return this._memoryStorage.getProperty(key);
1415
+ }
1416
+
1408
1417
  return value !== null ? SemiAsyncStorage.setItem(key, value) : SemiAsyncStorage.removeItem(key);
1409
1418
  };
1410
1419
 
@@ -1421,29 +1430,25 @@ function (_super) {
1421
1430
  };
1422
1431
 
1423
1432
  PostHog.prototype.getCustomUserAgent = function () {
1424
- // TODO
1425
1433
  return;
1426
1434
  };
1427
1435
 
1428
1436
  PostHog.prototype.getCommonEventProperties = function () {
1437
+ console.log('DeviceInfo', ExpoDevice__namespace);
1429
1438
  return __assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), {
1430
1439
  $app_build: '1',
1431
- $app_name: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.applicationName,
1432
- $app_namespace: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.applicationId,
1433
- $app_version: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.nativeApplicationVersion,
1434
- // "$device_id": "F31C35E8-5B28-4626-8AFC-213D1C655FF9",
1435
- $device_manufacturer: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.manufacturer,
1436
- // "$device_model": "x86_64",
1437
- $device_name: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.modelName,
1438
- // "$device_type": "ios",
1439
- $locale: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.locale,
1440
- // "$network_cellular": false,
1441
- // "$network_wifi": true,
1442
- $os_name: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.osName,
1443
- $os_version: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.osVersion,
1440
+ $app_name: ExpoApplication__namespace.applicationName,
1441
+ $app_namespace: ExpoApplication__namespace.applicationId,
1442
+ $app_version: ExpoApplication__namespace.nativeApplicationVersion,
1443
+ $device_manufacturer: ExpoDevice__namespace.manufacturer,
1444
+ $device_name: ExpoDevice__namespace.modelName,
1445
+ $device_type: reactNative.Platform.OS,
1446
+ $locale: ExpoLocalization__namespace.locale,
1447
+ $os_name: ExpoDevice__namespace.osName,
1448
+ $os_version: ExpoDevice__namespace.osVersion,
1444
1449
  $screen_height: reactNative.Dimensions.get('screen').height,
1445
1450
  $screen_width: reactNative.Dimensions.get('screen').width,
1446
- $timezone: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.timezone
1451
+ $timezone: ExpoLocalization__namespace.timezone
1447
1452
  });
1448
1453
  }; // Custom methods
1449
1454
 
@@ -1501,6 +1506,20 @@ function useLifecycleTracker(client) {
1501
1506
  }, [posthog]);
1502
1507
  }
1503
1508
 
1509
+ var _OptionalReactNativeNavigation = undefined;
1510
+
1511
+ var warn = function (name) {
1512
+ console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1513
+ };
1514
+
1515
+ try {
1516
+ _OptionalReactNativeNavigation = require('@react-navigation/native');
1517
+ } catch (e) {
1518
+ warn('@react-navigation/native');
1519
+ }
1520
+
1521
+ var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1522
+
1504
1523
  function _useNavigationTrackerDisabled() {
1505
1524
  return;
1506
1525
  }
@@ -1647,7 +1666,9 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1647
1666
  _e = options.maxElementsCaptured,
1648
1667
  maxElementsCaptured = _e === void 0 ? 20 : _e,
1649
1668
  _f = options.ignoreLabels,
1650
- ignoreLabels = _f === void 0 ? [] : _f;
1669
+ ignoreLabels = _f === void 0 ? [] : _f,
1670
+ _g = options.propsToCapture,
1671
+ propsToCapture = _g === void 0 ? ['style', 'testID', 'accessibilityLabel', 'ph-label'] : _g;
1651
1672
 
1652
1673
  if (!e._targetInst) {
1653
1674
  return;
@@ -1662,8 +1683,19 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1662
1683
  };
1663
1684
  var props = currentInst.memoizedProps;
1664
1685
 
1686
+ if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1687
+ return {
1688
+ value: void 0
1689
+ };
1690
+ }
1691
+
1665
1692
  if (props) {
1693
+ // Capture only props we have said to capture. By default this is only "safe" props
1666
1694
  Object.keys(props).forEach(function (key) {
1695
+ if (!propsToCapture.includes(key)) {
1696
+ return;
1697
+ }
1698
+
1667
1699
  var value = props[key];
1668
1700
 
1669
1701
  if (key === 'style') {
@@ -1676,12 +1708,6 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1676
1708
  }
1677
1709
  }
1678
1710
  });
1679
- }
1680
-
1681
- if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1682
- return {
1683
- value: void 0
1684
- };
1685
1711
  } // Try and find a sensible label
1686
1712
 
1687
1713