posthog-js-lite 2.0.0-alpha5 → 2.0.1
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 +3 -0
- package/README.md +68 -0
- package/lib/index.cjs.js +269 -85
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +55 -12
- package/lib/index.esm.js +269 -85
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +33 -9
- package/lib/posthog-core/src/types.d.ts +26 -3
- package/lib/posthog-core/src/utils.d.ts +0 -1
- package/lib/posthog-web/src/storage.d.ts +1 -5
- package/package.json +2 -2
- package/src/posthog-web.ts +5 -1
- package/src/storage.ts +18 -9
package/lib/index.esm.js
CHANGED
|
@@ -127,18 +127,33 @@ function __generator(thisArg, body) {
|
|
|
127
127
|
throw op[1];
|
|
128
128
|
return { value: op[0] ? op[1] : void 0, done: true };
|
|
129
129
|
}
|
|
130
|
+
}
|
|
131
|
+
function __spreadArray(to, from, pack) {
|
|
132
|
+
if (pack || arguments.length === 2)
|
|
133
|
+
for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
134
|
+
if (ar || !(i in from)) {
|
|
135
|
+
if (!ar)
|
|
136
|
+
ar = Array.prototype.slice.call(from, 0, i);
|
|
137
|
+
ar[i] = from[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
130
141
|
}
|
|
131
142
|
|
|
132
143
|
var PostHogPersistedProperty;
|
|
133
144
|
(function (PostHogPersistedProperty) {
|
|
145
|
+
PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
|
|
134
146
|
PostHogPersistedProperty["DistinctId"] = "distinct_id";
|
|
135
147
|
PostHogPersistedProperty["Props"] = "props";
|
|
136
148
|
PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
|
|
149
|
+
PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
|
|
137
150
|
PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
|
|
138
151
|
PostHogPersistedProperty["Queue"] = "queue";
|
|
139
152
|
PostHogPersistedProperty["OptedOut"] = "opted_out";
|
|
140
153
|
PostHogPersistedProperty["SessionId"] = "session_id";
|
|
141
154
|
PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
|
|
155
|
+
PostHogPersistedProperty["PersonProperties"] = "person_properties";
|
|
156
|
+
PostHogPersistedProperty["GroupProperties"] = "group_properties";
|
|
142
157
|
})(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
|
|
143
158
|
|
|
144
159
|
function assert(truthyValue, message) {
|
|
@@ -696,13 +711,13 @@ var PostHogCore = /** @class */ (function () {
|
|
|
696
711
|
this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
|
|
697
712
|
this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
|
|
698
713
|
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
699
|
-
this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
|
|
700
714
|
// If enable is explicitly set to false we override the optout
|
|
701
715
|
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
|
|
702
716
|
this._retryOptions = {
|
|
703
|
-
retryCount: (
|
|
704
|
-
retryDelay: (
|
|
717
|
+
retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
|
|
718
|
+
retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
|
|
705
719
|
};
|
|
720
|
+
this.requestTimeout = (_e = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _e !== void 0 ? _e : 10000; // 10 seconds
|
|
706
721
|
this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
|
|
707
722
|
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
708
723
|
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
@@ -713,12 +728,35 @@ var PostHogCore = /** @class */ (function () {
|
|
|
713
728
|
}
|
|
714
729
|
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
715
730
|
var featureFlags = this.getFeatureFlags();
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
731
|
+
var featureVariantProperties = {};
|
|
732
|
+
if (featureFlags) {
|
|
733
|
+
for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
|
|
734
|
+
var _b = _a[_i], feature = _b[0], variant = _b[1];
|
|
735
|
+
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
|
|
739
|
+
};
|
|
740
|
+
PostHogCore.prototype.setupBootstrap = function (options) {
|
|
741
|
+
var _a, _b, _c, _d;
|
|
742
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _a === void 0 ? void 0 : _a.distinctId) {
|
|
743
|
+
if ((_b = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _b === void 0 ? void 0 : _b.isIdentifiedId) {
|
|
744
|
+
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, options.bootstrap.distinctId);
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, options.bootstrap.distinctId);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if ((_c = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _c === void 0 ? void 0 : _c.featureFlags) {
|
|
751
|
+
var activeFlags = Object.keys(((_d = options.bootstrap) === null || _d === void 0 ? void 0 : _d.featureFlags) || {})
|
|
752
|
+
.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]); })
|
|
753
|
+
.reduce(function (res, key) {
|
|
754
|
+
var _a, _b;
|
|
755
|
+
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);
|
|
756
|
+
}, {});
|
|
757
|
+
this.setKnownFeatureFlags(activeFlags);
|
|
758
|
+
(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads) && this.setKnownFeatureFlagPayloads(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads);
|
|
759
|
+
}
|
|
722
760
|
};
|
|
723
761
|
Object.defineProperty(PostHogCore.prototype, "props", {
|
|
724
762
|
// NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
|
|
@@ -734,6 +772,9 @@ var PostHogCore = /** @class */ (function () {
|
|
|
734
772
|
enumerable: false,
|
|
735
773
|
configurable: true
|
|
736
774
|
});
|
|
775
|
+
PostHogCore.prototype.clearProps = function () {
|
|
776
|
+
this.props = undefined;
|
|
777
|
+
};
|
|
737
778
|
Object.defineProperty(PostHogCore.prototype, "optedOut", {
|
|
738
779
|
get: function () {
|
|
739
780
|
var _a, _b;
|
|
@@ -751,11 +792,16 @@ var PostHogCore = /** @class */ (function () {
|
|
|
751
792
|
PostHogCore.prototype.on = function (event, cb) {
|
|
752
793
|
return this._events.on(event, cb);
|
|
753
794
|
};
|
|
754
|
-
PostHogCore.prototype.reset = function () {
|
|
755
|
-
|
|
756
|
-
|
|
795
|
+
PostHogCore.prototype.reset = function (propertiesToKeep) {
|
|
796
|
+
var allPropertiesToKeep = __spreadArray([PostHogPersistedProperty.Queue], (propertiesToKeep || []), true);
|
|
797
|
+
// clean up props
|
|
798
|
+
this.clearProps();
|
|
799
|
+
for (var _i = 0, _a = Object.keys(PostHogPersistedProperty); _i < _a.length; _i++) {
|
|
800
|
+
var key = _a[_i];
|
|
801
|
+
if (!allPropertiesToKeep.includes(PostHogPersistedProperty[key])) {
|
|
802
|
+
this.setPersistedProperty(PostHogPersistedProperty[key], null);
|
|
803
|
+
}
|
|
757
804
|
}
|
|
758
|
-
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
|
|
759
805
|
};
|
|
760
806
|
PostHogCore.prototype.debug = function (enabled) {
|
|
761
807
|
var _a;
|
|
@@ -785,13 +831,16 @@ var PostHogCore = /** @class */ (function () {
|
|
|
785
831
|
PostHogCore.prototype.resetSessionId = function () {
|
|
786
832
|
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
|
|
787
833
|
};
|
|
788
|
-
PostHogCore.prototype.
|
|
789
|
-
var
|
|
790
|
-
if (!
|
|
791
|
-
|
|
792
|
-
this.setPersistedProperty(PostHogPersistedProperty.
|
|
834
|
+
PostHogCore.prototype.getAnonymousId = function () {
|
|
835
|
+
var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
|
|
836
|
+
if (!anonId) {
|
|
837
|
+
anonId = generateUUID(globalThis);
|
|
838
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
|
|
793
839
|
}
|
|
794
|
-
return
|
|
840
|
+
return anonId;
|
|
841
|
+
};
|
|
842
|
+
PostHogCore.prototype.getDistinctId = function () {
|
|
843
|
+
return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
|
|
795
844
|
};
|
|
796
845
|
PostHogCore.prototype.register = function (properties) {
|
|
797
846
|
this.props = __assign(__assign({}, this.props), properties);
|
|
@@ -805,32 +854,39 @@ var PostHogCore = /** @class */ (function () {
|
|
|
805
854
|
*** TRACKING
|
|
806
855
|
***/
|
|
807
856
|
PostHogCore.prototype.identify = function (distinctId, properties) {
|
|
808
|
-
|
|
857
|
+
var previousDistinctId = this.getDistinctId();
|
|
858
|
+
distinctId = distinctId || previousDistinctId;
|
|
809
859
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
810
860
|
this.groups(properties.$groups);
|
|
811
861
|
}
|
|
812
862
|
var payload = __assign(__assign({}, this.buildPayload({
|
|
813
863
|
distinct_id: distinctId,
|
|
814
864
|
event: '$identify',
|
|
815
|
-
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.
|
|
865
|
+
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
|
|
816
866
|
})), { $set: properties });
|
|
817
|
-
if (distinctId !==
|
|
867
|
+
if (distinctId !== previousDistinctId) {
|
|
868
|
+
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
869
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
818
870
|
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
871
|
+
if (this.getFeatureFlags()) {
|
|
872
|
+
void this.reloadFeatureFlagsAsync();
|
|
873
|
+
}
|
|
819
874
|
}
|
|
820
875
|
this.enqueue('identify', payload);
|
|
821
876
|
return this;
|
|
822
877
|
};
|
|
823
|
-
PostHogCore.prototype.capture = function (event, properties) {
|
|
824
|
-
|
|
825
|
-
if (properties && properties['groups']) {
|
|
826
|
-
properties.$groups = properties.groups;
|
|
827
|
-
delete properties.groups;
|
|
828
|
-
}
|
|
878
|
+
PostHogCore.prototype.capture = function (event, properties, forceSendFeatureFlags) {
|
|
879
|
+
if (forceSendFeatureFlags === void 0) { forceSendFeatureFlags = false; }
|
|
829
880
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
830
881
|
this.groups(properties.$groups);
|
|
831
882
|
}
|
|
832
|
-
|
|
833
|
-
|
|
883
|
+
if (forceSendFeatureFlags) {
|
|
884
|
+
this._sendFeatureFlags(event, properties);
|
|
885
|
+
}
|
|
886
|
+
else {
|
|
887
|
+
var payload = this.buildPayload({ event: event, properties: properties });
|
|
888
|
+
this.enqueue('capture', payload);
|
|
889
|
+
}
|
|
834
890
|
return this;
|
|
835
891
|
};
|
|
836
892
|
PostHogCore.prototype.alias = function (alias) {
|
|
@@ -860,11 +916,10 @@ var PostHogCore = /** @class */ (function () {
|
|
|
860
916
|
PostHogCore.prototype.groups = function (groups) {
|
|
861
917
|
// Get persisted groups
|
|
862
918
|
var existingGroups = this.props.$groups || {};
|
|
863
|
-
// NOTE: Should we do the same for groups listed in identify / capture?
|
|
864
919
|
this.register({
|
|
865
920
|
$groups: __assign(__assign({}, existingGroups), groups),
|
|
866
921
|
});
|
|
867
|
-
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.
|
|
922
|
+
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
|
|
868
923
|
void this.reloadFeatureFlagsAsync();
|
|
869
924
|
}
|
|
870
925
|
return this;
|
|
@@ -880,66 +935,151 @@ var PostHogCore = /** @class */ (function () {
|
|
|
880
935
|
return this;
|
|
881
936
|
};
|
|
882
937
|
PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties) {
|
|
883
|
-
var payload = {
|
|
938
|
+
var payload = this.buildPayload({
|
|
884
939
|
event: '$groupidentify',
|
|
885
|
-
distinctId: "$".concat(groupType, "_").concat(groupKey),
|
|
886
940
|
properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, this.getCommonEventProperties()),
|
|
887
|
-
};
|
|
941
|
+
});
|
|
888
942
|
this.enqueue('capture', payload);
|
|
889
943
|
return this;
|
|
890
944
|
};
|
|
945
|
+
/***
|
|
946
|
+
* PROPERTIES
|
|
947
|
+
***/
|
|
948
|
+
PostHogCore.prototype.personProperties = function (properties) {
|
|
949
|
+
// Get persisted person properties
|
|
950
|
+
var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
951
|
+
this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
|
|
952
|
+
return this;
|
|
953
|
+
};
|
|
954
|
+
PostHogCore.prototype.groupProperties = function (properties) {
|
|
955
|
+
// Get persisted group properties
|
|
956
|
+
var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
957
|
+
if (Object.keys(existingProperties).length !== 0) {
|
|
958
|
+
Object.keys(existingProperties).forEach(function (groupType) {
|
|
959
|
+
existingProperties[groupType] = __assign(__assign({}, existingProperties[groupType]), properties[groupType]);
|
|
960
|
+
delete properties[groupType];
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
|
|
964
|
+
return this;
|
|
965
|
+
};
|
|
891
966
|
/***
|
|
892
967
|
*** FEATURE FLAGS
|
|
893
968
|
***/
|
|
894
|
-
PostHogCore.prototype.decideAsync = function () {
|
|
969
|
+
PostHogCore.prototype.decideAsync = function (sendAnonDistinctId) {
|
|
970
|
+
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
895
971
|
if (this._decideResponsePromise) {
|
|
896
972
|
return this._decideResponsePromise;
|
|
897
973
|
}
|
|
898
|
-
return this._decideAsync();
|
|
974
|
+
return this._decideAsync(sendAnonDistinctId);
|
|
899
975
|
};
|
|
900
|
-
PostHogCore.prototype._decideAsync = function () {
|
|
976
|
+
PostHogCore.prototype._decideAsync = function (sendAnonDistinctId) {
|
|
977
|
+
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
901
978
|
return __awaiter(this, void 0, void 0, function () {
|
|
902
|
-
var url, distinctId, groups, fetchOptions;
|
|
979
|
+
var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
|
|
903
980
|
var _this = this;
|
|
904
981
|
return __generator(this, function (_a) {
|
|
905
|
-
url = "".concat(this.host, "/decide/?v=
|
|
982
|
+
url = "".concat(this.host, "/decide/?v=3");
|
|
906
983
|
distinctId = this.getDistinctId();
|
|
907
984
|
groups = this.props.$groups || {};
|
|
985
|
+
personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
986
|
+
groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
908
987
|
fetchOptions = {
|
|
909
988
|
method: 'POST',
|
|
910
989
|
headers: { 'Content-Type': 'application/json' },
|
|
911
|
-
body: JSON.stringify({
|
|
990
|
+
body: JSON.stringify({
|
|
991
|
+
token: this.apiKey,
|
|
992
|
+
distinct_id: distinctId,
|
|
993
|
+
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
|
|
994
|
+
groups: groups,
|
|
995
|
+
person_properties: personProperties,
|
|
996
|
+
group_properties: groupProperties,
|
|
997
|
+
}),
|
|
912
998
|
};
|
|
913
999
|
this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
|
|
914
1000
|
.then(function (r) { return r.json(); })
|
|
915
1001
|
.then(function (res) {
|
|
916
1002
|
if (res.featureFlags) {
|
|
917
|
-
|
|
1003
|
+
var newFeatureFlags = res.featureFlags;
|
|
1004
|
+
var newFeatureFlagPayloads = res.featureFlagPayloads;
|
|
1005
|
+
if (res.errorsWhileComputingFlags) {
|
|
1006
|
+
// if not all flags were computed, we upsert flags instead of replacing them
|
|
1007
|
+
var currentFlags = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
1008
|
+
var currentFlagPayloads = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
|
|
1009
|
+
newFeatureFlags = __assign(__assign({}, currentFlags), res.featureFlags);
|
|
1010
|
+
newFeatureFlagPayloads = __assign(__assign({}, currentFlagPayloads), res.featureFlagPayloads);
|
|
1011
|
+
}
|
|
1012
|
+
_this.setKnownFeatureFlags(newFeatureFlags);
|
|
1013
|
+
_this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads);
|
|
918
1014
|
}
|
|
919
|
-
_this._events.emit('featureflags', res.featureFlags);
|
|
920
1015
|
return res;
|
|
1016
|
+
})
|
|
1017
|
+
.finally(function () {
|
|
1018
|
+
_this._decideResponsePromise = undefined;
|
|
921
1019
|
});
|
|
922
1020
|
return [2 /*return*/, this._decideResponsePromise];
|
|
923
1021
|
});
|
|
924
1022
|
});
|
|
925
1023
|
};
|
|
926
|
-
PostHogCore.prototype.
|
|
927
|
-
|
|
928
|
-
|
|
1024
|
+
PostHogCore.prototype.setKnownFeatureFlags = function (featureFlags) {
|
|
1025
|
+
this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
|
|
1026
|
+
this._events.emit('featureflags', featureFlags);
|
|
1027
|
+
};
|
|
1028
|
+
PostHogCore.prototype.setKnownFeatureFlagPayloads = function (featureFlagPayloads) {
|
|
1029
|
+
this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads, featureFlagPayloads);
|
|
1030
|
+
};
|
|
1031
|
+
PostHogCore.prototype.getFeatureFlag = function (key) {
|
|
929
1032
|
var featureFlags = this.getFeatureFlags();
|
|
930
1033
|
if (!featureFlags) {
|
|
931
|
-
// If we haven't loaded flags yet we respond undefined
|
|
1034
|
+
// If we haven't loaded flags yet, or errored out, we respond with undefined
|
|
932
1035
|
return undefined;
|
|
933
1036
|
}
|
|
1037
|
+
var response = featureFlags[key];
|
|
1038
|
+
// `/decide` v3 returns all flags
|
|
1039
|
+
if (response === undefined) {
|
|
1040
|
+
// For cases where the flag is unknown, return false
|
|
1041
|
+
response = false;
|
|
1042
|
+
}
|
|
934
1043
|
if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
|
|
935
1044
|
this.flagCallReported[key] = true;
|
|
936
1045
|
this.capture('$feature_flag_called', {
|
|
937
1046
|
$feature_flag: key,
|
|
938
|
-
$feature_flag_response:
|
|
1047
|
+
$feature_flag_response: response,
|
|
939
1048
|
});
|
|
940
1049
|
}
|
|
941
|
-
// If we have flags we either return the value (true or string) or
|
|
942
|
-
return
|
|
1050
|
+
// If we have flags we either return the value (true or string) or false
|
|
1051
|
+
return response;
|
|
1052
|
+
};
|
|
1053
|
+
PostHogCore.prototype.getFeatureFlagPayload = function (key) {
|
|
1054
|
+
var payloads = this.getFeatureFlagPayloads();
|
|
1055
|
+
if (!payloads) {
|
|
1056
|
+
return undefined;
|
|
1057
|
+
}
|
|
1058
|
+
var response = payloads[key];
|
|
1059
|
+
// Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
|
|
1060
|
+
if (response === undefined) {
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
return this._parsePayload(response);
|
|
1064
|
+
};
|
|
1065
|
+
PostHogCore.prototype.getFeatureFlagPayloads = function () {
|
|
1066
|
+
var _this = this;
|
|
1067
|
+
var payloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
|
|
1068
|
+
if (payloads) {
|
|
1069
|
+
return Object.fromEntries(Object.entries(payloads).map(function (_a) {
|
|
1070
|
+
var k = _a[0], v = _a[1];
|
|
1071
|
+
return [k, _this._parsePayload(v)];
|
|
1072
|
+
}));
|
|
1073
|
+
}
|
|
1074
|
+
return payloads;
|
|
1075
|
+
};
|
|
1076
|
+
PostHogCore.prototype._parsePayload = function (response) {
|
|
1077
|
+
try {
|
|
1078
|
+
return JSON.parse(response);
|
|
1079
|
+
}
|
|
1080
|
+
catch (_a) {
|
|
1081
|
+
return response;
|
|
1082
|
+
}
|
|
943
1083
|
};
|
|
944
1084
|
PostHogCore.prototype.getFeatureFlags = function () {
|
|
945
1085
|
var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
@@ -958,35 +1098,34 @@ var PostHogCore = /** @class */ (function () {
|
|
|
958
1098
|
}
|
|
959
1099
|
return flags;
|
|
960
1100
|
};
|
|
961
|
-
PostHogCore.prototype.
|
|
962
|
-
var
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
1101
|
+
PostHogCore.prototype.getFeatureFlagsAndPayloads = function () {
|
|
1102
|
+
var flags = this.getFeatureFlags();
|
|
1103
|
+
var payloads = this.getFeatureFlagPayloads();
|
|
1104
|
+
return {
|
|
1105
|
+
flags: flags,
|
|
1106
|
+
payloads: payloads,
|
|
1107
|
+
};
|
|
966
1108
|
};
|
|
967
|
-
PostHogCore.prototype.
|
|
1109
|
+
PostHogCore.prototype.isFeatureEnabled = function (key) {
|
|
1110
|
+
var response = this.getFeatureFlag(key);
|
|
1111
|
+
if (response === undefined) {
|
|
1112
|
+
return undefined;
|
|
1113
|
+
}
|
|
1114
|
+
return !!response;
|
|
1115
|
+
};
|
|
1116
|
+
PostHogCore.prototype.reloadFeatureFlagsAsync = function (sendAnonDistinctId) {
|
|
1117
|
+
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
968
1118
|
return __awaiter(this, void 0, void 0, function () {
|
|
969
|
-
var _this = this;
|
|
970
1119
|
return __generator(this, function (_a) {
|
|
971
1120
|
switch (_a.label) {
|
|
972
|
-
case 0:
|
|
973
|
-
clearTimeout(this._decideTimer);
|
|
974
|
-
if (this._decidePollInterval) {
|
|
975
|
-
this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
|
|
976
|
-
}
|
|
977
|
-
this._decideResponsePromise = undefined;
|
|
978
|
-
return [4 /*yield*/, this.decideAsync()];
|
|
1121
|
+
case 0: return [4 /*yield*/, this.decideAsync(sendAnonDistinctId)];
|
|
979
1122
|
case 1: return [2 /*return*/, (_a.sent()).featureFlags];
|
|
980
1123
|
}
|
|
981
1124
|
});
|
|
982
1125
|
});
|
|
983
1126
|
};
|
|
984
|
-
// When listening to feature flags polling is active
|
|
985
1127
|
PostHogCore.prototype.onFeatureFlags = function (cb) {
|
|
986
1128
|
var _this = this;
|
|
987
|
-
if (!this._decideTimer) {
|
|
988
|
-
void this.reloadFeatureFlagsAsync();
|
|
989
|
-
}
|
|
990
1129
|
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
991
1130
|
var flags;
|
|
992
1131
|
return __generator(this, function (_a) {
|
|
@@ -998,12 +1137,33 @@ var PostHogCore = /** @class */ (function () {
|
|
|
998
1137
|
});
|
|
999
1138
|
}); });
|
|
1000
1139
|
};
|
|
1140
|
+
PostHogCore.prototype.onFeatureFlag = function (key, cb) {
|
|
1141
|
+
var _this = this;
|
|
1142
|
+
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1143
|
+
var flagResponse;
|
|
1144
|
+
return __generator(this, function (_a) {
|
|
1145
|
+
flagResponse = this.getFeatureFlag(key);
|
|
1146
|
+
if (flagResponse !== undefined) {
|
|
1147
|
+
cb(flagResponse);
|
|
1148
|
+
}
|
|
1149
|
+
return [2 /*return*/];
|
|
1150
|
+
});
|
|
1151
|
+
}); });
|
|
1152
|
+
};
|
|
1001
1153
|
PostHogCore.prototype.overrideFeatureFlag = function (flags) {
|
|
1002
1154
|
if (flags === null) {
|
|
1003
1155
|
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
|
|
1004
1156
|
}
|
|
1005
1157
|
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
|
|
1006
1158
|
};
|
|
1159
|
+
PostHogCore.prototype._sendFeatureFlags = function (event, properties) {
|
|
1160
|
+
var _this = this;
|
|
1161
|
+
this.reloadFeatureFlagsAsync(false).finally(function () {
|
|
1162
|
+
// Try to enqueue message irrespective of errors during feature flag fetching
|
|
1163
|
+
var payload = _this.buildPayload({ event: event, properties: properties });
|
|
1164
|
+
_this.enqueue('capture', payload);
|
|
1165
|
+
});
|
|
1166
|
+
};
|
|
1007
1167
|
/***
|
|
1008
1168
|
*** QUEUEING AND FLUSHING
|
|
1009
1169
|
***/
|
|
@@ -1032,13 +1192,15 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1032
1192
|
PostHogCore.prototype.flushAsync = function () {
|
|
1033
1193
|
var _this = this;
|
|
1034
1194
|
return new Promise(function (resolve, reject) {
|
|
1035
|
-
_this.flush(function (err, data) {
|
|
1195
|
+
_this.flush(function (err, data) {
|
|
1196
|
+
return err ? reject(err) : resolve(data);
|
|
1197
|
+
});
|
|
1036
1198
|
});
|
|
1037
1199
|
};
|
|
1038
1200
|
PostHogCore.prototype.flush = function (callback) {
|
|
1039
1201
|
var _this = this;
|
|
1040
1202
|
if (this.optedOut) {
|
|
1041
|
-
return callback
|
|
1203
|
+
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1042
1204
|
}
|
|
1043
1205
|
if (this._flushTimer) {
|
|
1044
1206
|
clearTimeout(this._flushTimer);
|
|
@@ -1046,7 +1208,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1046
1208
|
}
|
|
1047
1209
|
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1048
1210
|
if (!queue.length) {
|
|
1049
|
-
return callback
|
|
1211
|
+
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1050
1212
|
}
|
|
1051
1213
|
var items = queue.splice(0, this.flushAt);
|
|
1052
1214
|
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
@@ -1093,10 +1255,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1093
1255
|
});
|
|
1094
1256
|
};
|
|
1095
1257
|
PostHogCore.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1258
|
+
var _a;
|
|
1259
|
+
var _b;
|
|
1096
1260
|
return __awaiter(this, void 0, void 0, function () {
|
|
1097
1261
|
var _this = this;
|
|
1098
|
-
return __generator(this, function (
|
|
1099
|
-
|
|
1262
|
+
return __generator(this, function (_c) {
|
|
1263
|
+
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1264
|
+
var ctrl = new AbortController();
|
|
1265
|
+
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1266
|
+
return ctrl.signal;
|
|
1267
|
+
});
|
|
1268
|
+
return [2 /*return*/, retriable(function () {
|
|
1269
|
+
return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
|
|
1270
|
+
}, retryOptions || this._retryOptions)];
|
|
1100
1271
|
});
|
|
1101
1272
|
});
|
|
1102
1273
|
};
|
|
@@ -1105,7 +1276,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1105
1276
|
return __generator(this, function (_a) {
|
|
1106
1277
|
switch (_a.label) {
|
|
1107
1278
|
case 0:
|
|
1108
|
-
clearTimeout(this._decideTimer);
|
|
1109
1279
|
clearTimeout(this._flushTimer);
|
|
1110
1280
|
return [4 /*yield*/, this.flushAsync()];
|
|
1111
1281
|
case 1:
|
|
@@ -1121,7 +1291,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1121
1291
|
return PostHogCore;
|
|
1122
1292
|
}());
|
|
1123
1293
|
|
|
1124
|
-
var version = "2.0.
|
|
1294
|
+
var version = "2.0.1";
|
|
1125
1295
|
|
|
1126
1296
|
function getContext(window) {
|
|
1127
1297
|
var context = {};
|
|
@@ -1382,9 +1552,6 @@ var createStorageLike = function (store) {
|
|
|
1382
1552
|
};
|
|
1383
1553
|
};
|
|
1384
1554
|
|
|
1385
|
-
var _localStore = createStorageLike(window.localStorage);
|
|
1386
|
-
var _sessionStore = createStorageLike(window.sessionStorage);
|
|
1387
|
-
|
|
1388
1555
|
var checkStoreIsSupported = function (storage, key) {
|
|
1389
1556
|
if (key === void 0) {
|
|
1390
1557
|
key = '__mplssupport__';
|
|
@@ -1409,8 +1576,8 @@ var checkStoreIsSupported = function (storage, key) {
|
|
|
1409
1576
|
}
|
|
1410
1577
|
};
|
|
1411
1578
|
|
|
1412
|
-
var localStore =
|
|
1413
|
-
var
|
|
1579
|
+
var localStore = undefined;
|
|
1580
|
+
var sessionStore = undefined;
|
|
1414
1581
|
|
|
1415
1582
|
var createMemoryStorage = function () {
|
|
1416
1583
|
var _cache = {};
|
|
@@ -1442,16 +1609,30 @@ var createMemoryStorage = function () {
|
|
|
1442
1609
|
return store;
|
|
1443
1610
|
};
|
|
1444
1611
|
|
|
1445
|
-
var getStorage = function (type) {
|
|
1612
|
+
var getStorage = function (type, window) {
|
|
1613
|
+
if (window) {
|
|
1614
|
+
if (!localStorage) {
|
|
1615
|
+
var _localStore = createStorageLike(window.localStorage);
|
|
1616
|
+
|
|
1617
|
+
localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
if (!sessionStore) {
|
|
1621
|
+
var _sessionStore = createStorageLike(window.sessionStorage);
|
|
1622
|
+
|
|
1623
|
+
sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1446
1627
|
switch (type) {
|
|
1447
1628
|
case 'cookie':
|
|
1448
|
-
return cookieStore || localStore ||
|
|
1629
|
+
return cookieStore || localStore || sessionStore || createMemoryStorage();
|
|
1449
1630
|
|
|
1450
1631
|
case 'localStorage':
|
|
1451
|
-
return localStore ||
|
|
1632
|
+
return localStore || sessionStore || createMemoryStorage();
|
|
1452
1633
|
|
|
1453
1634
|
case 'sessionStorage':
|
|
1454
|
-
return
|
|
1635
|
+
return sessionStore || createMemoryStorage();
|
|
1455
1636
|
|
|
1456
1637
|
case 'memory':
|
|
1457
1638
|
return createMemoryStorage();
|
|
@@ -1471,7 +1652,10 @@ function (_super) {
|
|
|
1471
1652
|
|
|
1472
1653
|
|
|
1473
1654
|
_this._storageKey = (options === null || options === void 0 ? void 0 : options.persistence_name) ? "ph_".concat(options.persistence_name) : "ph_".concat(apiKey, "_posthog");
|
|
1474
|
-
_this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage');
|
|
1655
|
+
_this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage', window);
|
|
1656
|
+
|
|
1657
|
+
_this.setupBootstrap(options);
|
|
1658
|
+
|
|
1475
1659
|
return _this;
|
|
1476
1660
|
}
|
|
1477
1661
|
|