posthog-react-native 2.0.0-alpha11 → 2.0.0-alpha12

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:
@@ -1353,36 +1367,7 @@ var preloadSemiAsyncStorage = function () {
1353
1367
  return _preloadSemiAsyncStoragePromise;
1354
1368
  };
1355
1369
 
1356
- var _OptionalReactNativeNavigation = undefined;
1357
- var _OptionalExpoLocalization = undefined;
1358
- var _OptionalExpoNetwork = undefined;
1359
-
1360
- var warn = function (name) {
1361
- console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1362
- };
1363
-
1364
- try {
1365
- _OptionalReactNativeNavigation = require('@react-navigation/native');
1366
- } catch (e) {
1367
- warn('@react-navigation/native');
1368
- }
1369
-
1370
- try {
1371
- _OptionalExpoLocalization = require('expo-localization');
1372
- } catch (e) {
1373
- warn('expo-localization');
1374
- }
1375
-
1376
- try {
1377
- _OptionalExpoNetwork = require('expo-network');
1378
- } catch (e) {
1379
- warn('expo-network');
1380
- }
1381
-
1382
- var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1383
- var OptionalExpoLocalization = _OptionalExpoLocalization;
1384
-
1385
- var version = "2.0.0-alpha11";
1370
+ var version = "2.0.0-alpha12";
1386
1371
 
1387
1372
  var PostHog =
1388
1373
  /** @class */
@@ -1400,10 +1385,11 @@ function (_super) {
1400
1385
  // It is possible that the old library was used so we try to get the legacy distinctID
1401
1386
 
1402
1387
  void preloadSemiAsyncStorage().then(function () {
1403
- if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.DistinctId)) {
1388
+ if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1404
1389
  getLegacyValues().then(function (legacyValues) {
1405
1390
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
1406
1391
  SemiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1392
+ SemiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1407
1393
  }
1408
1394
  });
1409
1395
  }
@@ -1449,24 +1435,25 @@ function (_super) {
1449
1435
  };
1450
1436
 
1451
1437
  PostHog.prototype.getCommonEventProperties = function () {
1438
+ console.log('DeviceInfo', ExpoDevice__namespace);
1452
1439
  return __assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), {
1453
1440
  $app_build: '1',
1454
- $app_name: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.applicationName,
1455
- $app_namespace: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.applicationId,
1456
- $app_version: ExpoApplication__namespace === null || ExpoApplication__namespace === void 0 ? void 0 : ExpoApplication__namespace.nativeApplicationVersion,
1441
+ $app_name: ExpoApplication__namespace.applicationName,
1442
+ $app_namespace: ExpoApplication__namespace.applicationId,
1443
+ $app_version: ExpoApplication__namespace.nativeApplicationVersion,
1457
1444
  // "$device_id": "F31C35E8-5B28-4626-8AFC-213D1C655FF9",
1458
- $device_manufacturer: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.manufacturer,
1445
+ $device_manufacturer: ExpoDevice__namespace.manufacturer,
1459
1446
  // "$device_model": "x86_64",
1460
- $device_name: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.modelName,
1447
+ $device_name: ExpoDevice__namespace.modelName,
1461
1448
  // "$device_type": "ios",
1462
- $locale: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.locale,
1449
+ $locale: ExpoLocalization__namespace.locale,
1463
1450
  // "$network_cellular": false,
1464
1451
  // "$network_wifi": true,
1465
- $os_name: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.osName,
1466
- $os_version: ExpoDevice__namespace === null || ExpoDevice__namespace === void 0 ? void 0 : ExpoDevice__namespace.osVersion,
1452
+ $os_name: ExpoDevice__namespace.osName,
1453
+ $os_version: ExpoDevice__namespace.osVersion,
1467
1454
  $screen_height: reactNative.Dimensions.get('screen').height,
1468
1455
  $screen_width: reactNative.Dimensions.get('screen').width,
1469
- $timezone: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.timezone
1456
+ $timezone: ExpoLocalization__namespace.timezone
1470
1457
  });
1471
1458
  }; // Custom methods
1472
1459
 
@@ -1524,6 +1511,20 @@ function useLifecycleTracker(client) {
1524
1511
  }, [posthog]);
1525
1512
  }
1526
1513
 
1514
+ var _OptionalReactNativeNavigation = undefined;
1515
+
1516
+ var warn = function (name) {
1517
+ console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1518
+ };
1519
+
1520
+ try {
1521
+ _OptionalReactNativeNavigation = require('@react-navigation/native');
1522
+ } catch (e) {
1523
+ warn('@react-navigation/native');
1524
+ }
1525
+
1526
+ var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1527
+
1527
1528
  function _useNavigationTrackerDisabled() {
1528
1529
  return;
1529
1530
  }
@@ -1670,7 +1671,9 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1670
1671
  _e = options.maxElementsCaptured,
1671
1672
  maxElementsCaptured = _e === void 0 ? 20 : _e,
1672
1673
  _f = options.ignoreLabels,
1673
- ignoreLabels = _f === void 0 ? [] : _f;
1674
+ ignoreLabels = _f === void 0 ? [] : _f,
1675
+ _g = options.propsToCapture,
1676
+ propsToCapture = _g === void 0 ? ['style', 'testID', 'accessibilityLabel', 'ph-label'] : _g;
1674
1677
 
1675
1678
  if (!e._targetInst) {
1676
1679
  return;
@@ -1685,8 +1688,19 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1685
1688
  };
1686
1689
  var props = currentInst.memoizedProps;
1687
1690
 
1691
+ if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1692
+ return {
1693
+ value: void 0
1694
+ };
1695
+ }
1696
+
1688
1697
  if (props) {
1698
+ // Capture only props we have said to capture. By default this is only "safe" props
1689
1699
  Object.keys(props).forEach(function (key) {
1700
+ if (!propsToCapture.includes(key)) {
1701
+ return;
1702
+ }
1703
+
1690
1704
  var value = props[key];
1691
1705
 
1692
1706
  if (key === 'style') {
@@ -1699,12 +1713,6 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1699
1713
  }
1700
1714
  }
1701
1715
  });
1702
- }
1703
-
1704
- if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1705
- return {
1706
- value: void 0
1707
- };
1708
1716
  } // Try and find a sensible label
1709
1717
 
1710
1718