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