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.d.ts CHANGED
@@ -9,13 +9,13 @@ declare type PosthogCoreOptions = {
9
9
  enable?: boolean;
10
10
  sendFeatureFlagEvent?: boolean;
11
11
  preloadFeatureFlags?: boolean;
12
- decidePollInterval?: number;
13
12
  fetchRetryCount?: number;
14
13
  fetchRetryDelay?: number;
15
14
  sessionExpirationTimeSeconds?: number;
16
15
  captureMode?: 'json' | 'form';
17
16
  };
18
17
  declare enum PostHogPersistedProperty {
18
+ AnonymousId = "anonymous_id",
19
19
  DistinctId = "distinct_id",
20
20
  Props = "props",
21
21
  FeatureFlags = "feature_flags",
@@ -37,6 +37,7 @@ declare type PostHogFetchOptions = {
37
37
  declare type PostHogFetchResponse = {
38
38
  status: number;
39
39
  text: () => Promise<string>;
40
+ json: () => Promise<any>;
40
41
  };
41
42
  declare type PostHogEventProperties = {
42
43
  [key: string]: any;
@@ -94,8 +95,6 @@ declare abstract class PostHogCore {
94
95
  protected _events: SimpleEventEmitter;
95
96
  protected _flushTimer?: any;
96
97
  protected _decideResponsePromise?: Promise<PostHogDecideResponse>;
97
- protected _decideTimer?: any;
98
- protected _decidePollInterval: number;
99
98
  protected _retryOptions: RetriableOptions;
100
99
  protected _sessionExpirationTimeSeconds: number;
101
100
  abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
@@ -119,6 +118,7 @@ declare abstract class PostHogCore {
119
118
  private buildPayload;
120
119
  getSessionId(): string | undefined;
121
120
  resetSessionId(): void;
121
+ getAnonymousId(): string;
122
122
  getDistinctId(): string;
123
123
  register(properties: {
124
124
  [key: string]: any;
@@ -151,6 +151,7 @@ declare abstract class PostHogCore {
151
151
  isFeatureEnabled(key: string, defaultResult?: boolean): boolean;
152
152
  reloadFeatureFlagsAsync(): Promise<PostHogDecideResponse['featureFlags']>;
153
153
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
154
+ onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
154
155
  overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void;
155
156
  /***
156
157
  *** QUEUEING AND FLUSHING
@@ -193,6 +194,7 @@ declare type PostHogAutocaptureOptions = {
193
194
  noCaptureProp?: string;
194
195
  maxElementsCaptured?: number;
195
196
  ignoreLabels?: string[];
197
+ propsToCapture?: string[];
196
198
  navigation?: PostHogAutocaptureNavigationTrackerOptions;
197
199
  };
198
200
 
package/lib/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Platform, Dimensions, AppState, View } from 'react-native';
2
2
  import * as ExpoApplication from 'expo-application';
3
3
  import * as ExpoDevice from 'expo-device';
4
+ import * as ExpoLocalization from 'expo-localization';
4
5
  import * as FileSystem from 'expo-file-system';
5
6
  import React, { useRef, useEffect, useState, useCallback } from 'react';
6
7
 
@@ -137,6 +138,7 @@ function __generator(thisArg, body) {
137
138
 
138
139
  var PostHogPersistedProperty;
139
140
  (function (PostHogPersistedProperty) {
141
+ PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
140
142
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
141
143
  PostHogPersistedProperty["Props"] = "props";
142
144
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
@@ -691,7 +693,7 @@ var SimpleEventEmitter = /** @class */ (function () {
691
693
  var PostHogCore = /** @class */ (function () {
692
694
  function PostHogCore(apiKey, options) {
693
695
  var _this = this;
694
- var _a, _b, _c, _d, _e, _f;
696
+ var _a, _b, _c, _d, _e;
695
697
  this.flagCallReported = {};
696
698
  // internal
697
699
  this._events = new SimpleEventEmitter();
@@ -702,14 +704,13 @@ var PostHogCore = /** @class */ (function () {
702
704
  this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
703
705
  this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
704
706
  this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
705
- this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
706
707
  // If enable is explicitly set to false we override the optout
707
708
  this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
708
709
  this._retryOptions = {
709
- retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3,
710
- retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000,
710
+ retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
711
+ retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
711
712
  };
712
- this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
713
+ this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes
713
714
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
714
715
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
715
716
  safeSetTimeout(function () {
@@ -761,7 +762,6 @@ var PostHogCore = /** @class */ (function () {
761
762
  for (var key in PostHogPersistedProperty) {
762
763
  this.setPersistedProperty(PostHogPersistedProperty[key], null);
763
764
  }
764
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
765
765
  };
766
766
  PostHogCore.prototype.debug = function (enabled) {
767
767
  var _a;
@@ -791,13 +791,16 @@ var PostHogCore = /** @class */ (function () {
791
791
  PostHogCore.prototype.resetSessionId = function () {
792
792
  this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
793
793
  };
794
- PostHogCore.prototype.getDistinctId = function () {
795
- var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
796
- if (!distinctId) {
797
- distinctId = generateUUID(globalThis);
798
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
794
+ PostHogCore.prototype.getAnonymousId = function () {
795
+ var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
796
+ if (!anonId) {
797
+ anonId = generateUUID(globalThis);
798
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
799
799
  }
800
- return distinctId;
800
+ return anonId;
801
+ };
802
+ PostHogCore.prototype.getDistinctId = function () {
803
+ return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
801
804
  };
802
805
  PostHogCore.prototype.register = function (properties) {
803
806
  this.props = __assign(__assign({}, this.props), properties);
@@ -811,27 +814,28 @@ var PostHogCore = /** @class */ (function () {
811
814
  *** TRACKING
812
815
  ***/
813
816
  PostHogCore.prototype.identify = function (distinctId, properties) {
814
- distinctId = distinctId || this.getDistinctId();
817
+ var previousDistinctId = this.getDistinctId();
818
+ distinctId = distinctId || previousDistinctId;
815
819
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
816
820
  this.groups(properties.$groups);
817
821
  }
818
822
  var payload = __assign(__assign({}, this.buildPayload({
819
823
  distinct_id: distinctId,
820
824
  event: '$identify',
821
- properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }),
825
+ properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
822
826
  })), { $set: properties });
823
- if (distinctId !== this.getDistinctId()) {
827
+ if (distinctId !== previousDistinctId) {
828
+ // We keep the AnonymousId to be used by decide calls and identify to link the previousId
829
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
824
830
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
831
+ if (this.getFeatureFlags()) {
832
+ void this.reloadFeatureFlagsAsync();
833
+ }
825
834
  }
826
835
  this.enqueue('identify', payload);
827
836
  return this;
828
837
  };
829
838
  PostHogCore.prototype.capture = function (event, properties) {
830
- // NOTE: Legacy nodejs implementation uses groups
831
- if (properties && properties['groups']) {
832
- properties.$groups = properties.groups;
833
- delete properties.groups;
834
- }
835
839
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
836
840
  this.groups(properties.$groups);
837
841
  }
@@ -870,7 +874,7 @@ var PostHogCore = /** @class */ (function () {
870
874
  this.register({
871
875
  $groups: __assign(__assign({}, existingGroups), groups),
872
876
  });
873
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) {
877
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
874
878
  void this.reloadFeatureFlagsAsync();
875
879
  }
876
880
  return this;
@@ -914,16 +918,24 @@ var PostHogCore = /** @class */ (function () {
914
918
  fetchOptions = {
915
919
  method: 'POST',
916
920
  headers: { 'Content-Type': 'application/json' },
917
- body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }),
921
+ body: JSON.stringify({
922
+ token: this.apiKey,
923
+ distinct_id: distinctId,
924
+ $anon_distinct_id: this.getAnonymousId(),
925
+ groups: groups,
926
+ }),
918
927
  };
919
928
  this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
920
929
  .then(function (r) { return r.json(); })
921
930
  .then(function (res) {
922
931
  if (res.featureFlags) {
923
932
  _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
933
+ _this._events.emit('featureflags', res.featureFlags);
924
934
  }
925
- _this._events.emit('featureflags', res.featureFlags);
926
935
  return res;
936
+ })
937
+ .finally(function () {
938
+ _this._decideResponsePromise = undefined;
927
939
  });
928
940
  return [2 /*return*/, this._decideResponsePromise];
929
941
  });
@@ -972,27 +984,16 @@ var PostHogCore = /** @class */ (function () {
972
984
  };
973
985
  PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
974
986
  return __awaiter(this, void 0, void 0, function () {
975
- var _this = this;
976
987
  return __generator(this, function (_a) {
977
988
  switch (_a.label) {
978
- case 0:
979
- clearTimeout(this._decideTimer);
980
- if (this._decidePollInterval) {
981
- this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
982
- }
983
- this._decideResponsePromise = undefined;
984
- return [4 /*yield*/, this.decideAsync()];
989
+ case 0: return [4 /*yield*/, this.decideAsync()];
985
990
  case 1: return [2 /*return*/, (_a.sent()).featureFlags];
986
991
  }
987
992
  });
988
993
  });
989
994
  };
990
- // When listening to feature flags polling is active
991
995
  PostHogCore.prototype.onFeatureFlags = function (cb) {
992
996
  var _this = this;
993
- if (!this._decideTimer) {
994
- void this.reloadFeatureFlagsAsync();
995
- }
996
997
  return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
997
998
  var flags;
998
999
  return __generator(this, function (_a) {
@@ -1004,6 +1005,19 @@ var PostHogCore = /** @class */ (function () {
1004
1005
  });
1005
1006
  }); });
1006
1007
  };
1008
+ PostHogCore.prototype.onFeatureFlag = function (key, cb) {
1009
+ var _this = this;
1010
+ return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
1011
+ var flagResponse;
1012
+ return __generator(this, function (_a) {
1013
+ flagResponse = this.getFeatureFlag(key);
1014
+ if (flagResponse !== undefined) {
1015
+ cb(flagResponse);
1016
+ }
1017
+ return [2 /*return*/];
1018
+ });
1019
+ }); });
1020
+ };
1007
1021
  PostHogCore.prototype.overrideFeatureFlag = function (flags) {
1008
1022
  if (flags === null) {
1009
1023
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
@@ -1111,7 +1125,6 @@ var PostHogCore = /** @class */ (function () {
1111
1125
  return __generator(this, function (_a) {
1112
1126
  switch (_a.label) {
1113
1127
  case 0:
1114
- clearTimeout(this._decideTimer);
1115
1128
  clearTimeout(this._flushTimer);
1116
1129
  return [4 /*yield*/, this.flushAsync()];
1117
1130
  case 1:
@@ -1324,36 +1337,7 @@ var preloadSemiAsyncStorage = function () {
1324
1337
  return _preloadSemiAsyncStoragePromise;
1325
1338
  };
1326
1339
 
1327
- var _OptionalReactNativeNavigation = undefined;
1328
- var _OptionalExpoLocalization = undefined;
1329
- var _OptionalExpoNetwork = undefined;
1330
-
1331
- var warn = function (name) {
1332
- console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1333
- };
1334
-
1335
- try {
1336
- _OptionalReactNativeNavigation = require('@react-navigation/native');
1337
- } catch (e) {
1338
- warn('@react-navigation/native');
1339
- }
1340
-
1341
- try {
1342
- _OptionalExpoLocalization = require('expo-localization');
1343
- } catch (e) {
1344
- warn('expo-localization');
1345
- }
1346
-
1347
- try {
1348
- _OptionalExpoNetwork = require('expo-network');
1349
- } catch (e) {
1350
- warn('expo-network');
1351
- }
1352
-
1353
- var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1354
- var OptionalExpoLocalization = _OptionalExpoLocalization;
1355
-
1356
- var version = "2.0.0-alpha11";
1340
+ var version = "2.0.0-alpha12";
1357
1341
 
1358
1342
  var PostHog =
1359
1343
  /** @class */
@@ -1371,10 +1355,11 @@ function (_super) {
1371
1355
  // It is possible that the old library was used so we try to get the legacy distinctID
1372
1356
 
1373
1357
  void preloadSemiAsyncStorage().then(function () {
1374
- if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.DistinctId)) {
1358
+ if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1375
1359
  getLegacyValues().then(function (legacyValues) {
1376
1360
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
1377
1361
  SemiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1362
+ SemiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1378
1363
  }
1379
1364
  });
1380
1365
  }
@@ -1420,24 +1405,25 @@ function (_super) {
1420
1405
  };
1421
1406
 
1422
1407
  PostHog.prototype.getCommonEventProperties = function () {
1408
+ console.log('DeviceInfo', ExpoDevice);
1423
1409
  return __assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), {
1424
1410
  $app_build: '1',
1425
- $app_name: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.applicationName,
1426
- $app_namespace: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.applicationId,
1427
- $app_version: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.nativeApplicationVersion,
1411
+ $app_name: ExpoApplication.applicationName,
1412
+ $app_namespace: ExpoApplication.applicationId,
1413
+ $app_version: ExpoApplication.nativeApplicationVersion,
1428
1414
  // "$device_id": "F31C35E8-5B28-4626-8AFC-213D1C655FF9",
1429
- $device_manufacturer: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.manufacturer,
1415
+ $device_manufacturer: ExpoDevice.manufacturer,
1430
1416
  // "$device_model": "x86_64",
1431
- $device_name: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.modelName,
1417
+ $device_name: ExpoDevice.modelName,
1432
1418
  // "$device_type": "ios",
1433
- $locale: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.locale,
1419
+ $locale: ExpoLocalization.locale,
1434
1420
  // "$network_cellular": false,
1435
1421
  // "$network_wifi": true,
1436
- $os_name: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.osName,
1437
- $os_version: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.osVersion,
1422
+ $os_name: ExpoDevice.osName,
1423
+ $os_version: ExpoDevice.osVersion,
1438
1424
  $screen_height: Dimensions.get('screen').height,
1439
1425
  $screen_width: Dimensions.get('screen').width,
1440
- $timezone: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.timezone
1426
+ $timezone: ExpoLocalization.timezone
1441
1427
  });
1442
1428
  }; // Custom methods
1443
1429
 
@@ -1495,6 +1481,20 @@ function useLifecycleTracker(client) {
1495
1481
  }, [posthog]);
1496
1482
  }
1497
1483
 
1484
+ var _OptionalReactNativeNavigation = undefined;
1485
+
1486
+ var warn = function (name) {
1487
+ console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1488
+ };
1489
+
1490
+ try {
1491
+ _OptionalReactNativeNavigation = require('@react-navigation/native');
1492
+ } catch (e) {
1493
+ warn('@react-navigation/native');
1494
+ }
1495
+
1496
+ var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1497
+
1498
1498
  function _useNavigationTrackerDisabled() {
1499
1499
  return;
1500
1500
  }
@@ -1641,7 +1641,9 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1641
1641
  _e = options.maxElementsCaptured,
1642
1642
  maxElementsCaptured = _e === void 0 ? 20 : _e,
1643
1643
  _f = options.ignoreLabels,
1644
- ignoreLabels = _f === void 0 ? [] : _f;
1644
+ ignoreLabels = _f === void 0 ? [] : _f,
1645
+ _g = options.propsToCapture,
1646
+ propsToCapture = _g === void 0 ? ['style', 'testID', 'accessibilityLabel', 'ph-label'] : _g;
1645
1647
 
1646
1648
  if (!e._targetInst) {
1647
1649
  return;
@@ -1656,8 +1658,19 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1656
1658
  };
1657
1659
  var props = currentInst.memoizedProps;
1658
1660
 
1661
+ if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1662
+ return {
1663
+ value: void 0
1664
+ };
1665
+ }
1666
+
1659
1667
  if (props) {
1668
+ // Capture only props we have said to capture. By default this is only "safe" props
1660
1669
  Object.keys(props).forEach(function (key) {
1670
+ if (!propsToCapture.includes(key)) {
1671
+ return;
1672
+ }
1673
+
1661
1674
  var value = props[key];
1662
1675
 
1663
1676
  if (key === 'style') {
@@ -1670,12 +1683,6 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1670
1683
  }
1671
1684
  }
1672
1685
  });
1673
- }
1674
-
1675
- if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1676
- return {
1677
- value: void 0
1678
- };
1679
1686
  } // Try and find a sensible label
1680
1687
 
1681
1688