posthog-react-native 2.1.2 → 2.1.4

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.d.ts CHANGED
@@ -9,6 +9,11 @@ declare type PosthogCoreOptions = {
9
9
  enable?: boolean;
10
10
  sendFeatureFlagEvent?: boolean;
11
11
  preloadFeatureFlags?: boolean;
12
+ bootstrap?: {
13
+ distinctId?: string;
14
+ isIdentifiedId?: boolean;
15
+ featureFlags?: Record<string, boolean | string>;
16
+ };
12
17
  fetchRetryCount?: number;
13
18
  fetchRetryDelay?: number;
14
19
  sessionExpirationTimeSeconds?: number;
@@ -34,7 +39,8 @@ declare type PostHogFetchOptions = {
34
39
  headers: {
35
40
  [key: string]: string;
36
41
  };
37
- body: string;
42
+ body?: string;
43
+ signal?: AbortSignal;
38
44
  };
39
45
  declare type PostHogFetchResponse = {
40
46
  status: number;
@@ -108,6 +114,7 @@ declare abstract class PostHogCore {
108
114
  private _optoutOverride;
109
115
  constructor(apiKey: string, options?: PosthogCoreOptions);
110
116
  protected getCommonEventProperties(): any;
117
+ protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
111
118
  private get props();
112
119
  private set props(value);
113
120
  private clearProps;
@@ -158,6 +165,7 @@ declare abstract class PostHogCore {
158
165
  ***/
159
166
  private decideAsync;
160
167
  private _decideAsync;
168
+ private setKnownFeatureFlags;
161
169
  getFeatureFlag(key: string): boolean | string | undefined;
162
170
  getFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined;
163
171
  isFeatureEnabled(key: string): boolean | undefined;
@@ -210,7 +218,9 @@ declare type PostHogAutocaptureOptions = {
210
218
  maxElementsCaptured?: number;
211
219
  ignoreLabels?: string[];
212
220
  propsToCapture?: string[];
221
+ captureScreens?: boolean;
213
222
  navigation?: PostHogAutocaptureNavigationTrackerOptions;
223
+ captureLifecycleEvents?: boolean;
214
224
  };
215
225
 
216
226
  declare function _useNavigationTracker(options?: PostHogAutocaptureNavigationTrackerOptions, client?: PostHog): void;
package/lib/index.esm.js CHANGED
@@ -742,6 +742,26 @@ var PostHogCore = /** @class */ (function () {
742
742
  }
743
743
  return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
744
744
  };
745
+ PostHogCore.prototype.setupBootstrap = function (options) {
746
+ var _a, _b, _c, _d;
747
+ if ((_a = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _a === void 0 ? void 0 : _a.distinctId) {
748
+ if ((_b = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _b === void 0 ? void 0 : _b.isIdentifiedId) {
749
+ this.setPersistedProperty(PostHogPersistedProperty.DistinctId, options.bootstrap.distinctId);
750
+ }
751
+ else {
752
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, options.bootstrap.distinctId);
753
+ }
754
+ }
755
+ if ((_c = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _c === void 0 ? void 0 : _c.featureFlags) {
756
+ var activeFlags = Object.keys(((_d = options.bootstrap) === null || _d === void 0 ? void 0 : _d.featureFlags) || {})
757
+ .filter(function (flag) { var _a, _b; return !!((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[flag]); })
758
+ .reduce(function (res, key) {
759
+ var _a, _b;
760
+ return ((res[key] = ((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[key]) || false), res);
761
+ }, {});
762
+ this.setKnownFeatureFlags(activeFlags);
763
+ }
764
+ };
745
765
  Object.defineProperty(PostHogCore.prototype, "props", {
746
766
  // NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
747
767
  get: function () {
@@ -985,8 +1005,7 @@ var PostHogCore = /** @class */ (function () {
985
1005
  .then(function (r) { return r.json(); })
986
1006
  .then(function (res) {
987
1007
  if (res.featureFlags) {
988
- _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
989
- _this._events.emit('featureflags', res.featureFlags);
1008
+ _this.setKnownFeatureFlags(res.featureFlags);
990
1009
  }
991
1010
  return res;
992
1011
  })
@@ -997,6 +1016,10 @@ var PostHogCore = /** @class */ (function () {
997
1016
  });
998
1017
  });
999
1018
  };
1019
+ PostHogCore.prototype.setKnownFeatureFlags = function (featureFlags) {
1020
+ this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
1021
+ this._events.emit('featureflags', featureFlags);
1022
+ };
1000
1023
  PostHogCore.prototype.getFeatureFlag = function (key) {
1001
1024
  var featureFlags = this.getFeatureFlags();
1002
1025
  if (!featureFlags) {
@@ -1308,7 +1331,7 @@ var loadStorageAsync = function () {
1308
1331
  return __generator(this, function (_a) {
1309
1332
  switch (_a.label) {
1310
1333
  case 0:
1311
- uri = FileSystem.documentDirectory + POSTHOG_STORAGE_KEY;
1334
+ uri = (FileSystem.documentDirectory || '') + POSTHOG_STORAGE_KEY;
1312
1335
  _a.label = 1;
1313
1336
 
1314
1337
  case 1:
@@ -1339,13 +1362,13 @@ var loadStorageAsync = function () {
1339
1362
  });
1340
1363
  };
1341
1364
 
1342
- var persitStorageAsync = function (content) {
1365
+ var persistStorageAsync = function (content) {
1343
1366
  return __awaiter(void 0, void 0, void 0, function () {
1344
1367
  var uri, data;
1345
1368
  return __generator(this, function (_a) {
1346
1369
  switch (_a.label) {
1347
1370
  case 0:
1348
- uri = FileSystem.documentDirectory + POSTHOG_STORAGE_KEY;
1371
+ uri = (FileSystem.documentDirectory || '') + POSTHOG_STORAGE_KEY;
1349
1372
  data = {
1350
1373
  version: POSTHOG_STORAGE_VERSION,
1351
1374
  content: content
@@ -1375,18 +1398,18 @@ var SemiAsyncStorage = {
1375
1398
  },
1376
1399
  setItem: function (key, value) {
1377
1400
  _memoryCache[key] = value;
1378
- void persitStorageAsync(_memoryCache);
1401
+ void persistStorageAsync(_memoryCache);
1379
1402
  },
1380
1403
  removeItem: function (key) {
1381
1404
  delete _memoryCache[key];
1382
- void persitStorageAsync(_memoryCache);
1405
+ void persistStorageAsync(_memoryCache);
1383
1406
  },
1384
1407
  clear: function () {
1385
1408
  for (var key in _memoryCache) {
1386
1409
  delete _memoryCache[key];
1387
1410
  }
1388
1411
 
1389
- void persitStorageAsync(_memoryCache);
1412
+ void persistStorageAsync(_memoryCache);
1390
1413
  },
1391
1414
  getAllKeys: function () {
1392
1415
  return Object.keys(_memoryCache);
@@ -1402,13 +1425,13 @@ var preloadSemiAsyncStorage = function () {
1402
1425
 
1403
1426
  _preloadSemiAsyncStoragePromise = loadStorageAsync().then(function (res) {
1404
1427
  for (var key in res) {
1405
- _memoryCache[key] = res[key];
1428
+ _memoryCache[key] = _memoryCache[key] === undefined ? res[key] : _memoryCache[key];
1406
1429
  }
1407
1430
  });
1408
1431
  return _preloadSemiAsyncStoragePromise;
1409
1432
  };
1410
1433
 
1411
- var version = "2.0.0";
1434
+ var version = "2.1.4";
1412
1435
 
1413
1436
  var PostHog =
1414
1437
  /** @class */
@@ -1426,6 +1449,8 @@ function (_super) {
1426
1449
  // It is possible that the old library was used so we try to get the legacy distinctID
1427
1450
 
1428
1451
  void preloadSemiAsyncStorage().then(function () {
1452
+ _this.setupBootstrap(options);
1453
+
1429
1454
  if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1430
1455
  getLegacyValues().then(function (legacyValues) {
1431
1456
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
@@ -1452,7 +1477,7 @@ function (_super) {
1452
1477
 
1453
1478
  PostHog.prototype.setPersistedProperty = function (key, value) {
1454
1479
  if (this._persistence === 'memory') {
1455
- return this._memoryStorage.getProperty(key);
1480
+ return this._memoryStorage.setProperty(key, value);
1456
1481
  }
1457
1482
 
1458
1483
  return value !== null ? SemiAsyncStorage.setItem(key, value) : SemiAsyncStorage.removeItem(key);
@@ -1776,14 +1801,20 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1776
1801
  }
1777
1802
  };
1778
1803
 
1779
- function PostHogHooks(_a) {
1804
+ function PostHogNavigationHook(_a) {
1780
1805
  var options = _a.options;
1781
1806
  useNavigationTracker(options === null || options === void 0 ? void 0 : options.navigation);
1807
+ return null;
1808
+ }
1809
+
1810
+ function PostHogLifecycleHook() {
1782
1811
  useLifecycleTracker();
1783
1812
  return null;
1784
1813
  }
1785
1814
 
1786
1815
  var PostHogProvider = function (_a) {
1816
+ var _b, _c;
1817
+
1787
1818
  var children = _a.children,
1788
1819
  client = _a.client,
1789
1820
  options = _a.options,
@@ -1796,10 +1827,13 @@ var PostHogProvider = function (_a) {
1796
1827
  posthogRef.current = client ? client : apiKey ? new PostHog(apiKey, options) : undefined;
1797
1828
  }
1798
1829
 
1799
- var autocaptureEnabled = !!autocapture;
1800
1830
  var autocaptureOptions = autocapture && typeof autocapture !== 'boolean' ? autocapture : {};
1801
1831
  var posthog = posthogRef.current;
1802
1832
  var captureTouches = posthog && (autocapture === true || (autocaptureOptions === null || autocaptureOptions === void 0 ? void 0 : autocaptureOptions.captureTouches));
1833
+ var captureScreens = posthog && (autocapture === true || ((_b = autocaptureOptions === null || autocaptureOptions === void 0 ? void 0 : autocaptureOptions.captureScreens) !== null && _b !== void 0 ? _b : true)); // Default to true if not set
1834
+
1835
+ var captureLifecycle = posthog && (autocapture === true || ((_c = autocaptureOptions === null || autocaptureOptions === void 0 ? void 0 : autocaptureOptions.captureLifecycleEvents) !== null && _c !== void 0 ? _c : true)); // Default to true if not set
1836
+
1803
1837
  var onTouch = useCallback(function (type, e) {
1804
1838
  // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1805
1839
  if (!captureTouches) {
@@ -1809,8 +1843,7 @@ var PostHogProvider = function (_a) {
1809
1843
  if (type === 'end') {
1810
1844
  autocaptureFromTouchEvent(e, posthog, autocaptureOptions);
1811
1845
  }
1812
- }, [posthog, autocapture]); // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1813
-
1846
+ }, [posthog, autocapture]);
1814
1847
  return /*#__PURE__*/React.createElement(View, {
1815
1848
  "ph-label": "PostHogProvider",
1816
1849
  style: style || {
@@ -1823,9 +1856,9 @@ var PostHogProvider = function (_a) {
1823
1856
  value: {
1824
1857
  client: posthogRef.current
1825
1858
  }
1826
- }, autocaptureEnabled ? /*#__PURE__*/React.createElement(PostHogHooks, {
1859
+ }, /*#__PURE__*/React.createElement(React.Fragment, null, captureScreens ? /*#__PURE__*/React.createElement(PostHogNavigationHook, {
1827
1860
  options: autocaptureOptions
1828
- }) : null, children));
1861
+ }) : null, captureLifecycle ? /*#__PURE__*/React.createElement(PostHogLifecycleHook, null) : null), children));
1829
1862
  };
1830
1863
 
1831
1864
  export { PostHog, PostHogProvider, PostHog as default, useFeatureFlag, useFeatureFlags, useLifecycleTracker, useNavigationTracker, usePostHog };