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