posthog-react-native 2.0.0 → 2.1.0

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,5 @@
1
+ # 2.1.0 - 2022-09-02
2
+
3
+ What's new:
4
+
5
+ 1. 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
@@ -164,6 +164,17 @@ function __generator(thisArg, body) {
164
164
  throw op[1];
165
165
  return { value: op[0] ? op[1] : void 0, done: true };
166
166
  }
167
+ }
168
+ function __spreadArray(to, from, pack) {
169
+ if (pack || arguments.length === 2)
170
+ for (var i = 0, l = from.length, ar; i < l; i++) {
171
+ if (ar || !(i in from)) {
172
+ if (!ar)
173
+ ar = Array.prototype.slice.call(from, 0, i);
174
+ ar[i] = from[i];
175
+ }
176
+ }
177
+ return to.concat(ar || Array.prototype.slice.call(from));
167
178
  }
168
179
 
169
180
  var PostHogPersistedProperty;
@@ -177,6 +188,8 @@ var PostHogPersistedProperty;
177
188
  PostHogPersistedProperty["OptedOut"] = "opted_out";
178
189
  PostHogPersistedProperty["SessionId"] = "session_id";
179
190
  PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
191
+ PostHogPersistedProperty["PersonProperties"] = "person_properties";
192
+ PostHogPersistedProperty["GroupProperties"] = "group_properties";
180
193
  })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
181
194
 
182
195
  function assert(truthyValue, message) {
@@ -750,12 +763,14 @@ var PostHogCore = /** @class */ (function () {
750
763
  }
751
764
  PostHogCore.prototype.getCommonEventProperties = function () {
752
765
  var featureFlags = this.getFeatureFlags();
753
- return {
754
- $lib: this.getLibraryId(),
755
- $lib_version: this.getLibraryVersion(),
756
- $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined,
757
- $enabled_feature_flags: featureFlags,
758
- };
766
+ var featureVariantProperties = {};
767
+ if (featureFlags) {
768
+ for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
769
+ var _b = _a[_i], feature = _b[0], variant = _b[1];
770
+ featureVariantProperties["$feature/".concat(feature)] = variant;
771
+ }
772
+ }
773
+ return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
759
774
  };
760
775
  Object.defineProperty(PostHogCore.prototype, "props", {
761
776
  // NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
@@ -771,6 +786,9 @@ var PostHogCore = /** @class */ (function () {
771
786
  enumerable: false,
772
787
  configurable: true
773
788
  });
789
+ PostHogCore.prototype.clearProps = function () {
790
+ this.props = undefined;
791
+ };
774
792
  Object.defineProperty(PostHogCore.prototype, "optedOut", {
775
793
  get: function () {
776
794
  var _a, _b;
@@ -788,9 +806,15 @@ var PostHogCore = /** @class */ (function () {
788
806
  PostHogCore.prototype.on = function (event, cb) {
789
807
  return this._events.on(event, cb);
790
808
  };
791
- PostHogCore.prototype.reset = function () {
792
- for (var key in PostHogPersistedProperty) {
793
- this.setPersistedProperty(PostHogPersistedProperty[key], null);
809
+ PostHogCore.prototype.reset = function (propertiesToKeep) {
810
+ var allPropertiesToKeep = __spreadArray([PostHogPersistedProperty.Queue], (propertiesToKeep || []), true);
811
+ // clean up props
812
+ this.clearProps();
813
+ for (var _i = 0, _a = Object.keys(PostHogPersistedProperty); _i < _a.length; _i++) {
814
+ var key = _a[_i];
815
+ if (!allPropertiesToKeep.includes(PostHogPersistedProperty[key])) {
816
+ this.setPersistedProperty(PostHogPersistedProperty[key], null);
817
+ }
794
818
  }
795
819
  };
796
820
  PostHogCore.prototype.debug = function (enabled) {
@@ -865,12 +889,18 @@ var PostHogCore = /** @class */ (function () {
865
889
  this.enqueue('identify', payload);
866
890
  return this;
867
891
  };
868
- PostHogCore.prototype.capture = function (event, properties) {
892
+ PostHogCore.prototype.capture = function (event, properties, forceSendFeatureFlags) {
893
+ if (forceSendFeatureFlags === void 0) { forceSendFeatureFlags = false; }
869
894
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
870
895
  this.groups(properties.$groups);
871
896
  }
872
- var payload = this.buildPayload({ event: event, properties: properties });
873
- this.enqueue('capture', payload);
897
+ if (forceSendFeatureFlags) {
898
+ this._sendFeatureFlags(event, properties);
899
+ }
900
+ else {
901
+ var payload = this.buildPayload({ event: event, properties: properties });
902
+ this.enqueue('capture', payload);
903
+ }
874
904
  return this;
875
905
  };
876
906
  PostHogCore.prototype.alias = function (alias) {
@@ -900,7 +930,6 @@ var PostHogCore = /** @class */ (function () {
900
930
  PostHogCore.prototype.groups = function (groups) {
901
931
  // Get persisted groups
902
932
  var existingGroups = this.props.$groups || {};
903
- // NOTE: Should we do the same for groups listed in identify / capture?
904
933
  this.register({
905
934
  $groups: __assign(__assign({}, existingGroups), groups),
906
935
  });
@@ -928,31 +957,58 @@ var PostHogCore = /** @class */ (function () {
928
957
  this.enqueue('capture', payload);
929
958
  return this;
930
959
  };
960
+ /***
961
+ * PROPERTIES
962
+ ***/
963
+ PostHogCore.prototype.personProperties = function (properties) {
964
+ // Get persisted person properties
965
+ var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
966
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
967
+ return this;
968
+ };
969
+ PostHogCore.prototype.groupProperties = function (properties) {
970
+ // Get persisted group properties
971
+ var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
972
+ if (Object.keys(existingProperties).length !== 0) {
973
+ Object.keys(existingProperties).forEach(function (groupType) {
974
+ existingProperties[groupType] = __assign(__assign({}, existingProperties[groupType]), properties[groupType]);
975
+ delete properties[groupType];
976
+ });
977
+ }
978
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
979
+ return this;
980
+ };
931
981
  /***
932
982
  *** FEATURE FLAGS
933
983
  ***/
934
- PostHogCore.prototype.decideAsync = function () {
984
+ PostHogCore.prototype.decideAsync = function (sendAnonDistinctId) {
985
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
935
986
  if (this._decideResponsePromise) {
936
987
  return this._decideResponsePromise;
937
988
  }
938
- return this._decideAsync();
989
+ return this._decideAsync(sendAnonDistinctId);
939
990
  };
940
- PostHogCore.prototype._decideAsync = function () {
991
+ PostHogCore.prototype._decideAsync = function (sendAnonDistinctId) {
992
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
941
993
  return __awaiter(this, void 0, void 0, function () {
942
- var url, distinctId, groups, fetchOptions;
994
+ var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
943
995
  var _this = this;
944
996
  return __generator(this, function (_a) {
945
997
  url = "".concat(this.host, "/decide/?v=2");
946
998
  distinctId = this.getDistinctId();
947
999
  groups = this.props.$groups || {};
1000
+ personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1001
+ groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
948
1002
  fetchOptions = {
949
1003
  method: 'POST',
950
1004
  headers: { 'Content-Type': 'application/json' },
951
1005
  body: JSON.stringify({
952
1006
  token: this.apiKey,
953
1007
  distinct_id: distinctId,
954
- $anon_distinct_id: this.getAnonymousId(),
1008
+ $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
955
1009
  groups: groups,
1010
+ person_properties: personProperties,
1011
+ group_properties: groupProperties,
956
1012
  }),
957
1013
  };
958
1014
  this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
@@ -971,23 +1027,26 @@ var PostHogCore = /** @class */ (function () {
971
1027
  });
972
1028
  });
973
1029
  };
974
- PostHogCore.prototype.getFeatureFlag = function (key, defaultResult) {
975
- var _a;
976
- if (defaultResult === void 0) { defaultResult = false; }
1030
+ PostHogCore.prototype.getFeatureFlag = function (key) {
977
1031
  var featureFlags = this.getFeatureFlags();
978
1032
  if (!featureFlags) {
979
- // If we haven't loaded flags yet we respond undefined to indicate this
1033
+ // If we haven't loaded flags yet, or errored out, we respond with undefined
980
1034
  return undefined;
981
1035
  }
1036
+ var response = featureFlags[key];
1037
+ if (response === undefined) {
1038
+ // `/decide` returns nothing for flags which are false.
1039
+ response = false;
1040
+ }
982
1041
  if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
983
1042
  this.flagCallReported[key] = true;
984
1043
  this.capture('$feature_flag_called', {
985
1044
  $feature_flag: key,
986
- $feature_flag_response: featureFlags[key],
1045
+ $feature_flag_response: response,
987
1046
  });
988
1047
  }
989
- // If we have flags we either return the value (true or string) or the defaultResult
990
- return (_a = featureFlags[key]) !== null && _a !== void 0 ? _a : defaultResult;
1048
+ // If we have flags we either return the value (true or string) or false
1049
+ return response;
991
1050
  };
992
1051
  PostHogCore.prototype.getFeatureFlags = function () {
993
1052
  var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
@@ -1006,17 +1065,19 @@ var PostHogCore = /** @class */ (function () {
1006
1065
  }
1007
1066
  return flags;
1008
1067
  };
1009
- PostHogCore.prototype.isFeatureEnabled = function (key, defaultResult) {
1010
- var _a;
1011
- if (defaultResult === void 0) { defaultResult = false; }
1012
- var flag = (_a = this.getFeatureFlag(key, defaultResult)) !== null && _a !== void 0 ? _a : defaultResult;
1013
- return !!flag;
1068
+ PostHogCore.prototype.isFeatureEnabled = function (key) {
1069
+ var response = this.getFeatureFlag(key);
1070
+ if (response === undefined) {
1071
+ return undefined;
1072
+ }
1073
+ return !!response;
1014
1074
  };
1015
- PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
1075
+ PostHogCore.prototype.reloadFeatureFlagsAsync = function (sendAnonDistinctId) {
1076
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
1016
1077
  return __awaiter(this, void 0, void 0, function () {
1017
1078
  return __generator(this, function (_a) {
1018
1079
  switch (_a.label) {
1019
- case 0: return [4 /*yield*/, this.decideAsync()];
1080
+ case 0: return [4 /*yield*/, this.decideAsync(sendAnonDistinctId)];
1020
1081
  case 1: return [2 /*return*/, (_a.sent()).featureFlags];
1021
1082
  }
1022
1083
  });
@@ -1054,6 +1115,14 @@ var PostHogCore = /** @class */ (function () {
1054
1115
  }
1055
1116
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
1056
1117
  };
1118
+ PostHogCore.prototype._sendFeatureFlags = function (event, properties) {
1119
+ var _this = this;
1120
+ this.reloadFeatureFlagsAsync(false).finally(function () {
1121
+ // Try to enqueue message irrespective of errors during feature flag fetching
1122
+ var payload = _this.buildPayload({ event: event, properties: properties });
1123
+ _this.enqueue('capture', payload);
1124
+ });
1125
+ };
1057
1126
  /***
1058
1127
  *** QUEUEING AND FLUSHING
1059
1128
  ***/
@@ -1082,13 +1151,15 @@ var PostHogCore = /** @class */ (function () {
1082
1151
  PostHogCore.prototype.flushAsync = function () {
1083
1152
  var _this = this;
1084
1153
  return new Promise(function (resolve, reject) {
1085
- _this.flush(function (err, data) { return (err ? reject(err) : resolve(data)); });
1154
+ _this.flush(function (err, data) {
1155
+ return err ? reject(err) : resolve(data);
1156
+ });
1086
1157
  });
1087
1158
  };
1088
1159
  PostHogCore.prototype.flush = function (callback) {
1089
1160
  var _this = this;
1090
1161
  if (this.optedOut) {
1091
- return callback && safeSetTimeout(callback, 0);
1162
+ return callback === null || callback === void 0 ? void 0 : callback();
1092
1163
  }
1093
1164
  if (this._flushTimer) {
1094
1165
  clearTimeout(this._flushTimer);
@@ -1096,7 +1167,7 @@ var PostHogCore = /** @class */ (function () {
1096
1167
  }
1097
1168
  var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1098
1169
  if (!queue.length) {
1099
- return callback && safeSetTimeout(callback, 0);
1170
+ return callback === null || callback === void 0 ? void 0 : callback();
1100
1171
  }
1101
1172
  var items = queue.splice(0, this.flushAt);
1102
1173
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
@@ -1367,7 +1438,7 @@ var preloadSemiAsyncStorage = function () {
1367
1438
  return _preloadSemiAsyncStoragePromise;
1368
1439
  };
1369
1440
 
1370
- var version = "2.0.0";
1441
+ var version = "2.1.0";
1371
1442
 
1372
1443
  var PostHog =
1373
1444
  /** @class */
@@ -1604,10 +1675,10 @@ function useFeatureFlags(client) {
1604
1675
  return featureFlags;
1605
1676
  }
1606
1677
 
1607
- function useFeatureFlag(flag, defaultValue) {
1678
+ function useFeatureFlag(flag) {
1608
1679
  var posthog = usePostHog();
1609
1680
 
1610
- var _a = React.useState(posthog === null || posthog === void 0 ? void 0 : posthog.getFeatureFlag(flag, defaultValue)),
1681
+ var _a = React.useState(posthog === null || posthog === void 0 ? void 0 : posthog.getFeatureFlag(flag)),
1611
1682
  featureFlag = _a[0],
1612
1683
  setFeatureFlag = _a[1];
1613
1684
 
@@ -1616,11 +1687,11 @@ function useFeatureFlag(flag, defaultValue) {
1616
1687
  }
1617
1688
 
1618
1689
  React.useEffect(function () {
1619
- setFeatureFlag(posthog.getFeatureFlag(flag, defaultValue));
1690
+ setFeatureFlag(posthog.getFeatureFlag(flag));
1620
1691
  return posthog.onFeatureFlags(function () {
1621
- setFeatureFlag(posthog.getFeatureFlag(flag, defaultValue));
1692
+ setFeatureFlag(posthog.getFeatureFlag(flag));
1622
1693
  });
1623
- }, [posthog, flag, defaultValue]);
1694
+ }, [posthog, flag]);
1624
1695
  return featureFlag;
1625
1696
  }
1626
1697
 
@@ -1735,14 +1806,20 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1735
1806
  }
1736
1807
  };
1737
1808
 
1738
- function PostHogHooks(_a) {
1809
+ function PostHogNavigationHook(_a) {
1739
1810
  var options = _a.options;
1740
1811
  useNavigationTracker(options === null || options === void 0 ? void 0 : options.navigation);
1812
+ return null;
1813
+ }
1814
+
1815
+ function PostHogLifecycleHook() {
1741
1816
  useLifecycleTracker();
1742
1817
  return null;
1743
1818
  }
1744
1819
 
1745
1820
  var PostHogProvider = function (_a) {
1821
+ var _b, _c;
1822
+
1746
1823
  var children = _a.children,
1747
1824
  client = _a.client,
1748
1825
  options = _a.options,
@@ -1755,10 +1832,13 @@ var PostHogProvider = function (_a) {
1755
1832
  posthogRef.current = client ? client : apiKey ? new PostHog(apiKey, options) : undefined;
1756
1833
  }
1757
1834
 
1758
- var autocaptureEnabled = !!autocapture;
1759
1835
  var autocaptureOptions = autocapture && typeof autocapture !== 'boolean' ? autocapture : {};
1760
1836
  var posthog = posthogRef.current;
1761
1837
  var captureTouches = posthog && (autocapture === true || (autocaptureOptions === null || autocaptureOptions === void 0 ? void 0 : autocaptureOptions.captureTouches));
1838
+ 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
1839
+
1840
+ 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
1841
+
1762
1842
  var onTouch = React.useCallback(function (type, e) {
1763
1843
  // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1764
1844
  if (!captureTouches) {
@@ -1768,8 +1848,7 @@ var PostHogProvider = function (_a) {
1768
1848
  if (type === 'end') {
1769
1849
  autocaptureFromTouchEvent(e, posthog, autocaptureOptions);
1770
1850
  }
1771
- }, [posthog, autocapture]); // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1772
-
1851
+ }, [posthog, autocapture]);
1773
1852
  return /*#__PURE__*/React__default["default"].createElement(reactNative.View, {
1774
1853
  "ph-label": "PostHogProvider",
1775
1854
  style: style || {
@@ -1782,9 +1861,9 @@ var PostHogProvider = function (_a) {
1782
1861
  value: {
1783
1862
  client: posthogRef.current
1784
1863
  }
1785
- }, autocaptureEnabled ? /*#__PURE__*/React__default["default"].createElement(PostHogHooks, {
1864
+ }, /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, captureScreens ? /*#__PURE__*/React__default["default"].createElement(PostHogNavigationHook, {
1786
1865
  options: autocaptureOptions
1787
- }) : null, children));
1866
+ }) : null, captureLifecycle ? /*#__PURE__*/React__default["default"].createElement(PostHogLifecycleHook, null) : null), children));
1788
1867
  };
1789
1868
 
1790
1869
  exports.PostHog = PostHog;