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