posthog-react-native 2.0.0-alpha8 → 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/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
 
@@ -133,10 +134,22 @@ function __generator(thisArg, body) {
133
134
  throw op[1];
134
135
  return { value: op[0] ? op[1] : void 0, done: true };
135
136
  }
137
+ }
138
+ function __spreadArray(to, from, pack) {
139
+ if (pack || arguments.length === 2)
140
+ for (var i = 0, l = from.length, ar; i < l; i++) {
141
+ if (ar || !(i in from)) {
142
+ if (!ar)
143
+ ar = Array.prototype.slice.call(from, 0, i);
144
+ ar[i] = from[i];
145
+ }
146
+ }
147
+ return to.concat(ar || Array.prototype.slice.call(from));
136
148
  }
137
149
 
138
150
  var PostHogPersistedProperty;
139
151
  (function (PostHogPersistedProperty) {
152
+ PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
140
153
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
141
154
  PostHogPersistedProperty["Props"] = "props";
142
155
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
@@ -145,6 +158,8 @@ var PostHogPersistedProperty;
145
158
  PostHogPersistedProperty["OptedOut"] = "opted_out";
146
159
  PostHogPersistedProperty["SessionId"] = "session_id";
147
160
  PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
161
+ PostHogPersistedProperty["PersonProperties"] = "person_properties";
162
+ PostHogPersistedProperty["GroupProperties"] = "group_properties";
148
163
  })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
149
164
 
150
165
  function assert(truthyValue, message) {
@@ -162,7 +177,7 @@ function retriable(fn, props) {
162
177
  return __generator(this, function (_d) {
163
178
  switch (_d.label) {
164
179
  case 0:
165
- _a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ? 1000 : _b, _c = props.retryCheck, retryCheck = _c === void 0 ? function () { return true; } : _c;
180
+ _a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ? 5000 : _b, _c = props.retryCheck, retryCheck = _c === void 0 ? function () { return true; } : _c;
166
181
  lastError = null;
167
182
  i = 0;
168
183
  _d.label = 1;
@@ -676,13 +691,14 @@ var SimpleEventEmitter = /** @class */ (function () {
676
691
  };
677
692
  };
678
693
  SimpleEventEmitter.prototype.emit = function (event, payload) {
679
- if (!this.events[event]) {
680
- return;
681
- }
682
- for (var _i = 0, _a = this.events[event]; _i < _a.length; _i++) {
694
+ for (var _i = 0, _a = this.events[event] || []; _i < _a.length; _i++) {
683
695
  var listener = _a[_i];
684
696
  listener(payload);
685
697
  }
698
+ for (var _b = 0, _c = this.events['*'] || []; _b < _c.length; _b++) {
699
+ var listener = _c[_b];
700
+ listener(event, payload);
701
+ }
686
702
  };
687
703
  return SimpleEventEmitter;
688
704
  }());
@@ -690,7 +706,7 @@ var SimpleEventEmitter = /** @class */ (function () {
690
706
  var PostHogCore = /** @class */ (function () {
691
707
  function PostHogCore(apiKey, options) {
692
708
  var _this = this;
693
- var _a, _b, _c, _d, _e, _f;
709
+ var _a, _b, _c, _d, _e;
694
710
  this.flagCallReported = {};
695
711
  // internal
696
712
  this._events = new SimpleEventEmitter();
@@ -701,14 +717,13 @@ var PostHogCore = /** @class */ (function () {
701
717
  this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
702
718
  this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
703
719
  this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
704
- this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
705
720
  // If enable is explicitly set to false we override the optout
706
721
  this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
707
722
  this._retryOptions = {
708
- retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3,
709
- retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000,
723
+ retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
724
+ retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
710
725
  };
711
- this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
726
+ this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes
712
727
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
713
728
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
714
729
  safeSetTimeout(function () {
@@ -718,12 +733,14 @@ var PostHogCore = /** @class */ (function () {
718
733
  }
719
734
  PostHogCore.prototype.getCommonEventProperties = function () {
720
735
  var featureFlags = this.getFeatureFlags();
721
- return {
722
- $lib: this.getLibraryId(),
723
- $lib_version: this.getLibraryVersion(),
724
- $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined,
725
- $enabled_feature_flags: featureFlags,
726
- };
736
+ var featureVariantProperties = {};
737
+ if (featureFlags) {
738
+ for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
739
+ var _b = _a[_i], feature = _b[0], variant = _b[1];
740
+ featureVariantProperties["$feature/".concat(feature)] = variant;
741
+ }
742
+ }
743
+ return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
727
744
  };
728
745
  Object.defineProperty(PostHogCore.prototype, "props", {
729
746
  // NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
@@ -739,6 +756,9 @@ var PostHogCore = /** @class */ (function () {
739
756
  enumerable: false,
740
757
  configurable: true
741
758
  });
759
+ PostHogCore.prototype.clearProps = function () {
760
+ this.props = undefined;
761
+ };
742
762
  Object.defineProperty(PostHogCore.prototype, "optedOut", {
743
763
  get: function () {
744
764
  var _a, _b;
@@ -756,11 +776,24 @@ var PostHogCore = /** @class */ (function () {
756
776
  PostHogCore.prototype.on = function (event, cb) {
757
777
  return this._events.on(event, cb);
758
778
  };
759
- PostHogCore.prototype.reset = function () {
760
- for (var key in PostHogPersistedProperty) {
761
- this.setPersistedProperty(PostHogPersistedProperty[key], null);
779
+ PostHogCore.prototype.reset = function (propertiesToKeep) {
780
+ var allPropertiesToKeep = __spreadArray([PostHogPersistedProperty.Queue], (propertiesToKeep || []), true);
781
+ // clean up props
782
+ this.clearProps();
783
+ for (var _i = 0, _a = Object.keys(PostHogPersistedProperty); _i < _a.length; _i++) {
784
+ var key = _a[_i];
785
+ if (!allPropertiesToKeep.includes(PostHogPersistedProperty[key])) {
786
+ this.setPersistedProperty(PostHogPersistedProperty[key], null);
787
+ }
788
+ }
789
+ };
790
+ PostHogCore.prototype.debug = function (enabled) {
791
+ var _a;
792
+ if (enabled === void 0) { enabled = true; }
793
+ (_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
794
+ if (enabled) {
795
+ this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
762
796
  }
763
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
764
797
  };
765
798
  PostHogCore.prototype.buildPayload = function (payload) {
766
799
  return {
@@ -779,13 +812,19 @@ var PostHogCore = /** @class */ (function () {
779
812
  this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, Date.now());
780
813
  return sessionId;
781
814
  };
782
- PostHogCore.prototype.getDistinctId = function () {
783
- var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
784
- if (!distinctId) {
785
- distinctId = generateUUID(globalThis);
786
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
815
+ PostHogCore.prototype.resetSessionId = function () {
816
+ this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
817
+ };
818
+ PostHogCore.prototype.getAnonymousId = function () {
819
+ var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
820
+ if (!anonId) {
821
+ anonId = generateUUID(globalThis);
822
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
787
823
  }
788
- return distinctId;
824
+ return anonId;
825
+ };
826
+ PostHogCore.prototype.getDistinctId = function () {
827
+ return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
789
828
  };
790
829
  PostHogCore.prototype.register = function (properties) {
791
830
  this.props = __assign(__assign({}, this.props), properties);
@@ -799,32 +838,39 @@ var PostHogCore = /** @class */ (function () {
799
838
  *** TRACKING
800
839
  ***/
801
840
  PostHogCore.prototype.identify = function (distinctId, properties) {
802
- distinctId = distinctId || this.getDistinctId();
841
+ var previousDistinctId = this.getDistinctId();
842
+ distinctId = distinctId || previousDistinctId;
803
843
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
804
844
  this.groups(properties.$groups);
805
845
  }
806
846
  var payload = __assign(__assign({}, this.buildPayload({
807
847
  distinct_id: distinctId,
808
848
  event: '$identify',
809
- properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }),
849
+ properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
810
850
  })), { $set: properties });
811
- if (distinctId !== this.getDistinctId()) {
851
+ if (distinctId !== previousDistinctId) {
852
+ // We keep the AnonymousId to be used by decide calls and identify to link the previousId
853
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
812
854
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
855
+ if (this.getFeatureFlags()) {
856
+ void this.reloadFeatureFlagsAsync();
857
+ }
813
858
  }
814
859
  this.enqueue('identify', payload);
815
860
  return this;
816
861
  };
817
- PostHogCore.prototype.capture = function (event, properties) {
818
- // NOTE: Legacy nodejs implementation uses groups
819
- if (properties && properties['groups']) {
820
- properties.$groups = properties.groups;
821
- delete properties.groups;
822
- }
862
+ PostHogCore.prototype.capture = function (event, properties, forceSendFeatureFlags) {
863
+ if (forceSendFeatureFlags === void 0) { forceSendFeatureFlags = false; }
823
864
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
824
865
  this.groups(properties.$groups);
825
866
  }
826
- var payload = this.buildPayload({ event: event, properties: properties });
827
- this.enqueue('capture', payload);
867
+ if (forceSendFeatureFlags) {
868
+ this._sendFeatureFlags(event, properties);
869
+ }
870
+ else {
871
+ var payload = this.buildPayload({ event: event, properties: properties });
872
+ this.enqueue('capture', payload);
873
+ }
828
874
  return this;
829
875
  };
830
876
  PostHogCore.prototype.alias = function (alias) {
@@ -854,11 +900,10 @@ var PostHogCore = /** @class */ (function () {
854
900
  PostHogCore.prototype.groups = function (groups) {
855
901
  // Get persisted groups
856
902
  var existingGroups = this.props.$groups || {};
857
- // NOTE: Should we do the same for groups listed in identify / capture?
858
903
  this.register({
859
904
  $groups: __assign(__assign({}, existingGroups), groups),
860
905
  });
861
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) {
906
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
862
907
  void this.reloadFeatureFlagsAsync();
863
908
  }
864
909
  return this;
@@ -882,58 +927,96 @@ var PostHogCore = /** @class */ (function () {
882
927
  this.enqueue('capture', payload);
883
928
  return this;
884
929
  };
930
+ /***
931
+ * PROPERTIES
932
+ ***/
933
+ PostHogCore.prototype.personProperties = function (properties) {
934
+ // Get persisted person properties
935
+ var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
936
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
937
+ return this;
938
+ };
939
+ PostHogCore.prototype.groupProperties = function (properties) {
940
+ // Get persisted group properties
941
+ var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
942
+ if (Object.keys(existingProperties).length !== 0) {
943
+ Object.keys(existingProperties).forEach(function (groupType) {
944
+ existingProperties[groupType] = __assign(__assign({}, existingProperties[groupType]), properties[groupType]);
945
+ delete properties[groupType];
946
+ });
947
+ }
948
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
949
+ return this;
950
+ };
885
951
  /***
886
952
  *** FEATURE FLAGS
887
953
  ***/
888
- PostHogCore.prototype.decideAsync = function () {
954
+ PostHogCore.prototype.decideAsync = function (sendAnonDistinctId) {
955
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
889
956
  if (this._decideResponsePromise) {
890
957
  return this._decideResponsePromise;
891
958
  }
892
- return this._decideAsync();
959
+ return this._decideAsync(sendAnonDistinctId);
893
960
  };
894
- PostHogCore.prototype._decideAsync = function () {
961
+ PostHogCore.prototype._decideAsync = function (sendAnonDistinctId) {
962
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
895
963
  return __awaiter(this, void 0, void 0, function () {
896
- var url, distinctId, groups, fetchOptions;
964
+ var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
897
965
  var _this = this;
898
966
  return __generator(this, function (_a) {
899
967
  url = "".concat(this.host, "/decide/?v=2");
900
968
  distinctId = this.getDistinctId();
901
969
  groups = this.props.$groups || {};
970
+ personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
971
+ groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
902
972
  fetchOptions = {
903
973
  method: 'POST',
904
974
  headers: { 'Content-Type': 'application/json' },
905
- body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }),
975
+ body: JSON.stringify({
976
+ token: this.apiKey,
977
+ distinct_id: distinctId,
978
+ $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
979
+ groups: groups,
980
+ person_properties: personProperties,
981
+ group_properties: groupProperties,
982
+ }),
906
983
  };
907
984
  this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
908
985
  .then(function (r) { return r.json(); })
909
986
  .then(function (res) {
910
987
  if (res.featureFlags) {
911
988
  _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
989
+ _this._events.emit('featureflags', res.featureFlags);
912
990
  }
913
- _this._events.emit('featureflags', res.featureFlags);
914
991
  return res;
992
+ })
993
+ .finally(function () {
994
+ _this._decideResponsePromise = undefined;
915
995
  });
916
996
  return [2 /*return*/, this._decideResponsePromise];
917
997
  });
918
998
  });
919
999
  };
920
- PostHogCore.prototype.getFeatureFlag = function (key, defaultResult) {
921
- var _a;
922
- if (defaultResult === void 0) { defaultResult = false; }
1000
+ PostHogCore.prototype.getFeatureFlag = function (key) {
923
1001
  var featureFlags = this.getFeatureFlags();
924
1002
  if (!featureFlags) {
925
- // If we haven't loaded flags yet we respond undefined to indicate this
1003
+ // If we haven't loaded flags yet, or errored out, we respond with undefined
926
1004
  return undefined;
927
1005
  }
1006
+ var response = featureFlags[key];
1007
+ if (response === undefined) {
1008
+ // `/decide` returns nothing for flags which are false.
1009
+ response = false;
1010
+ }
928
1011
  if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
929
1012
  this.flagCallReported[key] = true;
930
1013
  this.capture('$feature_flag_called', {
931
1014
  $feature_flag: key,
932
- $feature_flag_response: featureFlags[key],
1015
+ $feature_flag_response: response,
933
1016
  });
934
1017
  }
935
- // If we have flags we either return the value (true or string) or the defaultResult
936
- return (_a = featureFlags[key]) !== null && _a !== void 0 ? _a : defaultResult;
1018
+ // If we have flags we either return the value (true or string) or false
1019
+ return response;
937
1020
  };
938
1021
  PostHogCore.prototype.getFeatureFlags = function () {
939
1022
  var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
@@ -952,35 +1035,26 @@ var PostHogCore = /** @class */ (function () {
952
1035
  }
953
1036
  return flags;
954
1037
  };
955
- PostHogCore.prototype.isFeatureEnabled = function (key, defaultResult) {
956
- var _a;
957
- if (defaultResult === void 0) { defaultResult = false; }
958
- var flag = (_a = this.getFeatureFlag(key, defaultResult)) !== null && _a !== void 0 ? _a : defaultResult;
959
- return !!flag;
1038
+ PostHogCore.prototype.isFeatureEnabled = function (key) {
1039
+ var response = this.getFeatureFlag(key);
1040
+ if (response === undefined) {
1041
+ return undefined;
1042
+ }
1043
+ return !!response;
960
1044
  };
961
- PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
1045
+ PostHogCore.prototype.reloadFeatureFlagsAsync = function (sendAnonDistinctId) {
1046
+ if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
962
1047
  return __awaiter(this, void 0, void 0, function () {
963
- var _this = this;
964
1048
  return __generator(this, function (_a) {
965
1049
  switch (_a.label) {
966
- case 0:
967
- clearTimeout(this._decideTimer);
968
- if (this._decidePollInterval) {
969
- this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
970
- }
971
- this._decideResponsePromise = undefined;
972
- return [4 /*yield*/, this.decideAsync()];
1050
+ case 0: return [4 /*yield*/, this.decideAsync(sendAnonDistinctId)];
973
1051
  case 1: return [2 /*return*/, (_a.sent()).featureFlags];
974
1052
  }
975
1053
  });
976
1054
  });
977
1055
  };
978
- // When listening to feature flags polling is active
979
1056
  PostHogCore.prototype.onFeatureFlags = function (cb) {
980
1057
  var _this = this;
981
- if (!this._decideTimer) {
982
- void this.reloadFeatureFlagsAsync();
983
- }
984
1058
  return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
985
1059
  var flags;
986
1060
  return __generator(this, function (_a) {
@@ -992,12 +1066,33 @@ var PostHogCore = /** @class */ (function () {
992
1066
  });
993
1067
  }); });
994
1068
  };
1069
+ PostHogCore.prototype.onFeatureFlag = function (key, cb) {
1070
+ var _this = this;
1071
+ return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
1072
+ var flagResponse;
1073
+ return __generator(this, function (_a) {
1074
+ flagResponse = this.getFeatureFlag(key);
1075
+ if (flagResponse !== undefined) {
1076
+ cb(flagResponse);
1077
+ }
1078
+ return [2 /*return*/];
1079
+ });
1080
+ }); });
1081
+ };
995
1082
  PostHogCore.prototype.overrideFeatureFlag = function (flags) {
996
1083
  if (flags === null) {
997
1084
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
998
1085
  }
999
1086
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
1000
1087
  };
1088
+ PostHogCore.prototype._sendFeatureFlags = function (event, properties) {
1089
+ var _this = this;
1090
+ this.reloadFeatureFlagsAsync(false).finally(function () {
1091
+ // Try to enqueue message irrespective of errors during feature flag fetching
1092
+ var payload = _this.buildPayload({ event: event, properties: properties });
1093
+ _this.enqueue('capture', payload);
1094
+ });
1095
+ };
1001
1096
  /***
1002
1097
  *** QUEUEING AND FLUSHING
1003
1098
  ***/
@@ -1026,13 +1121,15 @@ var PostHogCore = /** @class */ (function () {
1026
1121
  PostHogCore.prototype.flushAsync = function () {
1027
1122
  var _this = this;
1028
1123
  return new Promise(function (resolve, reject) {
1029
- _this.flush(function (err, data) { return (err ? reject(err) : resolve(data)); });
1124
+ _this.flush(function (err, data) {
1125
+ return err ? reject(err) : resolve(data);
1126
+ });
1030
1127
  });
1031
1128
  };
1032
1129
  PostHogCore.prototype.flush = function (callback) {
1033
1130
  var _this = this;
1034
1131
  if (this.optedOut) {
1035
- return callback && safeSetTimeout(callback, 0);
1132
+ return callback === null || callback === void 0 ? void 0 : callback();
1036
1133
  }
1037
1134
  if (this._flushTimer) {
1038
1135
  clearTimeout(this._flushTimer);
@@ -1040,7 +1137,7 @@ var PostHogCore = /** @class */ (function () {
1040
1137
  }
1041
1138
  var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1042
1139
  if (!queue.length) {
1043
- return callback && safeSetTimeout(callback, 0);
1140
+ return callback === null || callback === void 0 ? void 0 : callback();
1044
1141
  }
1045
1142
  var items = queue.splice(0, this.flushAt);
1046
1143
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
@@ -1099,7 +1196,6 @@ var PostHogCore = /** @class */ (function () {
1099
1196
  return __generator(this, function (_a) {
1100
1197
  switch (_a.label) {
1101
1198
  case 0:
1102
- clearTimeout(this._decideTimer);
1103
1199
  clearTimeout(this._flushTimer);
1104
1200
  return [4 /*yield*/, this.flushAsync()];
1105
1201
  case 1:
@@ -1115,6 +1211,19 @@ var PostHogCore = /** @class */ (function () {
1115
1211
  return PostHogCore;
1116
1212
  }());
1117
1213
 
1214
+ var PostHogMemoryStorage = /** @class */ (function () {
1215
+ function PostHogMemoryStorage() {
1216
+ this._memoryStorage = {};
1217
+ }
1218
+ PostHogMemoryStorage.prototype.getProperty = function (key) {
1219
+ return this._memoryStorage[key];
1220
+ };
1221
+ PostHogMemoryStorage.prototype.setProperty = function (key, value) {
1222
+ this._memoryStorage[key] = value !== null ? value : undefined;
1223
+ };
1224
+ return PostHogMemoryStorage;
1225
+ }());
1226
+
1118
1227
  var getLegacyValues = function () {
1119
1228
  return __awaiter(void 0, void 0, void 0, function () {
1120
1229
  var posthogFileDirectory, posthogDistinctIdFile, posthogAnonymousIdFile, res, _a, _b, _c, _d, _e, _f;
@@ -1299,36 +1408,7 @@ var preloadSemiAsyncStorage = function () {
1299
1408
  return _preloadSemiAsyncStoragePromise;
1300
1409
  };
1301
1410
 
1302
- var _OptionalReactNativeNavigation = undefined;
1303
- var _OptionalExpoLocalization = undefined;
1304
- var _OptionalExpoNetwork = undefined;
1305
-
1306
- var warn = function (name) {
1307
- console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1308
- };
1309
-
1310
- try {
1311
- _OptionalReactNativeNavigation = require('@react-navigation/native');
1312
- } catch (e) {
1313
- warn('@react-navigation/native');
1314
- }
1315
-
1316
- try {
1317
- _OptionalExpoLocalization = require('expo-localization');
1318
- } catch (e) {
1319
- warn('expo-localization');
1320
- }
1321
-
1322
- try {
1323
- _OptionalExpoNetwork = require('expo-network');
1324
- } catch (e) {
1325
- warn('expo-network');
1326
- }
1327
-
1328
- var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1329
- var OptionalExpoLocalization = _OptionalExpoLocalization;
1330
-
1331
- var version = "2.0.0-alpha8";
1411
+ var version = "2.1.0";
1332
1412
 
1333
1413
  var PostHog =
1334
1414
  /** @class */
@@ -1338,16 +1418,19 @@ function (_super) {
1338
1418
  function PostHog(apiKey, options) {
1339
1419
  var _this = _super.call(this, apiKey, options) || this;
1340
1420
 
1421
+ _this._memoryStorage = new PostHogMemoryStorage();
1422
+ _this._persistence = options === null || options === void 0 ? void 0 : options.persistence;
1341
1423
  AppState.addEventListener('change', function () {
1342
1424
  _this.flush();
1343
1425
  }); // Ensure the async storage has been preloaded (this call is cached)
1344
1426
  // It is possible that the old library was used so we try to get the legacy distinctID
1345
1427
 
1346
1428
  void preloadSemiAsyncStorage().then(function () {
1347
- if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.DistinctId)) {
1429
+ if (!SemiAsyncStorage.getItem(PostHogPersistedProperty.AnonymousId)) {
1348
1430
  getLegacyValues().then(function (legacyValues) {
1349
1431
  if (legacyValues === null || legacyValues === void 0 ? void 0 : legacyValues.distinctId) {
1350
1432
  SemiAsyncStorage.setItem(PostHogPersistedProperty.DistinctId, legacyValues.distinctId);
1433
+ SemiAsyncStorage.setItem(PostHogPersistedProperty.AnonymousId, legacyValues.anonymousId);
1351
1434
  }
1352
1435
  });
1353
1436
  }
@@ -1360,10 +1443,18 @@ function (_super) {
1360
1443
  };
1361
1444
 
1362
1445
  PostHog.prototype.getPersistedProperty = function (key) {
1446
+ if (this._persistence === 'memory') {
1447
+ return this._memoryStorage.getProperty(key);
1448
+ }
1449
+
1363
1450
  return SemiAsyncStorage.getItem(key) || undefined;
1364
1451
  };
1365
1452
 
1366
1453
  PostHog.prototype.setPersistedProperty = function (key, value) {
1454
+ if (this._persistence === 'memory') {
1455
+ return this._memoryStorage.getProperty(key);
1456
+ }
1457
+
1367
1458
  return value !== null ? SemiAsyncStorage.setItem(key, value) : SemiAsyncStorage.removeItem(key);
1368
1459
  };
1369
1460
 
@@ -1380,29 +1471,24 @@ function (_super) {
1380
1471
  };
1381
1472
 
1382
1473
  PostHog.prototype.getCustomUserAgent = function () {
1383
- // TODO
1384
1474
  return;
1385
1475
  };
1386
1476
 
1387
1477
  PostHog.prototype.getCommonEventProperties = function () {
1388
1478
  return __assign(__assign({}, _super.prototype.getCommonEventProperties.call(this)), {
1389
1479
  $app_build: '1',
1390
- $app_name: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.applicationName,
1391
- $app_namespace: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.applicationId,
1392
- $app_version: ExpoApplication === null || ExpoApplication === void 0 ? void 0 : ExpoApplication.nativeApplicationVersion,
1393
- // "$device_id": "F31C35E8-5B28-4626-8AFC-213D1C655FF9",
1394
- $device_manufacturer: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.manufacturer,
1395
- // "$device_model": "x86_64",
1396
- $device_name: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.modelName,
1397
- // "$device_type": "ios",
1398
- $locale: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.locale,
1399
- // "$network_cellular": false,
1400
- // "$network_wifi": true,
1401
- $os_name: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.osName,
1402
- $os_version: ExpoDevice === null || ExpoDevice === void 0 ? void 0 : ExpoDevice.osVersion,
1480
+ $app_name: ExpoApplication.applicationName,
1481
+ $app_namespace: ExpoApplication.applicationId,
1482
+ $app_version: ExpoApplication.nativeApplicationVersion,
1483
+ $device_manufacturer: ExpoDevice.manufacturer,
1484
+ $device_name: ExpoDevice.modelName,
1485
+ $device_type: Platform.OS,
1486
+ $locale: ExpoLocalization.locale,
1487
+ $os_name: ExpoDevice.osName,
1488
+ $os_version: ExpoDevice.osVersion,
1403
1489
  $screen_height: Dimensions.get('screen').height,
1404
1490
  $screen_width: Dimensions.get('screen').width,
1405
- $timezone: OptionalExpoLocalization === null || OptionalExpoLocalization === void 0 ? void 0 : OptionalExpoLocalization.timezone
1491
+ $timezone: ExpoLocalization.timezone
1406
1492
  });
1407
1493
  }; // Custom methods
1408
1494
 
@@ -1460,6 +1546,20 @@ function useLifecycleTracker(client) {
1460
1546
  }, [posthog]);
1461
1547
  }
1462
1548
 
1549
+ var _OptionalReactNativeNavigation = undefined;
1550
+
1551
+ var warn = function (name) {
1552
+ console.warn("PostHog: Missing ".concat(name, " optional dependency. Some functions may not work as expected..."));
1553
+ };
1554
+
1555
+ try {
1556
+ _OptionalReactNativeNavigation = require('@react-navigation/native');
1557
+ } catch (e) {
1558
+ warn('@react-navigation/native');
1559
+ }
1560
+
1561
+ var OptionalReactNativeNavigation = _OptionalReactNativeNavigation;
1562
+
1463
1563
  function _useNavigationTrackerDisabled() {
1464
1564
  return;
1465
1565
  }
@@ -1545,10 +1645,10 @@ function useFeatureFlags(client) {
1545
1645
  return featureFlags;
1546
1646
  }
1547
1647
 
1548
- function useFeatureFlag(flag, defaultValue) {
1648
+ function useFeatureFlag(flag) {
1549
1649
  var posthog = usePostHog();
1550
1650
 
1551
- var _a = useState(posthog === null || posthog === void 0 ? void 0 : posthog.getFeatureFlag(flag, defaultValue)),
1651
+ var _a = useState(posthog === null || posthog === void 0 ? void 0 : posthog.getFeatureFlag(flag)),
1552
1652
  featureFlag = _a[0],
1553
1653
  setFeatureFlag = _a[1];
1554
1654
 
@@ -1557,11 +1657,11 @@ function useFeatureFlag(flag, defaultValue) {
1557
1657
  }
1558
1658
 
1559
1659
  useEffect(function () {
1560
- setFeatureFlag(posthog.getFeatureFlag(flag, defaultValue));
1660
+ setFeatureFlag(posthog.getFeatureFlag(flag));
1561
1661
  return posthog.onFeatureFlags(function () {
1562
- setFeatureFlag(posthog.getFeatureFlag(flag, defaultValue));
1662
+ setFeatureFlag(posthog.getFeatureFlag(flag));
1563
1663
  });
1564
- }, [posthog, flag, defaultValue]);
1664
+ }, [posthog, flag]);
1565
1665
  return featureFlag;
1566
1666
  }
1567
1667
 
@@ -1606,7 +1706,9 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1606
1706
  _e = options.maxElementsCaptured,
1607
1707
  maxElementsCaptured = _e === void 0 ? 20 : _e,
1608
1708
  _f = options.ignoreLabels,
1609
- ignoreLabels = _f === void 0 ? [] : _f;
1709
+ ignoreLabels = _f === void 0 ? [] : _f,
1710
+ _g = options.propsToCapture,
1711
+ propsToCapture = _g === void 0 ? ['style', 'testID', 'accessibilityLabel', 'ph-label'] : _g;
1610
1712
 
1611
1713
  if (!e._targetInst) {
1612
1714
  return;
@@ -1621,8 +1723,19 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1621
1723
  };
1622
1724
  var props = currentInst.memoizedProps;
1623
1725
 
1726
+ if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1727
+ return {
1728
+ value: void 0
1729
+ };
1730
+ }
1731
+
1624
1732
  if (props) {
1733
+ // Capture only props we have said to capture. By default this is only "safe" props
1625
1734
  Object.keys(props).forEach(function (key) {
1735
+ if (!propsToCapture.includes(key)) {
1736
+ return;
1737
+ }
1738
+
1626
1739
  var value = props[key];
1627
1740
 
1628
1741
  if (key === 'style') {
@@ -1635,12 +1748,6 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1635
1748
  }
1636
1749
  }
1637
1750
  });
1638
- }
1639
-
1640
- if (props === null || props === void 0 ? void 0 : props[noCaptureProp]) {
1641
- return {
1642
- value: void 0
1643
- };
1644
1751
  } // Try and find a sensible label
1645
1752
 
1646
1753
 
@@ -1669,14 +1776,20 @@ var autocaptureFromTouchEvent = function (e, posthog, options) {
1669
1776
  }
1670
1777
  };
1671
1778
 
1672
- function PostHogHooks(_a) {
1779
+ function PostHogNavigationHook(_a) {
1673
1780
  var options = _a.options;
1674
1781
  useNavigationTracker(options === null || options === void 0 ? void 0 : options.navigation);
1782
+ return null;
1783
+ }
1784
+
1785
+ function PostHogLifecycleHook() {
1675
1786
  useLifecycleTracker();
1676
1787
  return null;
1677
1788
  }
1678
1789
 
1679
1790
  var PostHogProvider = function (_a) {
1791
+ var _b, _c;
1792
+
1680
1793
  var children = _a.children,
1681
1794
  client = _a.client,
1682
1795
  options = _a.options,
@@ -1689,10 +1802,13 @@ var PostHogProvider = function (_a) {
1689
1802
  posthogRef.current = client ? client : apiKey ? new PostHog(apiKey, options) : undefined;
1690
1803
  }
1691
1804
 
1692
- var autocaptureEnabled = !!autocapture;
1693
1805
  var autocaptureOptions = autocapture && typeof autocapture !== 'boolean' ? autocapture : {};
1694
1806
  var posthog = posthogRef.current;
1695
1807
  var captureTouches = posthog && (autocapture === true || (autocaptureOptions === null || autocaptureOptions === void 0 ? void 0 : autocaptureOptions.captureTouches));
1808
+ 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
1809
+
1810
+ 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
1811
+
1696
1812
  var onTouch = useCallback(function (type, e) {
1697
1813
  // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1698
1814
  if (!captureTouches) {
@@ -1702,8 +1818,7 @@ var PostHogProvider = function (_a) {
1702
1818
  if (type === 'end') {
1703
1819
  autocaptureFromTouchEvent(e, posthog, autocaptureOptions);
1704
1820
  }
1705
- }, [posthog, autocapture]); // TODO: Improve this to ensure we only capture presses and not just ends of a drag for example
1706
-
1821
+ }, [posthog, autocapture]);
1707
1822
  return /*#__PURE__*/React.createElement(View, {
1708
1823
  "ph-label": "PostHogProvider",
1709
1824
  style: style || {
@@ -1716,9 +1831,9 @@ var PostHogProvider = function (_a) {
1716
1831
  value: {
1717
1832
  client: posthogRef.current
1718
1833
  }
1719
- }, autocaptureEnabled ? /*#__PURE__*/React.createElement(PostHogHooks, {
1834
+ }, /*#__PURE__*/React.createElement(React.Fragment, null, captureScreens ? /*#__PURE__*/React.createElement(PostHogNavigationHook, {
1720
1835
  options: autocaptureOptions
1721
- }) : null, children));
1836
+ }) : null, captureLifecycle ? /*#__PURE__*/React.createElement(PostHogLifecycleHook, null) : null), children));
1722
1837
  };
1723
1838
 
1724
1839
  export { PostHog, PostHogProvider, PostHog as default, useFeatureFlag, useFeatureFlags, useLifecycleTracker, useNavigationTracker, usePostHog };