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