posthog-node 2.4.0 → 2.5.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 +8 -1
- package/lib/index.cjs.js +488 -369
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +109 -5
- package/lib/index.esm.js +488 -369
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +56 -22
- package/lib/posthog-node/src/posthog-node.d.ts +10 -6
- package/package.json +2 -2
- package/src/posthog-node.ts +57 -114
- package/test/posthog-node.spec.ts +58 -1
- package/lib/node_modules/tslib/tslib.es6.d.ts +0 -35
package/lib/index.esm.js
CHANGED
|
@@ -155,7 +155,7 @@ function __spreadArray(to, from, pack) {
|
|
|
155
155
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
var version = "2.
|
|
158
|
+
var version = "2.5.1";
|
|
159
159
|
|
|
160
160
|
var PostHogPersistedProperty;
|
|
161
161
|
(function (PostHogPersistedProperty) {
|
|
@@ -714,11 +714,10 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
714
714
|
return SimpleEventEmitter;
|
|
715
715
|
}());
|
|
716
716
|
|
|
717
|
-
var
|
|
718
|
-
function
|
|
719
|
-
var
|
|
720
|
-
|
|
721
|
-
this.flagCallReported = {};
|
|
717
|
+
var PostHogCoreStateless = /** @class */ (function () {
|
|
718
|
+
function PostHogCoreStateless(apiKey, options) {
|
|
719
|
+
var _a, _b, _c, _d;
|
|
720
|
+
this.pendingPromises = {};
|
|
722
721
|
// internal
|
|
723
722
|
this._events = new SimpleEventEmitter();
|
|
724
723
|
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
@@ -727,33 +726,383 @@ var PostHogCore = /** @class */ (function () {
|
|
|
727
726
|
this.flushAt = (options === null || options === void 0 ? void 0 : options.flushAt) ? Math.max(options === null || options === void 0 ? void 0 : options.flushAt, 1) : 20;
|
|
728
727
|
this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
|
|
729
728
|
this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
|
|
730
|
-
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
731
729
|
// If enable is explicitly set to false we override the optout
|
|
732
730
|
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
|
|
733
731
|
this._retryOptions = {
|
|
734
|
-
retryCount: (
|
|
735
|
-
retryDelay: (
|
|
732
|
+
retryCount: (_b = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _b !== void 0 ? _b : 3,
|
|
733
|
+
retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
|
|
734
|
+
};
|
|
735
|
+
this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
|
|
736
|
+
}
|
|
737
|
+
PostHogCoreStateless.prototype.getCommonEventProperties = function () {
|
|
738
|
+
return {
|
|
739
|
+
$lib: this.getLibraryId(),
|
|
740
|
+
$lib_version: this.getLibraryVersion(),
|
|
741
|
+
};
|
|
742
|
+
};
|
|
743
|
+
Object.defineProperty(PostHogCoreStateless.prototype, "optedOut", {
|
|
744
|
+
get: function () {
|
|
745
|
+
var _a, _b;
|
|
746
|
+
return (_b = (_a = this.getPersistedProperty(PostHogPersistedProperty.OptedOut)) !== null && _a !== void 0 ? _a : this._optoutOverride) !== null && _b !== void 0 ? _b : false;
|
|
747
|
+
},
|
|
748
|
+
enumerable: false,
|
|
749
|
+
configurable: true
|
|
750
|
+
});
|
|
751
|
+
PostHogCoreStateless.prototype.optIn = function () {
|
|
752
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
753
|
+
};
|
|
754
|
+
PostHogCoreStateless.prototype.optOut = function () {
|
|
755
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
756
|
+
};
|
|
757
|
+
PostHogCoreStateless.prototype.on = function (event, cb) {
|
|
758
|
+
return this._events.on(event, cb);
|
|
759
|
+
};
|
|
760
|
+
PostHogCoreStateless.prototype.debug = function (enabled) {
|
|
761
|
+
var _a;
|
|
762
|
+
if (enabled === void 0) { enabled = true; }
|
|
763
|
+
(_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
764
|
+
if (enabled) {
|
|
765
|
+
this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
PostHogCoreStateless.prototype.buildPayload = function (payload) {
|
|
769
|
+
return {
|
|
770
|
+
distinct_id: payload.distinct_id,
|
|
771
|
+
event: payload.event,
|
|
772
|
+
properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
|
|
773
|
+
};
|
|
774
|
+
};
|
|
775
|
+
/***
|
|
776
|
+
*** TRACKING
|
|
777
|
+
***/
|
|
778
|
+
PostHogCoreStateless.prototype.identifyStateless = function (distinctId, properties, options) {
|
|
779
|
+
// The properties passed to identifyStateless are event properties.
|
|
780
|
+
// To add person properties, pass in all person properties to the `$set` key.
|
|
781
|
+
var payload = __assign({}, this.buildPayload({
|
|
782
|
+
distinct_id: distinctId,
|
|
783
|
+
event: '$identify',
|
|
784
|
+
properties: properties,
|
|
785
|
+
}));
|
|
786
|
+
this.enqueue('identify', payload, options);
|
|
787
|
+
return this;
|
|
788
|
+
};
|
|
789
|
+
PostHogCoreStateless.prototype.captureStateless = function (distinctId, event, properties, options) {
|
|
790
|
+
var payload = this.buildPayload({ distinct_id: distinctId, event: event, properties: properties });
|
|
791
|
+
this.enqueue('capture', payload, options);
|
|
792
|
+
return this;
|
|
793
|
+
};
|
|
794
|
+
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties) {
|
|
795
|
+
var payload = this.buildPayload({
|
|
796
|
+
event: '$create_alias',
|
|
797
|
+
distinct_id: distinctId,
|
|
798
|
+
properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
|
|
799
|
+
});
|
|
800
|
+
this.enqueue('alias', payload);
|
|
801
|
+
return this;
|
|
802
|
+
};
|
|
803
|
+
/***
|
|
804
|
+
*** GROUPS
|
|
805
|
+
***/
|
|
806
|
+
PostHogCoreStateless.prototype.groupIdentifyStateless = function (groupType, groupKey, groupProperties, options, distinctId, eventProperties) {
|
|
807
|
+
var payload = this.buildPayload({
|
|
808
|
+
distinct_id: distinctId || "$".concat(groupType, "_").concat(groupKey),
|
|
809
|
+
event: '$groupidentify',
|
|
810
|
+
properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, (eventProperties || {})),
|
|
811
|
+
});
|
|
812
|
+
this.enqueue('capture', payload, options);
|
|
813
|
+
return this;
|
|
814
|
+
};
|
|
815
|
+
/***
|
|
816
|
+
*** FEATURE FLAGS
|
|
817
|
+
***/
|
|
818
|
+
PostHogCoreStateless.prototype.getDecide = function (distinctId, groups, personProperties, groupProperties, extraPayload) {
|
|
819
|
+
if (groups === void 0) { groups = {}; }
|
|
820
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
821
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
822
|
+
if (extraPayload === void 0) { extraPayload = {}; }
|
|
823
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
824
|
+
var url, fetchOptions;
|
|
825
|
+
return __generator(this, function (_a) {
|
|
826
|
+
url = "".concat(this.host, "/decide/?v=3");
|
|
827
|
+
fetchOptions = {
|
|
828
|
+
method: 'POST',
|
|
829
|
+
headers: { 'Content-Type': 'application/json' },
|
|
830
|
+
body: JSON.stringify(__assign({ token: this.apiKey, distinct_id: distinctId, groups: groups, person_properties: personProperties, group_properties: groupProperties }, extraPayload)),
|
|
831
|
+
};
|
|
832
|
+
return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
|
|
833
|
+
.then(function (response) { return response.json(); })
|
|
834
|
+
.catch(function (error) {
|
|
835
|
+
console.error('Error fetching feature flags', error);
|
|
836
|
+
return undefined;
|
|
837
|
+
})];
|
|
838
|
+
});
|
|
839
|
+
});
|
|
840
|
+
};
|
|
841
|
+
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
842
|
+
if (groups === void 0) { groups = {}; }
|
|
843
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
844
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
845
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
846
|
+
var featureFlags, response;
|
|
847
|
+
return __generator(this, function (_a) {
|
|
848
|
+
switch (_a.label) {
|
|
849
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
850
|
+
case 1:
|
|
851
|
+
featureFlags = _a.sent();
|
|
852
|
+
if (!featureFlags) {
|
|
853
|
+
// If we haven't loaded flags yet, or errored out, we respond with undefined
|
|
854
|
+
return [2 /*return*/, undefined];
|
|
855
|
+
}
|
|
856
|
+
response = featureFlags[key];
|
|
857
|
+
// `/decide` v3 returns all flags
|
|
858
|
+
if (response === undefined) {
|
|
859
|
+
// For cases where the flag is unknown, return false
|
|
860
|
+
response = false;
|
|
861
|
+
}
|
|
862
|
+
// If we have flags we either return the value (true or string) or false
|
|
863
|
+
return [2 /*return*/, response];
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
};
|
|
868
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
869
|
+
if (groups === void 0) { groups = {}; }
|
|
870
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
871
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
872
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
873
|
+
var payloads, response;
|
|
874
|
+
return __generator(this, function (_a) {
|
|
875
|
+
switch (_a.label) {
|
|
876
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
877
|
+
case 1:
|
|
878
|
+
payloads = _a.sent();
|
|
879
|
+
if (!payloads) {
|
|
880
|
+
return [2 /*return*/, undefined];
|
|
881
|
+
}
|
|
882
|
+
response = payloads[key];
|
|
883
|
+
// Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
|
|
884
|
+
if (response === undefined) {
|
|
885
|
+
return [2 /*return*/, null];
|
|
886
|
+
}
|
|
887
|
+
return [2 /*return*/, this._parsePayload(response)];
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
});
|
|
891
|
+
};
|
|
892
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
893
|
+
if (groups === void 0) { groups = {}; }
|
|
894
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
895
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
896
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
897
|
+
var payloads;
|
|
898
|
+
var _this = this;
|
|
899
|
+
return __generator(this, function (_a) {
|
|
900
|
+
switch (_a.label) {
|
|
901
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
902
|
+
case 1:
|
|
903
|
+
payloads = (_a.sent()).payloads;
|
|
904
|
+
if (payloads) {
|
|
905
|
+
return [2 /*return*/, Object.fromEntries(Object.entries(payloads).map(function (_a) {
|
|
906
|
+
var k = _a[0], v = _a[1];
|
|
907
|
+
return [k, _this._parsePayload(v)];
|
|
908
|
+
}))];
|
|
909
|
+
}
|
|
910
|
+
return [2 /*return*/, payloads];
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
});
|
|
914
|
+
};
|
|
915
|
+
PostHogCoreStateless.prototype._parsePayload = function (response) {
|
|
916
|
+
try {
|
|
917
|
+
return JSON.parse(response);
|
|
918
|
+
}
|
|
919
|
+
catch (_a) {
|
|
920
|
+
return response;
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
924
|
+
if (groups === void 0) { groups = {}; }
|
|
925
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
926
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
927
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
928
|
+
return __generator(this, function (_a) {
|
|
929
|
+
switch (_a.label) {
|
|
930
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
931
|
+
case 1: return [2 /*return*/, (_a.sent()).flags];
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
});
|
|
935
|
+
};
|
|
936
|
+
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
937
|
+
if (groups === void 0) { groups = {}; }
|
|
938
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
939
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
940
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
941
|
+
var decideResponse, flags, payloads;
|
|
942
|
+
return __generator(this, function (_a) {
|
|
943
|
+
switch (_a.label) {
|
|
944
|
+
case 0: return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties)];
|
|
945
|
+
case 1:
|
|
946
|
+
decideResponse = _a.sent();
|
|
947
|
+
flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
|
|
948
|
+
payloads = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlagPayloads;
|
|
949
|
+
return [2 /*return*/, {
|
|
950
|
+
flags: flags,
|
|
951
|
+
payloads: payloads,
|
|
952
|
+
}];
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
};
|
|
957
|
+
/***
|
|
958
|
+
*** QUEUEING AND FLUSHING
|
|
959
|
+
***/
|
|
960
|
+
PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
|
|
961
|
+
var _this = this;
|
|
962
|
+
if (this.optedOut) {
|
|
963
|
+
this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
|
|
967
|
+
if (message.distinctId) {
|
|
968
|
+
message.distinct_id = message.distinctId;
|
|
969
|
+
delete message.distinctId;
|
|
970
|
+
}
|
|
971
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
972
|
+
queue.push({ message: message });
|
|
973
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
974
|
+
this._events.emit(type, message);
|
|
975
|
+
// Flush queued events if we meet the flushAt length
|
|
976
|
+
if (queue.length >= this.flushAt) {
|
|
977
|
+
this.flush();
|
|
978
|
+
}
|
|
979
|
+
if (this.flushInterval && !this._flushTimer) {
|
|
980
|
+
this._flushTimer = safeSetTimeout(function () { return _this.flush(); }, this.flushInterval);
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
PostHogCoreStateless.prototype.flushAsync = function () {
|
|
984
|
+
var _this = this;
|
|
985
|
+
return new Promise(function (resolve, reject) {
|
|
986
|
+
_this.flush(function (err, data) {
|
|
987
|
+
return err ? reject(err) : resolve(data);
|
|
988
|
+
});
|
|
989
|
+
});
|
|
990
|
+
};
|
|
991
|
+
PostHogCoreStateless.prototype.flush = function (callback) {
|
|
992
|
+
var _this = this;
|
|
993
|
+
if (this._flushTimer) {
|
|
994
|
+
clearTimeout(this._flushTimer);
|
|
995
|
+
this._flushTimer = null;
|
|
996
|
+
}
|
|
997
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
998
|
+
if (!queue.length) {
|
|
999
|
+
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1000
|
+
}
|
|
1001
|
+
var items = queue.splice(0, this.flushAt);
|
|
1002
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1003
|
+
var messages = items.map(function (item) { return item.message; });
|
|
1004
|
+
var data = {
|
|
1005
|
+
api_key: this.apiKey,
|
|
1006
|
+
batch: messages,
|
|
1007
|
+
sent_at: currentISOTime(),
|
|
736
1008
|
};
|
|
737
|
-
|
|
738
|
-
|
|
1009
|
+
var promiseUUID = generateUUID();
|
|
1010
|
+
var done = function (err) {
|
|
1011
|
+
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1012
|
+
// remove promise from pendingPromises
|
|
1013
|
+
delete _this.pendingPromises[promiseUUID];
|
|
1014
|
+
_this._events.emit('flush', messages);
|
|
1015
|
+
};
|
|
1016
|
+
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1017
|
+
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1018
|
+
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1019
|
+
// but browsers such as Chrome and Safari have not caught up.
|
|
1020
|
+
this.getCustomUserAgent();
|
|
1021
|
+
var payload = JSON.stringify(data);
|
|
1022
|
+
var url = this.captureMode === 'form'
|
|
1023
|
+
? "".concat(this.host, "/e/?ip=1&_=").concat(currentTimestamp(), "&v=").concat(this.getLibraryVersion())
|
|
1024
|
+
: "".concat(this.host, "/batch/");
|
|
1025
|
+
var fetchOptions = this.captureMode === 'form'
|
|
1026
|
+
? {
|
|
1027
|
+
method: 'POST',
|
|
1028
|
+
mode: 'no-cors',
|
|
1029
|
+
credentials: 'omit',
|
|
1030
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1031
|
+
body: "data=".concat(encodeURIComponent(LZString.compressToBase64(payload)), "&compression=lz64"),
|
|
1032
|
+
}
|
|
1033
|
+
: {
|
|
1034
|
+
method: 'POST',
|
|
1035
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1036
|
+
body: payload,
|
|
1037
|
+
};
|
|
1038
|
+
var requestPromise = this.fetchWithRetry(url, fetchOptions);
|
|
1039
|
+
this.pendingPromises[promiseUUID] = requestPromise;
|
|
1040
|
+
requestPromise
|
|
1041
|
+
.then(function () { return done(); })
|
|
1042
|
+
.catch(function (err) {
|
|
1043
|
+
if (err.response) {
|
|
1044
|
+
var error = new Error(err.response.statusText);
|
|
1045
|
+
return done(error);
|
|
1046
|
+
}
|
|
1047
|
+
done(err);
|
|
1048
|
+
});
|
|
1049
|
+
};
|
|
1050
|
+
PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1051
|
+
var _a;
|
|
1052
|
+
var _b;
|
|
1053
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1054
|
+
var _this = this;
|
|
1055
|
+
return __generator(this, function (_c) {
|
|
1056
|
+
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1057
|
+
var ctrl = new AbortController();
|
|
1058
|
+
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1059
|
+
return ctrl.signal;
|
|
1060
|
+
});
|
|
1061
|
+
return [2 /*return*/, retriable(function () {
|
|
1062
|
+
return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
|
|
1063
|
+
}, retryOptions || this._retryOptions)];
|
|
1064
|
+
});
|
|
1065
|
+
});
|
|
1066
|
+
};
|
|
1067
|
+
PostHogCoreStateless.prototype.shutdownAsync = function () {
|
|
1068
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1069
|
+
return __generator(this, function (_a) {
|
|
1070
|
+
switch (_a.label) {
|
|
1071
|
+
case 0:
|
|
1072
|
+
clearTimeout(this._flushTimer);
|
|
1073
|
+
return [4 /*yield*/, this.flushAsync()];
|
|
1074
|
+
case 1:
|
|
1075
|
+
_a.sent();
|
|
1076
|
+
return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
|
|
1077
|
+
case 2:
|
|
1078
|
+
_a.sent();
|
|
1079
|
+
return [2 /*return*/];
|
|
1080
|
+
}
|
|
1081
|
+
});
|
|
1082
|
+
});
|
|
1083
|
+
};
|
|
1084
|
+
PostHogCoreStateless.prototype.shutdown = function () {
|
|
1085
|
+
void this.shutdownAsync();
|
|
1086
|
+
};
|
|
1087
|
+
return PostHogCoreStateless;
|
|
1088
|
+
}());
|
|
1089
|
+
/** @class */ ((function (_super) {
|
|
1090
|
+
__extends(PostHogCore, _super);
|
|
1091
|
+
function PostHogCore(apiKey, options) {
|
|
1092
|
+
var _this = this;
|
|
1093
|
+
var _a, _b;
|
|
1094
|
+
_this = _super.call(this, apiKey, options) || this;
|
|
1095
|
+
_this.flagCallReported = {};
|
|
1096
|
+
_this.sendFeatureFlagEvent = (_a = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _a !== void 0 ? _a : true;
|
|
1097
|
+
_this._sessionExpirationTimeSeconds = (_b = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _b !== void 0 ? _b : 1800; // 30 minutes
|
|
739
1098
|
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
740
1099
|
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
741
1100
|
safeSetTimeout(function () {
|
|
742
|
-
|
|
1101
|
+
_this.reloadFeatureFlags();
|
|
743
1102
|
}, 1);
|
|
744
1103
|
}
|
|
1104
|
+
return _this;
|
|
745
1105
|
}
|
|
746
|
-
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
747
|
-
var featureFlags = this.getFeatureFlags();
|
|
748
|
-
var featureVariantProperties = {};
|
|
749
|
-
if (featureFlags) {
|
|
750
|
-
for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
|
|
751
|
-
var _b = _a[_i], feature = _b[0], variant = _b[1];
|
|
752
|
-
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
|
|
756
|
-
};
|
|
757
1106
|
PostHogCore.prototype.setupBootstrap = function (options) {
|
|
758
1107
|
var _a, _b, _c, _d;
|
|
759
1108
|
if ((_a = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _a === void 0 ? void 0 : _a.distinctId) {
|
|
@@ -790,21 +1139,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
790
1139
|
configurable: true
|
|
791
1140
|
});
|
|
792
1141
|
PostHogCore.prototype.clearProps = function () {
|
|
793
|
-
this.props = undefined;
|
|
794
|
-
};
|
|
795
|
-
Object.defineProperty(PostHogCore.prototype, "optedOut", {
|
|
796
|
-
get: function () {
|
|
797
|
-
var _a, _b;
|
|
798
|
-
return (_b = (_a = this.getPersistedProperty(PostHogPersistedProperty.OptedOut)) !== null && _a !== void 0 ? _a : this._optoutOverride) !== null && _b !== void 0 ? _b : false;
|
|
799
|
-
},
|
|
800
|
-
enumerable: false,
|
|
801
|
-
configurable: true
|
|
802
|
-
});
|
|
803
|
-
PostHogCore.prototype.optIn = function () {
|
|
804
|
-
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
805
|
-
};
|
|
806
|
-
PostHogCore.prototype.optOut = function () {
|
|
807
|
-
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
1142
|
+
this.props = undefined;
|
|
808
1143
|
};
|
|
809
1144
|
PostHogCore.prototype.on = function (event, cb) {
|
|
810
1145
|
return this._events.on(event, cb);
|
|
@@ -820,20 +1155,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
820
1155
|
}
|
|
821
1156
|
}
|
|
822
1157
|
};
|
|
823
|
-
PostHogCore.prototype.
|
|
824
|
-
var
|
|
825
|
-
|
|
826
|
-
(
|
|
827
|
-
|
|
828
|
-
|
|
1158
|
+
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
1159
|
+
var featureFlags = this.getFeatureFlags();
|
|
1160
|
+
var featureVariantProperties = {};
|
|
1161
|
+
if (featureFlags) {
|
|
1162
|
+
for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
|
|
1163
|
+
var _b = _a[_i], feature = _b[0], variant = _b[1];
|
|
1164
|
+
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
1165
|
+
}
|
|
829
1166
|
}
|
|
1167
|
+
return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
|
|
830
1168
|
};
|
|
831
|
-
PostHogCore.prototype.
|
|
832
|
-
return {
|
|
833
|
-
distinct_id: payload.distinct_id || this.getDistinctId(),
|
|
834
|
-
event: payload.event,
|
|
835
|
-
properties: __assign(__assign(__assign(__assign({}, this.props), (payload.properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() }),
|
|
836
|
-
};
|
|
1169
|
+
PostHogCore.prototype.enrichProperties = function (properties) {
|
|
1170
|
+
return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
|
|
837
1171
|
};
|
|
838
1172
|
PostHogCore.prototype.getSessionId = function () {
|
|
839
1173
|
var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
|
|
@@ -876,48 +1210,41 @@ var PostHogCore = /** @class */ (function () {
|
|
|
876
1210
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
877
1211
|
this.groups(properties.$groups);
|
|
878
1212
|
}
|
|
879
|
-
var
|
|
880
|
-
distinct_id: distinctId,
|
|
881
|
-
event: '$identify',
|
|
882
|
-
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
|
|
883
|
-
})), { $set: properties });
|
|
1213
|
+
var allProperties = this.enrichProperties(__assign(__assign({}, properties), { $anon_distinct_id: this.getAnonymousId(), $set: properties }));
|
|
884
1214
|
if (distinctId !== previousDistinctId) {
|
|
885
1215
|
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
886
1216
|
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
887
1217
|
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
888
1218
|
if (this.getFeatureFlags()) {
|
|
889
|
-
|
|
1219
|
+
this.reloadFeatureFlags();
|
|
890
1220
|
}
|
|
891
1221
|
}
|
|
892
|
-
|
|
1222
|
+
_super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
|
|
893
1223
|
return this;
|
|
894
1224
|
};
|
|
895
1225
|
PostHogCore.prototype.capture = function (event, properties, options) {
|
|
1226
|
+
var distinctId = this.getDistinctId();
|
|
896
1227
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
897
1228
|
this.groups(properties.$groups);
|
|
898
1229
|
}
|
|
899
|
-
var
|
|
900
|
-
|
|
1230
|
+
var allProperties = this.enrichProperties(properties);
|
|
1231
|
+
_super.prototype.captureStateless.call(this, distinctId, event, allProperties, options);
|
|
901
1232
|
return this;
|
|
902
1233
|
};
|
|
903
1234
|
PostHogCore.prototype.alias = function (alias) {
|
|
904
1235
|
var distinctId = this.getDistinctId();
|
|
905
|
-
var
|
|
906
|
-
|
|
907
|
-
properties: {
|
|
908
|
-
distinct_id: distinctId,
|
|
909
|
-
alias: alias,
|
|
910
|
-
},
|
|
911
|
-
});
|
|
912
|
-
this.enqueue('alias', payload);
|
|
1236
|
+
var allProperties = this.enrichProperties({});
|
|
1237
|
+
_super.prototype.aliasStateless.call(this, alias, distinctId, allProperties);
|
|
913
1238
|
return this;
|
|
914
1239
|
};
|
|
915
1240
|
PostHogCore.prototype.autocapture = function (eventType, elements, properties, options) {
|
|
916
1241
|
if (properties === void 0) { properties = {}; }
|
|
917
|
-
var
|
|
1242
|
+
var distinctId = this.getDistinctId();
|
|
1243
|
+
var payload = {
|
|
1244
|
+
distinct_id: distinctId,
|
|
918
1245
|
event: '$autocapture',
|
|
919
|
-
properties: __assign(__assign({}, properties), { $event_type: eventType, $elements: elements }),
|
|
920
|
-
}
|
|
1246
|
+
properties: __assign(__assign({}, this.enrichProperties(properties)), { $event_type: eventType, $elements: elements }),
|
|
1247
|
+
};
|
|
921
1248
|
this.enqueue('autocapture', payload, options);
|
|
922
1249
|
return this;
|
|
923
1250
|
};
|
|
@@ -931,7 +1258,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
931
1258
|
$groups: __assign(__assign({}, existingGroups), groups),
|
|
932
1259
|
});
|
|
933
1260
|
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
|
|
934
|
-
|
|
1261
|
+
this.reloadFeatureFlags();
|
|
935
1262
|
}
|
|
936
1263
|
return this;
|
|
937
1264
|
};
|
|
@@ -946,11 +1273,9 @@ var PostHogCore = /** @class */ (function () {
|
|
|
946
1273
|
return this;
|
|
947
1274
|
};
|
|
948
1275
|
PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties, options) {
|
|
949
|
-
var
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
});
|
|
953
|
-
this.enqueue('capture', payload, options);
|
|
1276
|
+
var distinctId = this.getDistinctId();
|
|
1277
|
+
var eventProperties = this.enrichProperties({});
|
|
1278
|
+
_super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, groupProperties, options, distinctId, eventProperties);
|
|
954
1279
|
return this;
|
|
955
1280
|
};
|
|
956
1281
|
/***
|
|
@@ -987,30 +1312,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
987
1312
|
PostHogCore.prototype._decideAsync = function (sendAnonDistinctId) {
|
|
988
1313
|
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
989
1314
|
return __awaiter(this, void 0, void 0, function () {
|
|
990
|
-
var
|
|
1315
|
+
var distinctId, groups, personProperties, groupProperties, extraProperties;
|
|
991
1316
|
var _this = this;
|
|
992
1317
|
return __generator(this, function (_a) {
|
|
993
|
-
url = "".concat(this.host, "/decide/?v=3");
|
|
994
1318
|
distinctId = this.getDistinctId();
|
|
995
1319
|
groups = this.props.$groups || {};
|
|
996
1320
|
personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
997
1321
|
groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1001
|
-
body: JSON.stringify({
|
|
1002
|
-
token: this.apiKey,
|
|
1003
|
-
distinct_id: distinctId,
|
|
1004
|
-
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
|
|
1005
|
-
groups: groups,
|
|
1006
|
-
person_properties: personProperties,
|
|
1007
|
-
group_properties: groupProperties,
|
|
1008
|
-
}),
|
|
1322
|
+
extraProperties = {
|
|
1323
|
+
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
|
|
1009
1324
|
};
|
|
1010
|
-
this._decideResponsePromise =
|
|
1011
|
-
.then(function (r) { return r.json(); })
|
|
1325
|
+
this._decideResponsePromise = _super.prototype.getDecide.call(this, distinctId, groups, personProperties, groupProperties, extraProperties)
|
|
1012
1326
|
.then(function (res) {
|
|
1013
|
-
if (res.featureFlags) {
|
|
1327
|
+
if (res === null || res === void 0 ? void 0 : res.featureFlags) {
|
|
1014
1328
|
var newFeatureFlags = res.featureFlags;
|
|
1015
1329
|
var newFeatureFlagPayloads = res.featureFlagPayloads;
|
|
1016
1330
|
if (res.errorsWhileComputingFlags) {
|
|
@@ -1084,14 +1398,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1084
1398
|
}
|
|
1085
1399
|
return payloads;
|
|
1086
1400
|
};
|
|
1087
|
-
PostHogCore.prototype._parsePayload = function (response) {
|
|
1088
|
-
try {
|
|
1089
|
-
return JSON.parse(response);
|
|
1090
|
-
}
|
|
1091
|
-
catch (_a) {
|
|
1092
|
-
return response;
|
|
1093
|
-
}
|
|
1094
|
-
};
|
|
1095
1401
|
PostHogCore.prototype.getFeatureFlags = function () {
|
|
1096
1402
|
var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
1097
1403
|
var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
|
|
@@ -1124,13 +1430,27 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1124
1430
|
}
|
|
1125
1431
|
return !!response;
|
|
1126
1432
|
};
|
|
1433
|
+
// Used when we want to trigger the reload but we don't care about the result
|
|
1434
|
+
PostHogCore.prototype.reloadFeatureFlags = function (cb) {
|
|
1435
|
+
this.decideAsync()
|
|
1436
|
+
.then(function (res) {
|
|
1437
|
+
cb === null || cb === void 0 ? void 0 : cb(undefined, res === null || res === void 0 ? void 0 : res.featureFlags);
|
|
1438
|
+
})
|
|
1439
|
+
.catch(function (e) {
|
|
1440
|
+
cb === null || cb === void 0 ? void 0 : cb(e, undefined);
|
|
1441
|
+
if (!cb) {
|
|
1442
|
+
console.log('[PostHog] Error reloading feature flags', e);
|
|
1443
|
+
}
|
|
1444
|
+
});
|
|
1445
|
+
};
|
|
1127
1446
|
PostHogCore.prototype.reloadFeatureFlagsAsync = function (sendAnonDistinctId) {
|
|
1447
|
+
var _a;
|
|
1128
1448
|
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
1129
1449
|
return __awaiter(this, void 0, void 0, function () {
|
|
1130
|
-
return __generator(this, function (
|
|
1131
|
-
switch (
|
|
1450
|
+
return __generator(this, function (_b) {
|
|
1451
|
+
switch (_b.label) {
|
|
1132
1452
|
case 0: return [4 /*yield*/, this.decideAsync(sendAnonDistinctId)];
|
|
1133
|
-
case 1: return [2 /*return*/, (_a.sent()).featureFlags];
|
|
1453
|
+
case 1: return [2 /*return*/, (_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.featureFlags];
|
|
1134
1454
|
}
|
|
1135
1455
|
});
|
|
1136
1456
|
});
|
|
@@ -1167,132 +1487,8 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1167
1487
|
}
|
|
1168
1488
|
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
|
|
1169
1489
|
};
|
|
1170
|
-
/***
|
|
1171
|
-
*** QUEUEING AND FLUSHING
|
|
1172
|
-
***/
|
|
1173
|
-
PostHogCore.prototype.enqueue = function (type, _message, options) {
|
|
1174
|
-
var _this = this;
|
|
1175
|
-
if (this.optedOut) {
|
|
1176
|
-
return;
|
|
1177
|
-
}
|
|
1178
|
-
var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
|
|
1179
|
-
if (message.distinctId) {
|
|
1180
|
-
message.distinct_id = message.distinctId;
|
|
1181
|
-
delete message.distinctId;
|
|
1182
|
-
}
|
|
1183
|
-
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1184
|
-
queue.push({ message: message });
|
|
1185
|
-
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1186
|
-
this._events.emit(type, message);
|
|
1187
|
-
// Flush queued events if we meet the flushAt length
|
|
1188
|
-
if (queue.length >= this.flushAt) {
|
|
1189
|
-
this.flush();
|
|
1190
|
-
}
|
|
1191
|
-
if (this.flushInterval && !this._flushTimer) {
|
|
1192
|
-
this._flushTimer = safeSetTimeout(function () { return _this.flush(); }, this.flushInterval);
|
|
1193
|
-
}
|
|
1194
|
-
};
|
|
1195
|
-
PostHogCore.prototype.flushAsync = function () {
|
|
1196
|
-
var _this = this;
|
|
1197
|
-
return new Promise(function (resolve, reject) {
|
|
1198
|
-
_this.flush(function (err, data) {
|
|
1199
|
-
return err ? reject(err) : resolve(data);
|
|
1200
|
-
});
|
|
1201
|
-
});
|
|
1202
|
-
};
|
|
1203
|
-
PostHogCore.prototype.flush = function (callback) {
|
|
1204
|
-
var _this = this;
|
|
1205
|
-
if (this.optedOut) {
|
|
1206
|
-
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1207
|
-
}
|
|
1208
|
-
if (this._flushTimer) {
|
|
1209
|
-
clearTimeout(this._flushTimer);
|
|
1210
|
-
this._flushTimer = null;
|
|
1211
|
-
}
|
|
1212
|
-
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1213
|
-
if (!queue.length) {
|
|
1214
|
-
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1215
|
-
}
|
|
1216
|
-
var items = queue.splice(0, this.flushAt);
|
|
1217
|
-
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1218
|
-
var messages = items.map(function (item) { return item.message; });
|
|
1219
|
-
var data = {
|
|
1220
|
-
api_key: this.apiKey,
|
|
1221
|
-
batch: messages,
|
|
1222
|
-
sent_at: currentISOTime(),
|
|
1223
|
-
};
|
|
1224
|
-
var done = function (err) {
|
|
1225
|
-
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1226
|
-
_this._events.emit('flush', messages);
|
|
1227
|
-
};
|
|
1228
|
-
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1229
|
-
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1230
|
-
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1231
|
-
// but browsers such as Chrome and Safari have not caught up.
|
|
1232
|
-
this.getCustomUserAgent();
|
|
1233
|
-
var payload = JSON.stringify(data);
|
|
1234
|
-
var url = this.captureMode === 'form'
|
|
1235
|
-
? "".concat(this.host, "/e/?ip=1&_=").concat(currentTimestamp(), "&v=").concat(this.getLibraryVersion())
|
|
1236
|
-
: "".concat(this.host, "/batch/");
|
|
1237
|
-
var fetchOptions = this.captureMode === 'form'
|
|
1238
|
-
? {
|
|
1239
|
-
method: 'POST',
|
|
1240
|
-
mode: 'no-cors',
|
|
1241
|
-
credentials: 'omit',
|
|
1242
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1243
|
-
body: "data=".concat(encodeURIComponent(LZString.compressToBase64(payload)), "&compression=lz64"),
|
|
1244
|
-
}
|
|
1245
|
-
: {
|
|
1246
|
-
method: 'POST',
|
|
1247
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1248
|
-
body: payload,
|
|
1249
|
-
};
|
|
1250
|
-
this.fetchWithRetry(url, fetchOptions)
|
|
1251
|
-
.then(function () { return done(); })
|
|
1252
|
-
.catch(function (err) {
|
|
1253
|
-
if (err.response) {
|
|
1254
|
-
var error = new Error(err.response.statusText);
|
|
1255
|
-
return done(error);
|
|
1256
|
-
}
|
|
1257
|
-
done(err);
|
|
1258
|
-
});
|
|
1259
|
-
};
|
|
1260
|
-
PostHogCore.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1261
|
-
var _a;
|
|
1262
|
-
var _b;
|
|
1263
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1264
|
-
var _this = this;
|
|
1265
|
-
return __generator(this, function (_c) {
|
|
1266
|
-
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1267
|
-
var ctrl = new AbortController();
|
|
1268
|
-
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1269
|
-
return ctrl.signal;
|
|
1270
|
-
});
|
|
1271
|
-
return [2 /*return*/, retriable(function () {
|
|
1272
|
-
return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
|
|
1273
|
-
}, retryOptions || this._retryOptions)];
|
|
1274
|
-
});
|
|
1275
|
-
});
|
|
1276
|
-
};
|
|
1277
|
-
PostHogCore.prototype.shutdownAsync = function () {
|
|
1278
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1279
|
-
return __generator(this, function (_a) {
|
|
1280
|
-
switch (_a.label) {
|
|
1281
|
-
case 0:
|
|
1282
|
-
clearTimeout(this._flushTimer);
|
|
1283
|
-
return [4 /*yield*/, this.flushAsync()];
|
|
1284
|
-
case 1:
|
|
1285
|
-
_a.sent();
|
|
1286
|
-
return [2 /*return*/];
|
|
1287
|
-
}
|
|
1288
|
-
});
|
|
1289
|
-
});
|
|
1290
|
-
};
|
|
1291
|
-
PostHogCore.prototype.shutdown = function () {
|
|
1292
|
-
void this.shutdownAsync();
|
|
1293
|
-
};
|
|
1294
1490
|
return PostHogCore;
|
|
1295
|
-
}());
|
|
1491
|
+
})(PostHogCoreStateless));
|
|
1296
1492
|
|
|
1297
1493
|
var PostHogMemoryStorage = /** @class */ (function () {
|
|
1298
1494
|
function PostHogMemoryStorage() {
|
|
@@ -2057,104 +2253,73 @@ function convertToDateTime(value) {
|
|
|
2057
2253
|
}
|
|
2058
2254
|
|
|
2059
2255
|
var THIRTY_SECONDS = 30 * 1000;
|
|
2060
|
-
var MAX_CACHE_SIZE = 50 * 1000;
|
|
2256
|
+
var MAX_CACHE_SIZE = 50 * 1000; // The actual exported Nodejs API.
|
|
2061
2257
|
|
|
2062
|
-
var
|
|
2258
|
+
var PostHog =
|
|
2063
2259
|
/** @class */
|
|
2064
2260
|
function (_super) {
|
|
2065
|
-
__extends(
|
|
2261
|
+
__extends(PostHog, _super);
|
|
2066
2262
|
|
|
2067
|
-
function
|
|
2263
|
+
function PostHog(apiKey, options) {
|
|
2068
2264
|
if (options === void 0) {
|
|
2069
2265
|
options = {};
|
|
2070
2266
|
}
|
|
2071
2267
|
|
|
2072
2268
|
var _this = this;
|
|
2073
2269
|
|
|
2074
|
-
|
|
2075
|
-
options.preloadFeatureFlags = false; // Don't preload as this makes no sense without a distinctId
|
|
2076
|
-
|
|
2077
|
-
options.sendFeatureFlagEvent = false; // Let `posthog-node` handle this on its own, since we're dealing with multiple distinctIDs
|
|
2270
|
+
var _a;
|
|
2078
2271
|
|
|
2272
|
+
options.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'json';
|
|
2079
2273
|
_this = _super.call(this, apiKey, options) || this;
|
|
2080
|
-
_this.options = options;
|
|
2081
2274
|
_this._memoryStorage = new PostHogMemoryStorage();
|
|
2275
|
+
_this.options = options;
|
|
2276
|
+
|
|
2277
|
+
if (options.personalApiKey) {
|
|
2278
|
+
_this.featureFlagsPoller = new FeatureFlagsPoller({
|
|
2279
|
+
pollingInterval: typeof options.featureFlagsPollingInterval === 'number' ? options.featureFlagsPollingInterval : THIRTY_SECONDS,
|
|
2280
|
+
personalApiKey: options.personalApiKey,
|
|
2281
|
+
projectApiKey: apiKey,
|
|
2282
|
+
timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a : 10000,
|
|
2283
|
+
host: _this.host,
|
|
2284
|
+
fetch: options.fetch
|
|
2285
|
+
});
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2288
|
+
_this.distinctIdHasSentFlagCalls = {};
|
|
2289
|
+
_this.maxCacheSize = options.maxCacheSize || MAX_CACHE_SIZE;
|
|
2082
2290
|
return _this;
|
|
2083
2291
|
}
|
|
2084
2292
|
|
|
2085
|
-
|
|
2293
|
+
PostHog.prototype.getPersistedProperty = function (key) {
|
|
2086
2294
|
return this._memoryStorage.getProperty(key);
|
|
2087
2295
|
};
|
|
2088
2296
|
|
|
2089
|
-
|
|
2297
|
+
PostHog.prototype.setPersistedProperty = function (key, value) {
|
|
2090
2298
|
return this._memoryStorage.setProperty(key, value);
|
|
2091
2299
|
};
|
|
2092
2300
|
|
|
2093
|
-
|
|
2094
|
-
// Sessions don't make sense for Node
|
|
2095
|
-
return undefined;
|
|
2096
|
-
};
|
|
2097
|
-
|
|
2098
|
-
PostHogClient.prototype.fetch = function (url, options) {
|
|
2301
|
+
PostHog.prototype.fetch = function (url, options) {
|
|
2099
2302
|
return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
|
|
2100
2303
|
};
|
|
2101
2304
|
|
|
2102
|
-
|
|
2305
|
+
PostHog.prototype.getLibraryId = function () {
|
|
2103
2306
|
return 'posthog-node';
|
|
2104
2307
|
};
|
|
2105
2308
|
|
|
2106
|
-
|
|
2309
|
+
PostHog.prototype.getLibraryVersion = function () {
|
|
2107
2310
|
return version;
|
|
2108
2311
|
};
|
|
2109
2312
|
|
|
2110
|
-
|
|
2313
|
+
PostHog.prototype.getCustomUserAgent = function () {
|
|
2111
2314
|
return "posthog-node/".concat(version);
|
|
2112
2315
|
};
|
|
2113
2316
|
|
|
2114
|
-
return PostHogClient;
|
|
2115
|
-
}(PostHogCore); // The actual exported Nodejs API.
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
var PostHog =
|
|
2119
|
-
/** @class */
|
|
2120
|
-
function () {
|
|
2121
|
-
function PostHog(apiKey, options) {
|
|
2122
|
-
if (options === void 0) {
|
|
2123
|
-
options = {};
|
|
2124
|
-
}
|
|
2125
|
-
|
|
2126
|
-
var _a;
|
|
2127
|
-
|
|
2128
|
-
this._sharedClient = new PostHogClient(apiKey, options);
|
|
2129
|
-
|
|
2130
|
-
if (options.personalApiKey) {
|
|
2131
|
-
this.featureFlagsPoller = new FeatureFlagsPoller({
|
|
2132
|
-
pollingInterval: typeof options.featureFlagsPollingInterval === 'number' ? options.featureFlagsPollingInterval : THIRTY_SECONDS,
|
|
2133
|
-
personalApiKey: options.personalApiKey,
|
|
2134
|
-
projectApiKey: apiKey,
|
|
2135
|
-
timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a : 10000,
|
|
2136
|
-
host: this._sharedClient.host,
|
|
2137
|
-
fetch: options.fetch
|
|
2138
|
-
});
|
|
2139
|
-
}
|
|
2140
|
-
|
|
2141
|
-
this.distinctIdHasSentFlagCalls = {};
|
|
2142
|
-
this.maxCacheSize = options.maxCacheSize || MAX_CACHE_SIZE;
|
|
2143
|
-
}
|
|
2144
|
-
|
|
2145
|
-
PostHog.prototype.reInit = function (distinctId) {
|
|
2146
|
-
// Certain properties we want to persist. Queue is persisted always by default.
|
|
2147
|
-
this._sharedClient.reset([PostHogPersistedProperty.OptedOut]);
|
|
2148
|
-
|
|
2149
|
-
this._sharedClient.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
2150
|
-
};
|
|
2151
|
-
|
|
2152
2317
|
PostHog.prototype.enable = function () {
|
|
2153
|
-
return
|
|
2318
|
+
return _super.prototype.optIn.call(this);
|
|
2154
2319
|
};
|
|
2155
2320
|
|
|
2156
2321
|
PostHog.prototype.disable = function () {
|
|
2157
|
-
return
|
|
2322
|
+
return _super.prototype.optOut.call(this);
|
|
2158
2323
|
};
|
|
2159
2324
|
|
|
2160
2325
|
PostHog.prototype.capture = function (_a) {
|
|
@@ -2166,39 +2331,50 @@ function () {
|
|
|
2166
2331
|
groups = _a.groups,
|
|
2167
2332
|
sendFeatureFlags = _a.sendFeatureFlags,
|
|
2168
2333
|
timestamp = _a.timestamp;
|
|
2169
|
-
this.reInit(distinctId);
|
|
2170
|
-
|
|
2171
|
-
if (groups) {
|
|
2172
|
-
this._sharedClient.groups(groups);
|
|
2173
|
-
}
|
|
2174
2334
|
|
|
2175
|
-
var _capture = function () {
|
|
2176
|
-
|
|
2335
|
+
var _capture = function (props) {
|
|
2336
|
+
_super.prototype.captureStateless.call(_this, distinctId, event, props, {
|
|
2177
2337
|
timestamp: timestamp
|
|
2178
2338
|
});
|
|
2179
2339
|
};
|
|
2180
2340
|
|
|
2181
2341
|
if (sendFeatureFlags) {
|
|
2182
|
-
|
|
2183
|
-
|
|
2342
|
+
_super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups).then(function (flags) {
|
|
2343
|
+
var featureVariantProperties = {};
|
|
2344
|
+
|
|
2345
|
+
if (flags) {
|
|
2346
|
+
for (var _i = 0, _a = Object.entries(flags); _i < _a.length; _i++) {
|
|
2347
|
+
var _b = _a[_i],
|
|
2348
|
+
feature = _b[0],
|
|
2349
|
+
variant = _b[1];
|
|
2350
|
+
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
|
|
2354
|
+
var flagProperties = __assign({
|
|
2355
|
+
$active_feature_flags: flags ? Object.keys(flags) : undefined
|
|
2356
|
+
}, featureVariantProperties);
|
|
2357
|
+
|
|
2358
|
+
_capture(__assign(__assign(__assign({}, properties), {
|
|
2359
|
+
$groups: groups
|
|
2360
|
+
}), flagProperties));
|
|
2184
2361
|
});
|
|
2185
2362
|
} else {
|
|
2186
|
-
_capture()
|
|
2363
|
+
_capture(__assign(__assign({}, properties), {
|
|
2364
|
+
$groups: groups
|
|
2365
|
+
}));
|
|
2187
2366
|
}
|
|
2188
2367
|
};
|
|
2189
2368
|
|
|
2190
2369
|
PostHog.prototype.identify = function (_a) {
|
|
2191
2370
|
var distinctId = _a.distinctId,
|
|
2192
2371
|
properties = _a.properties;
|
|
2193
|
-
this.reInit(distinctId);
|
|
2194
2372
|
|
|
2195
|
-
|
|
2373
|
+
_super.prototype.identifyStateless.call(this, distinctId, properties);
|
|
2196
2374
|
};
|
|
2197
2375
|
|
|
2198
2376
|
PostHog.prototype.alias = function (data) {
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
this._sharedClient.alias(data.alias);
|
|
2377
|
+
_super.prototype.aliasStateless.call(this, data.alias, data.distinctId);
|
|
2202
2378
|
};
|
|
2203
2379
|
|
|
2204
2380
|
PostHog.prototype.getFeatureFlag = function (key, distinctId, options) {
|
|
@@ -2231,28 +2407,12 @@ function () {
|
|
|
2231
2407
|
if (!(!flagWasLocallyEvaluated && !onlyEvaluateLocally)) return [3
|
|
2232
2408
|
/*break*/
|
|
2233
2409
|
, 3];
|
|
2234
|
-
this.reInit(distinctId);
|
|
2235
|
-
|
|
2236
|
-
if (groups != undefined) {
|
|
2237
|
-
this._sharedClient.groups(groups);
|
|
2238
|
-
}
|
|
2239
|
-
|
|
2240
|
-
if (personProperties) {
|
|
2241
|
-
this._sharedClient.personProperties(personProperties);
|
|
2242
|
-
}
|
|
2243
|
-
|
|
2244
|
-
if (groupProperties) {
|
|
2245
|
-
this._sharedClient.groupProperties(groupProperties);
|
|
2246
|
-
}
|
|
2247
|
-
|
|
2248
2410
|
return [4
|
|
2249
2411
|
/*yield*/
|
|
2250
|
-
,
|
|
2412
|
+
, _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
|
|
2251
2413
|
|
|
2252
2414
|
case 2:
|
|
2253
|
-
_d.sent();
|
|
2254
|
-
|
|
2255
|
-
response = this._sharedClient.getFeatureFlag(key);
|
|
2415
|
+
response = _d.sent();
|
|
2256
2416
|
_d.label = 3;
|
|
2257
2417
|
|
|
2258
2418
|
case 3:
|
|
@@ -2341,28 +2501,12 @@ function () {
|
|
|
2341
2501
|
if (!(!payloadWasLocallyEvaluated && !onlyEvaluateLocally)) return [3
|
|
2342
2502
|
/*break*/
|
|
2343
2503
|
, 6];
|
|
2344
|
-
this.reInit(distinctId);
|
|
2345
|
-
|
|
2346
|
-
if (groups != undefined) {
|
|
2347
|
-
this._sharedClient.groups(groups);
|
|
2348
|
-
}
|
|
2349
|
-
|
|
2350
|
-
if (personProperties) {
|
|
2351
|
-
this._sharedClient.personProperties(personProperties);
|
|
2352
|
-
}
|
|
2353
|
-
|
|
2354
|
-
if (groupProperties) {
|
|
2355
|
-
this._sharedClient.groupProperties(groupProperties);
|
|
2356
|
-
}
|
|
2357
|
-
|
|
2358
2504
|
return [4
|
|
2359
2505
|
/*yield*/
|
|
2360
|
-
,
|
|
2506
|
+
, _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
|
|
2361
2507
|
|
|
2362
2508
|
case 5:
|
|
2363
|
-
_d.sent();
|
|
2364
|
-
|
|
2365
|
-
response = this._sharedClient.getFeatureFlagPayload(key);
|
|
2509
|
+
response = _d.sent();
|
|
2366
2510
|
_d.label = 6;
|
|
2367
2511
|
|
|
2368
2512
|
case 6:
|
|
@@ -2466,28 +2610,12 @@ function () {
|
|
|
2466
2610
|
if (!(fallbackToDecide && !onlyEvaluateLocally)) return [3
|
|
2467
2611
|
/*break*/
|
|
2468
2612
|
, 3];
|
|
2469
|
-
this.reInit(distinctId);
|
|
2470
|
-
|
|
2471
|
-
if (groups) {
|
|
2472
|
-
this._sharedClient.groups(groups);
|
|
2473
|
-
}
|
|
2474
|
-
|
|
2475
|
-
if (personProperties) {
|
|
2476
|
-
this._sharedClient.personProperties(personProperties);
|
|
2477
|
-
}
|
|
2478
|
-
|
|
2479
|
-
if (groupProperties) {
|
|
2480
|
-
this._sharedClient.groupProperties(groupProperties);
|
|
2481
|
-
}
|
|
2482
|
-
|
|
2483
2613
|
return [4
|
|
2484
2614
|
/*yield*/
|
|
2485
|
-
,
|
|
2615
|
+
, _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties)];
|
|
2486
2616
|
|
|
2487
2617
|
case 2:
|
|
2488
|
-
_c.sent();
|
|
2489
|
-
|
|
2490
|
-
remoteEvaluationResult = this._sharedClient.getFeatureFlagsAndPayloads();
|
|
2618
|
+
remoteEvaluationResult = _c.sent();
|
|
2491
2619
|
featureFlags = __assign(__assign({}, featureFlags), remoteEvaluationResult.flags || {});
|
|
2492
2620
|
featureFlagPayloads = __assign(__assign({}, featureFlagPayloads), remoteEvaluationResult.payloads || {});
|
|
2493
2621
|
_c.label = 3;
|
|
@@ -2508,9 +2636,8 @@ function () {
|
|
|
2508
2636
|
var groupType = _a.groupType,
|
|
2509
2637
|
groupKey = _a.groupKey,
|
|
2510
2638
|
properties = _a.properties;
|
|
2511
|
-
this.reInit("$".concat(groupType, "_").concat(groupKey));
|
|
2512
2639
|
|
|
2513
|
-
|
|
2640
|
+
_super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties);
|
|
2514
2641
|
};
|
|
2515
2642
|
|
|
2516
2643
|
PostHog.prototype.reloadFeatureFlags = function () {
|
|
@@ -2535,10 +2662,6 @@ function () {
|
|
|
2535
2662
|
});
|
|
2536
2663
|
};
|
|
2537
2664
|
|
|
2538
|
-
PostHog.prototype.flush = function () {
|
|
2539
|
-
this._sharedClient.flush();
|
|
2540
|
-
};
|
|
2541
|
-
|
|
2542
2665
|
PostHog.prototype.shutdown = function () {
|
|
2543
2666
|
void this.shutdownAsync();
|
|
2544
2667
|
};
|
|
@@ -2551,17 +2674,13 @@ function () {
|
|
|
2551
2674
|
(_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.stopPoller();
|
|
2552
2675
|
return [2
|
|
2553
2676
|
/*return*/
|
|
2554
|
-
,
|
|
2677
|
+
, _super.prototype.shutdownAsync.call(this)];
|
|
2555
2678
|
});
|
|
2556
2679
|
});
|
|
2557
2680
|
};
|
|
2558
2681
|
|
|
2559
|
-
PostHog.prototype.debug = function (enabled) {
|
|
2560
|
-
return this._sharedClient.debug(enabled);
|
|
2561
|
-
};
|
|
2562
|
-
|
|
2563
2682
|
return PostHog;
|
|
2564
|
-
}();
|
|
2683
|
+
}(PostHogCoreStateless);
|
|
2565
2684
|
|
|
2566
2685
|
export { PostHog };
|
|
2567
2686
|
//# sourceMappingURL=index.esm.js.map
|