posthog-js-lite 2.2.0 → 2.3.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 +12 -0
- package/lib/index.cjs.js +516 -238
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +61 -23
- package/lib/index.esm.js +516 -238
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +58 -22
- package/lib/posthog-core/src/types.d.ts +2 -0
- package/lib/posthog-core/src/utils.d.ts +1 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -1
- package/lib/node_modules/tslib/tslib.es6.d.ts +0 -35
package/lib/index.cjs.js
CHANGED
|
@@ -180,29 +180,31 @@ function retriable(fn, props) {
|
|
|
180
180
|
i = 0;
|
|
181
181
|
_d.label = 1;
|
|
182
182
|
case 1:
|
|
183
|
-
if (!(i < retryCount + 1)) return [3 /*break*/,
|
|
184
|
-
|
|
183
|
+
if (!(i < retryCount + 1)) return [3 /*break*/, 7];
|
|
184
|
+
if (!(i > 0)) return [3 /*break*/, 3];
|
|
185
|
+
// don't wait when it's the last try
|
|
186
|
+
return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
|
|
185
187
|
case 2:
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
// don't wait when it's the last try
|
|
189
|
+
_d.sent();
|
|
190
|
+
_d.label = 3;
|
|
188
191
|
case 3:
|
|
192
|
+
_d.trys.push([3, 5, , 6]);
|
|
193
|
+
return [4 /*yield*/, fn()];
|
|
194
|
+
case 4:
|
|
189
195
|
res = _d.sent();
|
|
190
196
|
return [2 /*return*/, res];
|
|
191
|
-
case
|
|
197
|
+
case 5:
|
|
192
198
|
e_1 = _d.sent();
|
|
193
199
|
lastError = e_1;
|
|
194
200
|
if (!retryCheck(e_1)) {
|
|
195
201
|
throw e_1;
|
|
196
202
|
}
|
|
197
|
-
return [3 /*break*/,
|
|
198
|
-
case 5: return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
|
|
203
|
+
return [3 /*break*/, 6];
|
|
199
204
|
case 6:
|
|
200
|
-
_d.sent();
|
|
201
|
-
_d.label = 7;
|
|
202
|
-
case 7:
|
|
203
205
|
i++;
|
|
204
206
|
return [3 /*break*/, 1];
|
|
205
|
-
case
|
|
207
|
+
case 7: throw lastError;
|
|
206
208
|
}
|
|
207
209
|
});
|
|
208
210
|
});
|
|
@@ -701,11 +703,35 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
701
703
|
return SimpleEventEmitter;
|
|
702
704
|
}());
|
|
703
705
|
|
|
704
|
-
var
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
var
|
|
708
|
-
|
|
706
|
+
var PostHogFetchHttpError = /** @class */ (function (_super) {
|
|
707
|
+
__extends(PostHogFetchHttpError, _super);
|
|
708
|
+
function PostHogFetchHttpError(response) {
|
|
709
|
+
var _this = _super.call(this, 'HTTP error while fetching PostHog: ' + response.status) || this;
|
|
710
|
+
_this.response = response;
|
|
711
|
+
_this.name = 'PostHogFetchHttpError';
|
|
712
|
+
return _this;
|
|
713
|
+
}
|
|
714
|
+
return PostHogFetchHttpError;
|
|
715
|
+
}(Error));
|
|
716
|
+
var PostHogFetchNetworkError = /** @class */ (function (_super) {
|
|
717
|
+
__extends(PostHogFetchNetworkError, _super);
|
|
718
|
+
function PostHogFetchNetworkError(error) {
|
|
719
|
+
var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
|
|
720
|
+
_this.error = error;
|
|
721
|
+
_this.name = 'PostHogFetchNetworkError';
|
|
722
|
+
return _this;
|
|
723
|
+
}
|
|
724
|
+
return PostHogFetchNetworkError;
|
|
725
|
+
}(Error));
|
|
726
|
+
function isPostHogFetchError(err) {
|
|
727
|
+
return typeof err === 'object' && (err.name === 'PostHogFetchHttpError' || err.name === 'PostHogFetchNetworkError');
|
|
728
|
+
}
|
|
729
|
+
var PostHogCoreStateless = /** @class */ (function () {
|
|
730
|
+
function PostHogCoreStateless(apiKey, options) {
|
|
731
|
+
var _a, _b, _c, _d, _e;
|
|
732
|
+
this.debugMode = false;
|
|
733
|
+
this.pendingPromises = {};
|
|
734
|
+
this.disableGeoip = true;
|
|
709
735
|
// internal
|
|
710
736
|
this._events = new SimpleEventEmitter();
|
|
711
737
|
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
@@ -714,33 +740,438 @@ var PostHogCore = /** @class */ (function () {
|
|
|
714
740
|
this.flushAt = (options === null || options === void 0 ? void 0 : options.flushAt) ? Math.max(options === null || options === void 0 ? void 0 : options.flushAt, 1) : 20;
|
|
715
741
|
this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
|
|
716
742
|
this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
|
|
717
|
-
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
718
743
|
// If enable is explicitly set to false we override the optout
|
|
719
744
|
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
|
|
720
745
|
this._retryOptions = {
|
|
721
|
-
retryCount: (
|
|
722
|
-
retryDelay: (
|
|
746
|
+
retryCount: (_b = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _b !== void 0 ? _b : 3,
|
|
747
|
+
retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
|
|
748
|
+
retryCheck: isPostHogFetchError,
|
|
749
|
+
};
|
|
750
|
+
this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
|
|
751
|
+
this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
|
|
752
|
+
}
|
|
753
|
+
PostHogCoreStateless.prototype.getCommonEventProperties = function () {
|
|
754
|
+
return {
|
|
755
|
+
$lib: this.getLibraryId(),
|
|
756
|
+
$lib_version: this.getLibraryVersion(),
|
|
757
|
+
};
|
|
758
|
+
};
|
|
759
|
+
Object.defineProperty(PostHogCoreStateless.prototype, "optedOut", {
|
|
760
|
+
get: function () {
|
|
761
|
+
var _a, _b;
|
|
762
|
+
return (_b = (_a = this.getPersistedProperty(PostHogPersistedProperty.OptedOut)) !== null && _a !== void 0 ? _a : this._optoutOverride) !== null && _b !== void 0 ? _b : false;
|
|
763
|
+
},
|
|
764
|
+
enumerable: false,
|
|
765
|
+
configurable: true
|
|
766
|
+
});
|
|
767
|
+
PostHogCoreStateless.prototype.optIn = function () {
|
|
768
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
769
|
+
};
|
|
770
|
+
PostHogCoreStateless.prototype.optOut = function () {
|
|
771
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
772
|
+
};
|
|
773
|
+
PostHogCoreStateless.prototype.on = function (event, cb) {
|
|
774
|
+
return this._events.on(event, cb);
|
|
775
|
+
};
|
|
776
|
+
PostHogCoreStateless.prototype.debug = function (enabled) {
|
|
777
|
+
var _a;
|
|
778
|
+
if (enabled === void 0) { enabled = true; }
|
|
779
|
+
(_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
780
|
+
this.debugMode = enabled;
|
|
781
|
+
if (enabled) {
|
|
782
|
+
this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
PostHogCoreStateless.prototype.buildPayload = function (payload) {
|
|
786
|
+
return {
|
|
787
|
+
distinct_id: payload.distinct_id,
|
|
788
|
+
event: payload.event,
|
|
789
|
+
properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
/***
|
|
793
|
+
*** TRACKING
|
|
794
|
+
***/
|
|
795
|
+
PostHogCoreStateless.prototype.identifyStateless = function (distinctId, properties, options) {
|
|
796
|
+
// The properties passed to identifyStateless are event properties.
|
|
797
|
+
// To add person properties, pass in all person properties to the `$set` key.
|
|
798
|
+
var payload = __assign({}, this.buildPayload({
|
|
799
|
+
distinct_id: distinctId,
|
|
800
|
+
event: '$identify',
|
|
801
|
+
properties: properties,
|
|
802
|
+
}));
|
|
803
|
+
this.enqueue('identify', payload, options);
|
|
804
|
+
return this;
|
|
805
|
+
};
|
|
806
|
+
PostHogCoreStateless.prototype.captureStateless = function (distinctId, event, properties, options) {
|
|
807
|
+
var payload = this.buildPayload({ distinct_id: distinctId, event: event, properties: properties });
|
|
808
|
+
this.enqueue('capture', payload, options);
|
|
809
|
+
return this;
|
|
810
|
+
};
|
|
811
|
+
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties, options) {
|
|
812
|
+
var payload = this.buildPayload({
|
|
813
|
+
event: '$create_alias',
|
|
814
|
+
distinct_id: distinctId,
|
|
815
|
+
properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
|
|
816
|
+
});
|
|
817
|
+
this.enqueue('alias', payload, options);
|
|
818
|
+
return this;
|
|
819
|
+
};
|
|
820
|
+
/***
|
|
821
|
+
*** GROUPS
|
|
822
|
+
***/
|
|
823
|
+
PostHogCoreStateless.prototype.groupIdentifyStateless = function (groupType, groupKey, groupProperties, options, distinctId, eventProperties) {
|
|
824
|
+
var payload = this.buildPayload({
|
|
825
|
+
distinct_id: distinctId || "$".concat(groupType, "_").concat(groupKey),
|
|
826
|
+
event: '$groupidentify',
|
|
827
|
+
properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, (eventProperties || {})),
|
|
828
|
+
});
|
|
829
|
+
this.enqueue('capture', payload, options);
|
|
830
|
+
return this;
|
|
831
|
+
};
|
|
832
|
+
/***
|
|
833
|
+
*** FEATURE FLAGS
|
|
834
|
+
***/
|
|
835
|
+
PostHogCoreStateless.prototype.getDecide = function (distinctId, groups, personProperties, groupProperties, extraPayload) {
|
|
836
|
+
if (groups === void 0) { groups = {}; }
|
|
837
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
838
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
839
|
+
if (extraPayload === void 0) { extraPayload = {}; }
|
|
840
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
841
|
+
var url, fetchOptions;
|
|
842
|
+
return __generator(this, function (_a) {
|
|
843
|
+
url = "".concat(this.host, "/decide/?v=3");
|
|
844
|
+
fetchOptions = {
|
|
845
|
+
method: 'POST',
|
|
846
|
+
headers: { 'Content-Type': 'application/json' },
|
|
847
|
+
body: JSON.stringify(__assign({ token: this.apiKey, distinct_id: distinctId, groups: groups, person_properties: personProperties, group_properties: groupProperties }, extraPayload)),
|
|
848
|
+
};
|
|
849
|
+
return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
|
|
850
|
+
.then(function (response) { return response.json(); })
|
|
851
|
+
.catch(function (error) {
|
|
852
|
+
console.error('Error fetching feature flags', error);
|
|
853
|
+
return undefined;
|
|
854
|
+
})];
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
};
|
|
858
|
+
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
859
|
+
if (groups === void 0) { groups = {}; }
|
|
860
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
861
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
862
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
863
|
+
var featureFlags, response;
|
|
864
|
+
return __generator(this, function (_a) {
|
|
865
|
+
switch (_a.label) {
|
|
866
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
867
|
+
case 1:
|
|
868
|
+
featureFlags = _a.sent();
|
|
869
|
+
if (!featureFlags) {
|
|
870
|
+
// If we haven't loaded flags yet, or errored out, we respond with undefined
|
|
871
|
+
return [2 /*return*/, undefined];
|
|
872
|
+
}
|
|
873
|
+
response = featureFlags[key];
|
|
874
|
+
// `/decide` v3 returns all flags
|
|
875
|
+
if (response === undefined) {
|
|
876
|
+
// For cases where the flag is unknown, return false
|
|
877
|
+
response = false;
|
|
878
|
+
}
|
|
879
|
+
// If we have flags we either return the value (true or string) or false
|
|
880
|
+
return [2 /*return*/, response];
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
};
|
|
885
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
886
|
+
if (groups === void 0) { groups = {}; }
|
|
887
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
888
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
889
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
890
|
+
var payloads, response;
|
|
891
|
+
return __generator(this, function (_a) {
|
|
892
|
+
switch (_a.label) {
|
|
893
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
894
|
+
case 1:
|
|
895
|
+
payloads = _a.sent();
|
|
896
|
+
if (!payloads) {
|
|
897
|
+
return [2 /*return*/, undefined];
|
|
898
|
+
}
|
|
899
|
+
response = payloads[key];
|
|
900
|
+
// Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
|
|
901
|
+
if (response === undefined) {
|
|
902
|
+
return [2 /*return*/, null];
|
|
903
|
+
}
|
|
904
|
+
return [2 /*return*/, this._parsePayload(response)];
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
910
|
+
if (groups === void 0) { groups = {}; }
|
|
911
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
912
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
913
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
914
|
+
var payloads;
|
|
915
|
+
var _this = this;
|
|
916
|
+
return __generator(this, function (_a) {
|
|
917
|
+
switch (_a.label) {
|
|
918
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
919
|
+
case 1:
|
|
920
|
+
payloads = (_a.sent()).payloads;
|
|
921
|
+
if (payloads) {
|
|
922
|
+
return [2 /*return*/, Object.fromEntries(Object.entries(payloads).map(function (_a) {
|
|
923
|
+
var k = _a[0], v = _a[1];
|
|
924
|
+
return [k, _this._parsePayload(v)];
|
|
925
|
+
}))];
|
|
926
|
+
}
|
|
927
|
+
return [2 /*return*/, payloads];
|
|
928
|
+
}
|
|
929
|
+
});
|
|
930
|
+
});
|
|
931
|
+
};
|
|
932
|
+
PostHogCoreStateless.prototype._parsePayload = function (response) {
|
|
933
|
+
try {
|
|
934
|
+
return JSON.parse(response);
|
|
935
|
+
}
|
|
936
|
+
catch (_a) {
|
|
937
|
+
return response;
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
941
|
+
if (groups === void 0) { groups = {}; }
|
|
942
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
943
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
944
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
945
|
+
return __generator(this, function (_a) {
|
|
946
|
+
switch (_a.label) {
|
|
947
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
948
|
+
case 1: return [2 /*return*/, (_a.sent()).flags];
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
};
|
|
953
|
+
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
954
|
+
if (groups === void 0) { groups = {}; }
|
|
955
|
+
if (personProperties === void 0) { personProperties = {}; }
|
|
956
|
+
if (groupProperties === void 0) { groupProperties = {}; }
|
|
957
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
958
|
+
var extraPayload, decideResponse, flags, payloads;
|
|
959
|
+
return __generator(this, function (_a) {
|
|
960
|
+
switch (_a.label) {
|
|
961
|
+
case 0:
|
|
962
|
+
extraPayload = {};
|
|
963
|
+
if (disableGeoip !== null && disableGeoip !== void 0 ? disableGeoip : this.disableGeoip) {
|
|
964
|
+
extraPayload['geoip_disable'] = true;
|
|
965
|
+
}
|
|
966
|
+
return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload)];
|
|
967
|
+
case 1:
|
|
968
|
+
decideResponse = _a.sent();
|
|
969
|
+
flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
|
|
970
|
+
payloads = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlagPayloads;
|
|
971
|
+
return [2 /*return*/, {
|
|
972
|
+
flags: flags,
|
|
973
|
+
payloads: payloads,
|
|
974
|
+
}];
|
|
975
|
+
}
|
|
976
|
+
});
|
|
977
|
+
});
|
|
978
|
+
};
|
|
979
|
+
/***
|
|
980
|
+
*** QUEUEING AND FLUSHING
|
|
981
|
+
***/
|
|
982
|
+
PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
|
|
983
|
+
var _this = this;
|
|
984
|
+
var _a;
|
|
985
|
+
if (this.optedOut) {
|
|
986
|
+
this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
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() });
|
|
990
|
+
var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
|
|
991
|
+
if (addGeoipDisableProperty) {
|
|
992
|
+
if (!message.properties) {
|
|
993
|
+
message.properties = {};
|
|
994
|
+
}
|
|
995
|
+
message['properties']['$geoip_disable'] = true;
|
|
996
|
+
}
|
|
997
|
+
if (message.distinctId) {
|
|
998
|
+
message.distinct_id = message.distinctId;
|
|
999
|
+
delete message.distinctId;
|
|
1000
|
+
}
|
|
1001
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1002
|
+
queue.push({ message: message });
|
|
1003
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1004
|
+
this._events.emit(type, message);
|
|
1005
|
+
// Flush queued events if we meet the flushAt length
|
|
1006
|
+
if (queue.length >= this.flushAt) {
|
|
1007
|
+
this.flush();
|
|
1008
|
+
}
|
|
1009
|
+
if (this.flushInterval && !this._flushTimer) {
|
|
1010
|
+
this._flushTimer = safeSetTimeout(function () { return _this.flush(); }, this.flushInterval);
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
PostHogCoreStateless.prototype.flushAsync = function () {
|
|
1014
|
+
var _this = this;
|
|
1015
|
+
return new Promise(function (resolve, reject) {
|
|
1016
|
+
_this.flush(function (err, data) {
|
|
1017
|
+
return err ? reject(err) : resolve(data);
|
|
1018
|
+
});
|
|
1019
|
+
});
|
|
1020
|
+
};
|
|
1021
|
+
PostHogCoreStateless.prototype.flush = function (callback) {
|
|
1022
|
+
var _this = this;
|
|
1023
|
+
if (this._flushTimer) {
|
|
1024
|
+
clearTimeout(this._flushTimer);
|
|
1025
|
+
this._flushTimer = null;
|
|
1026
|
+
}
|
|
1027
|
+
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1028
|
+
if (!queue.length) {
|
|
1029
|
+
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1030
|
+
}
|
|
1031
|
+
var items = queue.splice(0, this.flushAt);
|
|
1032
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1033
|
+
var messages = items.map(function (item) { return item.message; });
|
|
1034
|
+
var data = {
|
|
1035
|
+
api_key: this.apiKey,
|
|
1036
|
+
batch: messages,
|
|
1037
|
+
sent_at: currentISOTime(),
|
|
723
1038
|
};
|
|
724
|
-
|
|
725
|
-
|
|
1039
|
+
var promiseUUID = generateUUID();
|
|
1040
|
+
var done = function (err) {
|
|
1041
|
+
if (err) {
|
|
1042
|
+
_this._events.emit('error', err);
|
|
1043
|
+
}
|
|
1044
|
+
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1045
|
+
// remove promise from pendingPromises
|
|
1046
|
+
delete _this.pendingPromises[promiseUUID];
|
|
1047
|
+
_this._events.emit('flush', messages);
|
|
1048
|
+
};
|
|
1049
|
+
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1050
|
+
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1051
|
+
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1052
|
+
// but browsers such as Chrome and Safari have not caught up.
|
|
1053
|
+
this.getCustomUserAgent();
|
|
1054
|
+
var payload = JSON.stringify(data);
|
|
1055
|
+
var url = this.captureMode === 'form'
|
|
1056
|
+
? "".concat(this.host, "/e/?ip=1&_=").concat(currentTimestamp(), "&v=").concat(this.getLibraryVersion())
|
|
1057
|
+
: "".concat(this.host, "/batch/");
|
|
1058
|
+
var fetchOptions = this.captureMode === 'form'
|
|
1059
|
+
? {
|
|
1060
|
+
method: 'POST',
|
|
1061
|
+
mode: 'no-cors',
|
|
1062
|
+
credentials: 'omit',
|
|
1063
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1064
|
+
body: "data=".concat(encodeURIComponent(LZString.compressToBase64(payload)), "&compression=lz64"),
|
|
1065
|
+
}
|
|
1066
|
+
: {
|
|
1067
|
+
method: 'POST',
|
|
1068
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1069
|
+
body: payload,
|
|
1070
|
+
};
|
|
1071
|
+
var requestPromise = this.fetchWithRetry(url, fetchOptions);
|
|
1072
|
+
this.pendingPromises[promiseUUID] = requestPromise;
|
|
1073
|
+
requestPromise
|
|
1074
|
+
.then(function () { return done(); })
|
|
1075
|
+
.catch(function (err) {
|
|
1076
|
+
done(err);
|
|
1077
|
+
});
|
|
1078
|
+
};
|
|
1079
|
+
PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1080
|
+
var _a;
|
|
1081
|
+
var _b;
|
|
1082
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1083
|
+
var _this = this;
|
|
1084
|
+
return __generator(this, function (_c) {
|
|
1085
|
+
switch (_c.label) {
|
|
1086
|
+
case 0:
|
|
1087
|
+
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1088
|
+
var ctrl = new AbortController();
|
|
1089
|
+
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1090
|
+
return ctrl.signal;
|
|
1091
|
+
});
|
|
1092
|
+
return [4 /*yield*/, retriable(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1093
|
+
var res, e_1;
|
|
1094
|
+
return __generator(this, function (_a) {
|
|
1095
|
+
switch (_a.label) {
|
|
1096
|
+
case 0:
|
|
1097
|
+
res = null;
|
|
1098
|
+
_a.label = 1;
|
|
1099
|
+
case 1:
|
|
1100
|
+
_a.trys.push([1, 3, , 4]);
|
|
1101
|
+
return [4 /*yield*/, this.fetch(url, __assign({ signal: AbortSignal.timeout(this.requestTimeout) }, options))];
|
|
1102
|
+
case 2:
|
|
1103
|
+
res = _a.sent();
|
|
1104
|
+
return [3 /*break*/, 4];
|
|
1105
|
+
case 3:
|
|
1106
|
+
e_1 = _a.sent();
|
|
1107
|
+
// fetch will only throw on network errors or on timeouts
|
|
1108
|
+
throw new PostHogFetchNetworkError(e_1);
|
|
1109
|
+
case 4:
|
|
1110
|
+
if (res.status < 200 || res.status >= 400) {
|
|
1111
|
+
throw new PostHogFetchHttpError(res);
|
|
1112
|
+
}
|
|
1113
|
+
return [2 /*return*/, res];
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
}); }, __assign(__assign({}, this._retryOptions), retryOptions))];
|
|
1117
|
+
case 1: return [2 /*return*/, _c.sent()];
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
});
|
|
1121
|
+
};
|
|
1122
|
+
PostHogCoreStateless.prototype.shutdownAsync = function () {
|
|
1123
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1124
|
+
var e_2;
|
|
1125
|
+
return __generator(this, function (_a) {
|
|
1126
|
+
switch (_a.label) {
|
|
1127
|
+
case 0:
|
|
1128
|
+
clearTimeout(this._flushTimer);
|
|
1129
|
+
_a.label = 1;
|
|
1130
|
+
case 1:
|
|
1131
|
+
_a.trys.push([1, 4, , 5]);
|
|
1132
|
+
return [4 /*yield*/, this.flushAsync()];
|
|
1133
|
+
case 2:
|
|
1134
|
+
_a.sent();
|
|
1135
|
+
return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
|
|
1136
|
+
case 3:
|
|
1137
|
+
_a.sent();
|
|
1138
|
+
return [3 /*break*/, 5];
|
|
1139
|
+
case 4:
|
|
1140
|
+
e_2 = _a.sent();
|
|
1141
|
+
if (!isPostHogFetchError(e_2)) {
|
|
1142
|
+
throw e_2;
|
|
1143
|
+
}
|
|
1144
|
+
console.error('Error while shutting down PostHog', e_2);
|
|
1145
|
+
return [3 /*break*/, 5];
|
|
1146
|
+
case 5: return [2 /*return*/];
|
|
1147
|
+
}
|
|
1148
|
+
});
|
|
1149
|
+
});
|
|
1150
|
+
};
|
|
1151
|
+
PostHogCoreStateless.prototype.shutdown = function () {
|
|
1152
|
+
void this.shutdownAsync();
|
|
1153
|
+
};
|
|
1154
|
+
return PostHogCoreStateless;
|
|
1155
|
+
}());
|
|
1156
|
+
var PostHogCore = /** @class */ (function (_super) {
|
|
1157
|
+
__extends(PostHogCore, _super);
|
|
1158
|
+
function PostHogCore(apiKey, options) {
|
|
1159
|
+
var _this = this;
|
|
1160
|
+
var _a, _b, _c;
|
|
1161
|
+
// Default for stateful mode is to not disable geoip. Only override if explicitly set
|
|
1162
|
+
var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
|
|
1163
|
+
_this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
|
|
1164
|
+
_this.flagCallReported = {};
|
|
1165
|
+
_this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
1166
|
+
_this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
|
|
726
1167
|
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
727
1168
|
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
728
1169
|
safeSetTimeout(function () {
|
|
729
|
-
|
|
1170
|
+
_this.reloadFeatureFlags();
|
|
730
1171
|
}, 1);
|
|
731
1172
|
}
|
|
1173
|
+
return _this;
|
|
732
1174
|
}
|
|
733
|
-
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
734
|
-
var featureFlags = this.getFeatureFlags();
|
|
735
|
-
var featureVariantProperties = {};
|
|
736
|
-
if (featureFlags) {
|
|
737
|
-
for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
|
|
738
|
-
var _b = _a[_i], feature = _b[0], variant = _b[1];
|
|
739
|
-
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
|
|
743
|
-
};
|
|
744
1175
|
PostHogCore.prototype.setupBootstrap = function (options) {
|
|
745
1176
|
var _a, _b, _c, _d;
|
|
746
1177
|
if ((_a = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _a === void 0 ? void 0 : _a.distinctId) {
|
|
@@ -779,20 +1210,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
779
1210
|
PostHogCore.prototype.clearProps = function () {
|
|
780
1211
|
this.props = undefined;
|
|
781
1212
|
};
|
|
782
|
-
Object.defineProperty(PostHogCore.prototype, "optedOut", {
|
|
783
|
-
get: function () {
|
|
784
|
-
var _a, _b;
|
|
785
|
-
return (_b = (_a = this.getPersistedProperty(PostHogPersistedProperty.OptedOut)) !== null && _a !== void 0 ? _a : this._optoutOverride) !== null && _b !== void 0 ? _b : false;
|
|
786
|
-
},
|
|
787
|
-
enumerable: false,
|
|
788
|
-
configurable: true
|
|
789
|
-
});
|
|
790
|
-
PostHogCore.prototype.optIn = function () {
|
|
791
|
-
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
792
|
-
};
|
|
793
|
-
PostHogCore.prototype.optOut = function () {
|
|
794
|
-
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
795
|
-
};
|
|
796
1213
|
PostHogCore.prototype.on = function (event, cb) {
|
|
797
1214
|
return this._events.on(event, cb);
|
|
798
1215
|
};
|
|
@@ -807,20 +1224,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
807
1224
|
}
|
|
808
1225
|
}
|
|
809
1226
|
};
|
|
810
|
-
PostHogCore.prototype.
|
|
811
|
-
var
|
|
812
|
-
|
|
813
|
-
(
|
|
814
|
-
|
|
815
|
-
|
|
1227
|
+
PostHogCore.prototype.getCommonEventProperties = function () {
|
|
1228
|
+
var featureFlags = this.getFeatureFlags();
|
|
1229
|
+
var featureVariantProperties = {};
|
|
1230
|
+
if (featureFlags) {
|
|
1231
|
+
for (var _i = 0, _a = Object.entries(featureFlags); _i < _a.length; _i++) {
|
|
1232
|
+
var _b = _a[_i], feature = _b[0], variant = _b[1];
|
|
1233
|
+
featureVariantProperties["$feature/".concat(feature)] = variant;
|
|
1234
|
+
}
|
|
816
1235
|
}
|
|
1236
|
+
return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
|
|
817
1237
|
};
|
|
818
|
-
PostHogCore.prototype.
|
|
819
|
-
return {
|
|
820
|
-
distinct_id: payload.distinct_id || this.getDistinctId(),
|
|
821
|
-
event: payload.event,
|
|
822
|
-
properties: __assign(__assign(__assign(__assign({}, this.props), (payload.properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() }),
|
|
823
|
-
};
|
|
1238
|
+
PostHogCore.prototype.enrichProperties = function (properties) {
|
|
1239
|
+
return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
|
|
824
1240
|
};
|
|
825
1241
|
PostHogCore.prototype.getSessionId = function () {
|
|
826
1242
|
var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
|
|
@@ -863,48 +1279,41 @@ var PostHogCore = /** @class */ (function () {
|
|
|
863
1279
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
864
1280
|
this.groups(properties.$groups);
|
|
865
1281
|
}
|
|
866
|
-
var
|
|
867
|
-
distinct_id: distinctId,
|
|
868
|
-
event: '$identify',
|
|
869
|
-
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
|
|
870
|
-
})), { $set: properties });
|
|
1282
|
+
var allProperties = this.enrichProperties(__assign(__assign({}, properties), { $anon_distinct_id: this.getAnonymousId(), $set: properties }));
|
|
871
1283
|
if (distinctId !== previousDistinctId) {
|
|
872
1284
|
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
873
1285
|
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
874
1286
|
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
875
1287
|
if (this.getFeatureFlags()) {
|
|
876
|
-
|
|
1288
|
+
this.reloadFeatureFlags();
|
|
877
1289
|
}
|
|
878
1290
|
}
|
|
879
|
-
|
|
1291
|
+
_super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
|
|
880
1292
|
return this;
|
|
881
1293
|
};
|
|
882
1294
|
PostHogCore.prototype.capture = function (event, properties, options) {
|
|
1295
|
+
var distinctId = this.getDistinctId();
|
|
883
1296
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
884
1297
|
this.groups(properties.$groups);
|
|
885
1298
|
}
|
|
886
|
-
var
|
|
887
|
-
|
|
1299
|
+
var allProperties = this.enrichProperties(properties);
|
|
1300
|
+
_super.prototype.captureStateless.call(this, distinctId, event, allProperties, options);
|
|
888
1301
|
return this;
|
|
889
1302
|
};
|
|
890
1303
|
PostHogCore.prototype.alias = function (alias) {
|
|
891
1304
|
var distinctId = this.getDistinctId();
|
|
892
|
-
var
|
|
893
|
-
|
|
894
|
-
properties: {
|
|
895
|
-
distinct_id: distinctId,
|
|
896
|
-
alias: alias,
|
|
897
|
-
},
|
|
898
|
-
});
|
|
899
|
-
this.enqueue('alias', payload);
|
|
1305
|
+
var allProperties = this.enrichProperties({});
|
|
1306
|
+
_super.prototype.aliasStateless.call(this, alias, distinctId, allProperties);
|
|
900
1307
|
return this;
|
|
901
1308
|
};
|
|
902
1309
|
PostHogCore.prototype.autocapture = function (eventType, elements, properties, options) {
|
|
903
1310
|
if (properties === void 0) { properties = {}; }
|
|
904
|
-
var
|
|
1311
|
+
var distinctId = this.getDistinctId();
|
|
1312
|
+
var payload = {
|
|
1313
|
+
distinct_id: distinctId,
|
|
905
1314
|
event: '$autocapture',
|
|
906
|
-
properties: __assign(__assign({}, properties), { $event_type: eventType, $elements: elements }),
|
|
907
|
-
}
|
|
1315
|
+
properties: __assign(__assign({}, this.enrichProperties(properties)), { $event_type: eventType, $elements: elements }),
|
|
1316
|
+
};
|
|
908
1317
|
this.enqueue('autocapture', payload, options);
|
|
909
1318
|
return this;
|
|
910
1319
|
};
|
|
@@ -918,7 +1327,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
918
1327
|
$groups: __assign(__assign({}, existingGroups), groups),
|
|
919
1328
|
});
|
|
920
1329
|
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
|
|
921
|
-
|
|
1330
|
+
this.reloadFeatureFlags();
|
|
922
1331
|
}
|
|
923
1332
|
return this;
|
|
924
1333
|
};
|
|
@@ -933,11 +1342,9 @@ var PostHogCore = /** @class */ (function () {
|
|
|
933
1342
|
return this;
|
|
934
1343
|
};
|
|
935
1344
|
PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties, options) {
|
|
936
|
-
var
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
});
|
|
940
|
-
this.enqueue('capture', payload, options);
|
|
1345
|
+
var distinctId = this.getDistinctId();
|
|
1346
|
+
var eventProperties = this.enrichProperties({});
|
|
1347
|
+
_super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, groupProperties, options, distinctId, eventProperties);
|
|
941
1348
|
return this;
|
|
942
1349
|
};
|
|
943
1350
|
/***
|
|
@@ -974,30 +1381,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
974
1381
|
PostHogCore.prototype._decideAsync = function (sendAnonDistinctId) {
|
|
975
1382
|
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
976
1383
|
return __awaiter(this, void 0, void 0, function () {
|
|
977
|
-
var
|
|
1384
|
+
var distinctId, groups, personProperties, groupProperties, extraProperties;
|
|
978
1385
|
var _this = this;
|
|
979
1386
|
return __generator(this, function (_a) {
|
|
980
|
-
url = "".concat(this.host, "/decide/?v=3");
|
|
981
1387
|
distinctId = this.getDistinctId();
|
|
982
1388
|
groups = this.props.$groups || {};
|
|
983
1389
|
personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
984
1390
|
groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
headers: { 'Content-Type': 'application/json' },
|
|
988
|
-
body: JSON.stringify({
|
|
989
|
-
token: this.apiKey,
|
|
990
|
-
distinct_id: distinctId,
|
|
991
|
-
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
|
|
992
|
-
groups: groups,
|
|
993
|
-
person_properties: personProperties,
|
|
994
|
-
group_properties: groupProperties,
|
|
995
|
-
}),
|
|
1391
|
+
extraProperties = {
|
|
1392
|
+
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
|
|
996
1393
|
};
|
|
997
|
-
this._decideResponsePromise =
|
|
998
|
-
.then(function (r) { return r.json(); })
|
|
1394
|
+
this._decideResponsePromise = _super.prototype.getDecide.call(this, distinctId, groups, personProperties, groupProperties, extraProperties)
|
|
999
1395
|
.then(function (res) {
|
|
1000
|
-
if (res.featureFlags) {
|
|
1396
|
+
if (res === null || res === void 0 ? void 0 : res.featureFlags) {
|
|
1001
1397
|
var newFeatureFlags = res.featureFlags;
|
|
1002
1398
|
var newFeatureFlagPayloads = res.featureFlagPayloads;
|
|
1003
1399
|
if (res.errorsWhileComputingFlags) {
|
|
@@ -1071,14 +1467,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1071
1467
|
}
|
|
1072
1468
|
return payloads;
|
|
1073
1469
|
};
|
|
1074
|
-
PostHogCore.prototype._parsePayload = function (response) {
|
|
1075
|
-
try {
|
|
1076
|
-
return JSON.parse(response);
|
|
1077
|
-
}
|
|
1078
|
-
catch (_a) {
|
|
1079
|
-
return response;
|
|
1080
|
-
}
|
|
1081
|
-
};
|
|
1082
1470
|
PostHogCore.prototype.getFeatureFlags = function () {
|
|
1083
1471
|
var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
1084
1472
|
var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
|
|
@@ -1111,13 +1499,27 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1111
1499
|
}
|
|
1112
1500
|
return !!response;
|
|
1113
1501
|
};
|
|
1502
|
+
// Used when we want to trigger the reload but we don't care about the result
|
|
1503
|
+
PostHogCore.prototype.reloadFeatureFlags = function (cb) {
|
|
1504
|
+
this.decideAsync()
|
|
1505
|
+
.then(function (res) {
|
|
1506
|
+
cb === null || cb === void 0 ? void 0 : cb(undefined, res === null || res === void 0 ? void 0 : res.featureFlags);
|
|
1507
|
+
})
|
|
1508
|
+
.catch(function (e) {
|
|
1509
|
+
cb === null || cb === void 0 ? void 0 : cb(e, undefined);
|
|
1510
|
+
if (!cb) {
|
|
1511
|
+
console.log('[PostHog] Error reloading feature flags', e);
|
|
1512
|
+
}
|
|
1513
|
+
});
|
|
1514
|
+
};
|
|
1114
1515
|
PostHogCore.prototype.reloadFeatureFlagsAsync = function (sendAnonDistinctId) {
|
|
1516
|
+
var _a;
|
|
1115
1517
|
if (sendAnonDistinctId === void 0) { sendAnonDistinctId = true; }
|
|
1116
1518
|
return __awaiter(this, void 0, void 0, function () {
|
|
1117
|
-
return __generator(this, function (
|
|
1118
|
-
switch (
|
|
1519
|
+
return __generator(this, function (_b) {
|
|
1520
|
+
switch (_b.label) {
|
|
1119
1521
|
case 0: return [4 /*yield*/, this.decideAsync(sendAnonDistinctId)];
|
|
1120
|
-
case 1: return [2 /*return*/, (_a.sent()).featureFlags];
|
|
1522
|
+
case 1: return [2 /*return*/, (_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.featureFlags];
|
|
1121
1523
|
}
|
|
1122
1524
|
});
|
|
1123
1525
|
});
|
|
@@ -1154,134 +1556,10 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1154
1556
|
}
|
|
1155
1557
|
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
|
|
1156
1558
|
};
|
|
1157
|
-
/***
|
|
1158
|
-
*** QUEUEING AND FLUSHING
|
|
1159
|
-
***/
|
|
1160
|
-
PostHogCore.prototype.enqueue = function (type, _message, options) {
|
|
1161
|
-
var _this = this;
|
|
1162
|
-
if (this.optedOut) {
|
|
1163
|
-
return;
|
|
1164
|
-
}
|
|
1165
|
-
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() });
|
|
1166
|
-
if (message.distinctId) {
|
|
1167
|
-
message.distinct_id = message.distinctId;
|
|
1168
|
-
delete message.distinctId;
|
|
1169
|
-
}
|
|
1170
|
-
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1171
|
-
queue.push({ message: message });
|
|
1172
|
-
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1173
|
-
this._events.emit(type, message);
|
|
1174
|
-
// Flush queued events if we meet the flushAt length
|
|
1175
|
-
if (queue.length >= this.flushAt) {
|
|
1176
|
-
this.flush();
|
|
1177
|
-
}
|
|
1178
|
-
if (this.flushInterval && !this._flushTimer) {
|
|
1179
|
-
this._flushTimer = safeSetTimeout(function () { return _this.flush(); }, this.flushInterval);
|
|
1180
|
-
}
|
|
1181
|
-
};
|
|
1182
|
-
PostHogCore.prototype.flushAsync = function () {
|
|
1183
|
-
var _this = this;
|
|
1184
|
-
return new Promise(function (resolve, reject) {
|
|
1185
|
-
_this.flush(function (err, data) {
|
|
1186
|
-
return err ? reject(err) : resolve(data);
|
|
1187
|
-
});
|
|
1188
|
-
});
|
|
1189
|
-
};
|
|
1190
|
-
PostHogCore.prototype.flush = function (callback) {
|
|
1191
|
-
var _this = this;
|
|
1192
|
-
if (this.optedOut) {
|
|
1193
|
-
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1194
|
-
}
|
|
1195
|
-
if (this._flushTimer) {
|
|
1196
|
-
clearTimeout(this._flushTimer);
|
|
1197
|
-
this._flushTimer = null;
|
|
1198
|
-
}
|
|
1199
|
-
var queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1200
|
-
if (!queue.length) {
|
|
1201
|
-
return callback === null || callback === void 0 ? void 0 : callback();
|
|
1202
|
-
}
|
|
1203
|
-
var items = queue.splice(0, this.flushAt);
|
|
1204
|
-
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1205
|
-
var messages = items.map(function (item) { return item.message; });
|
|
1206
|
-
var data = {
|
|
1207
|
-
api_key: this.apiKey,
|
|
1208
|
-
batch: messages,
|
|
1209
|
-
sent_at: currentISOTime(),
|
|
1210
|
-
};
|
|
1211
|
-
var done = function (err) {
|
|
1212
|
-
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1213
|
-
_this._events.emit('flush', messages);
|
|
1214
|
-
};
|
|
1215
|
-
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1216
|
-
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1217
|
-
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1218
|
-
// but browsers such as Chrome and Safari have not caught up.
|
|
1219
|
-
this.getCustomUserAgent();
|
|
1220
|
-
var payload = JSON.stringify(data);
|
|
1221
|
-
var url = this.captureMode === 'form'
|
|
1222
|
-
? "".concat(this.host, "/e/?ip=1&_=").concat(currentTimestamp(), "&v=").concat(this.getLibraryVersion())
|
|
1223
|
-
: "".concat(this.host, "/batch/");
|
|
1224
|
-
var fetchOptions = this.captureMode === 'form'
|
|
1225
|
-
? {
|
|
1226
|
-
method: 'POST',
|
|
1227
|
-
mode: 'no-cors',
|
|
1228
|
-
credentials: 'omit',
|
|
1229
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1230
|
-
body: "data=".concat(encodeURIComponent(LZString.compressToBase64(payload)), "&compression=lz64"),
|
|
1231
|
-
}
|
|
1232
|
-
: {
|
|
1233
|
-
method: 'POST',
|
|
1234
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1235
|
-
body: payload,
|
|
1236
|
-
};
|
|
1237
|
-
this.fetchWithRetry(url, fetchOptions)
|
|
1238
|
-
.then(function () { return done(); })
|
|
1239
|
-
.catch(function (err) {
|
|
1240
|
-
if (err.response) {
|
|
1241
|
-
var error = new Error(err.response.statusText);
|
|
1242
|
-
return done(error);
|
|
1243
|
-
}
|
|
1244
|
-
done(err);
|
|
1245
|
-
});
|
|
1246
|
-
};
|
|
1247
|
-
PostHogCore.prototype.fetchWithRetry = function (url, options, retryOptions) {
|
|
1248
|
-
var _a;
|
|
1249
|
-
var _b;
|
|
1250
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1251
|
-
var _this = this;
|
|
1252
|
-
return __generator(this, function (_c) {
|
|
1253
|
-
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1254
|
-
var ctrl = new AbortController();
|
|
1255
|
-
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1256
|
-
return ctrl.signal;
|
|
1257
|
-
});
|
|
1258
|
-
return [2 /*return*/, retriable(function () {
|
|
1259
|
-
return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
|
|
1260
|
-
}, retryOptions || this._retryOptions)];
|
|
1261
|
-
});
|
|
1262
|
-
});
|
|
1263
|
-
};
|
|
1264
|
-
PostHogCore.prototype.shutdownAsync = function () {
|
|
1265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1266
|
-
return __generator(this, function (_a) {
|
|
1267
|
-
switch (_a.label) {
|
|
1268
|
-
case 0:
|
|
1269
|
-
clearTimeout(this._flushTimer);
|
|
1270
|
-
return [4 /*yield*/, this.flushAsync()];
|
|
1271
|
-
case 1:
|
|
1272
|
-
_a.sent();
|
|
1273
|
-
return [2 /*return*/];
|
|
1274
|
-
}
|
|
1275
|
-
});
|
|
1276
|
-
});
|
|
1277
|
-
};
|
|
1278
|
-
PostHogCore.prototype.shutdown = function () {
|
|
1279
|
-
void this.shutdownAsync();
|
|
1280
|
-
};
|
|
1281
1559
|
return PostHogCore;
|
|
1282
|
-
}());
|
|
1560
|
+
}(PostHogCoreStateless));
|
|
1283
1561
|
|
|
1284
|
-
var version = "2.
|
|
1562
|
+
var version = "2.3.0";
|
|
1285
1563
|
|
|
1286
1564
|
function getContext(window) {
|
|
1287
1565
|
var context = {};
|