posthog-js-lite 2.2.1 → 2.4.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 +13 -0
- package/lib/index.cjs.js +146 -55
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +12 -7
- package/lib/index.esm.js +146 -55
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +9 -6
- 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/src/posthog-web.ts +4 -0
- package/test/posthog-web.spec.ts +72 -10
- package/tsconfig.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ declare type PosthogCoreOptions = {
|
|
|
16
16
|
requestTimeout?: number;
|
|
17
17
|
sessionExpirationTimeSeconds?: number;
|
|
18
18
|
captureMode?: 'json' | 'form';
|
|
19
|
+
disableGeoip?: boolean;
|
|
19
20
|
};
|
|
20
21
|
declare enum PostHogPersistedProperty {
|
|
21
22
|
AnonymousId = "anonymous_id",
|
|
@@ -43,6 +44,7 @@ declare type PostHogFetchOptions = {
|
|
|
43
44
|
};
|
|
44
45
|
declare type PosthogCaptureOptions = {
|
|
45
46
|
timestamp?: Date;
|
|
47
|
+
disableGeoip?: boolean;
|
|
46
48
|
};
|
|
47
49
|
declare type PostHogFetchResponse = {
|
|
48
50
|
status: number;
|
|
@@ -88,7 +90,7 @@ declare type JsonType = string | number | boolean | null | {
|
|
|
88
90
|
interface RetriableOptions {
|
|
89
91
|
retryCount?: number;
|
|
90
92
|
retryDelay?: number;
|
|
91
|
-
retryCheck?: (err: any) =>
|
|
93
|
+
retryCheck?: (err: any) => boolean;
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
declare class SimpleEventEmitter {
|
|
@@ -108,6 +110,9 @@ declare abstract class PostHogCoreStateless {
|
|
|
108
110
|
private requestTimeout;
|
|
109
111
|
private captureMode;
|
|
110
112
|
private removeDebugCallback?;
|
|
113
|
+
private debugMode;
|
|
114
|
+
private pendingPromises;
|
|
115
|
+
private disableGeoip;
|
|
111
116
|
private _optoutOverride;
|
|
112
117
|
protected _events: SimpleEventEmitter;
|
|
113
118
|
protected _flushTimer?: any;
|
|
@@ -135,7 +140,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
135
140
|
}, options?: PosthogCaptureOptions): this;
|
|
136
141
|
protected aliasStateless(alias: string, distinctId: string, properties?: {
|
|
137
142
|
[key: string]: any;
|
|
138
|
-
}): this;
|
|
143
|
+
}, options?: PosthogCaptureOptions): this;
|
|
139
144
|
/***
|
|
140
145
|
*** GROUPS
|
|
141
146
|
***/
|
|
@@ -144,12 +149,12 @@ declare abstract class PostHogCoreStateless {
|
|
|
144
149
|
*** FEATURE FLAGS
|
|
145
150
|
***/
|
|
146
151
|
protected getDecide(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>): Promise<PostHogDecideResponse | undefined>;
|
|
147
|
-
protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
148
|
-
protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
149
|
-
protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
152
|
+
protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<boolean | string | undefined>;
|
|
153
|
+
protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<JsonType | undefined>;
|
|
154
|
+
protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
|
|
150
155
|
protected _parsePayload(response: any): any;
|
|
151
|
-
protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
152
|
-
protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
156
|
+
protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
|
|
157
|
+
protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
|
|
153
158
|
flags: PostHogDecideResponse['featureFlags'] | undefined;
|
|
154
159
|
payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
|
|
155
160
|
}>;
|
package/lib/index.esm.js
CHANGED
|
@@ -176,29 +176,31 @@ function retriable(fn, props) {
|
|
|
176
176
|
i = 0;
|
|
177
177
|
_d.label = 1;
|
|
178
178
|
case 1:
|
|
179
|
-
if (!(i < retryCount + 1)) return [3 /*break*/,
|
|
180
|
-
|
|
179
|
+
if (!(i < retryCount + 1)) return [3 /*break*/, 7];
|
|
180
|
+
if (!(i > 0)) return [3 /*break*/, 3];
|
|
181
|
+
// don't wait when it's the last try
|
|
182
|
+
return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
|
|
181
183
|
case 2:
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
// don't wait when it's the last try
|
|
185
|
+
_d.sent();
|
|
186
|
+
_d.label = 3;
|
|
184
187
|
case 3:
|
|
188
|
+
_d.trys.push([3, 5, , 6]);
|
|
189
|
+
return [4 /*yield*/, fn()];
|
|
190
|
+
case 4:
|
|
185
191
|
res = _d.sent();
|
|
186
192
|
return [2 /*return*/, res];
|
|
187
|
-
case
|
|
193
|
+
case 5:
|
|
188
194
|
e_1 = _d.sent();
|
|
189
195
|
lastError = e_1;
|
|
190
196
|
if (!retryCheck(e_1)) {
|
|
191
197
|
throw e_1;
|
|
192
198
|
}
|
|
193
|
-
return [3 /*break*/,
|
|
194
|
-
case 5: return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
|
|
199
|
+
return [3 /*break*/, 6];
|
|
195
200
|
case 6:
|
|
196
|
-
_d.sent();
|
|
197
|
-
_d.label = 7;
|
|
198
|
-
case 7:
|
|
199
201
|
i++;
|
|
200
202
|
return [3 /*break*/, 1];
|
|
201
|
-
case
|
|
203
|
+
case 7: throw lastError;
|
|
202
204
|
}
|
|
203
205
|
});
|
|
204
206
|
});
|
|
@@ -697,9 +699,35 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
697
699
|
return SimpleEventEmitter;
|
|
698
700
|
}());
|
|
699
701
|
|
|
702
|
+
var PostHogFetchHttpError = /** @class */ (function (_super) {
|
|
703
|
+
__extends(PostHogFetchHttpError, _super);
|
|
704
|
+
function PostHogFetchHttpError(response) {
|
|
705
|
+
var _this = _super.call(this, 'HTTP error while fetching PostHog: ' + response.status) || this;
|
|
706
|
+
_this.response = response;
|
|
707
|
+
_this.name = 'PostHogFetchHttpError';
|
|
708
|
+
return _this;
|
|
709
|
+
}
|
|
710
|
+
return PostHogFetchHttpError;
|
|
711
|
+
}(Error));
|
|
712
|
+
var PostHogFetchNetworkError = /** @class */ (function (_super) {
|
|
713
|
+
__extends(PostHogFetchNetworkError, _super);
|
|
714
|
+
function PostHogFetchNetworkError(error) {
|
|
715
|
+
var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
|
|
716
|
+
_this.error = error;
|
|
717
|
+
_this.name = 'PostHogFetchNetworkError';
|
|
718
|
+
return _this;
|
|
719
|
+
}
|
|
720
|
+
return PostHogFetchNetworkError;
|
|
721
|
+
}(Error));
|
|
722
|
+
function isPostHogFetchError(err) {
|
|
723
|
+
return typeof err === 'object' && (err.name === 'PostHogFetchHttpError' || err.name === 'PostHogFetchNetworkError');
|
|
724
|
+
}
|
|
700
725
|
var PostHogCoreStateless = /** @class */ (function () {
|
|
701
726
|
function PostHogCoreStateless(apiKey, options) {
|
|
702
|
-
var _a, _b, _c, _d;
|
|
727
|
+
var _a, _b, _c, _d, _e;
|
|
728
|
+
this.debugMode = false;
|
|
729
|
+
this.pendingPromises = {};
|
|
730
|
+
this.disableGeoip = true;
|
|
703
731
|
// internal
|
|
704
732
|
this._events = new SimpleEventEmitter();
|
|
705
733
|
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
@@ -713,8 +741,10 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
713
741
|
this._retryOptions = {
|
|
714
742
|
retryCount: (_b = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _b !== void 0 ? _b : 3,
|
|
715
743
|
retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
|
|
744
|
+
retryCheck: isPostHogFetchError,
|
|
716
745
|
};
|
|
717
746
|
this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
|
|
747
|
+
this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
|
|
718
748
|
}
|
|
719
749
|
PostHogCoreStateless.prototype.getCommonEventProperties = function () {
|
|
720
750
|
return {
|
|
@@ -743,6 +773,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
743
773
|
var _a;
|
|
744
774
|
if (enabled === void 0) { enabled = true; }
|
|
745
775
|
(_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
776
|
+
this.debugMode = enabled;
|
|
746
777
|
if (enabled) {
|
|
747
778
|
this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
|
|
748
779
|
}
|
|
@@ -773,13 +804,13 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
773
804
|
this.enqueue('capture', payload, options);
|
|
774
805
|
return this;
|
|
775
806
|
};
|
|
776
|
-
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties) {
|
|
807
|
+
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties, options) {
|
|
777
808
|
var payload = this.buildPayload({
|
|
778
809
|
event: '$create_alias',
|
|
779
810
|
distinct_id: distinctId,
|
|
780
811
|
properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
|
|
781
812
|
});
|
|
782
|
-
this.enqueue('alias', payload);
|
|
813
|
+
this.enqueue('alias', payload, options);
|
|
783
814
|
return this;
|
|
784
815
|
};
|
|
785
816
|
/***
|
|
@@ -820,7 +851,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
820
851
|
});
|
|
821
852
|
});
|
|
822
853
|
};
|
|
823
|
-
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
854
|
+
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
824
855
|
if (groups === void 0) { groups = {}; }
|
|
825
856
|
if (personProperties === void 0) { personProperties = {}; }
|
|
826
857
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -828,7 +859,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
828
859
|
var featureFlags, response;
|
|
829
860
|
return __generator(this, function (_a) {
|
|
830
861
|
switch (_a.label) {
|
|
831
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
862
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
832
863
|
case 1:
|
|
833
864
|
featureFlags = _a.sent();
|
|
834
865
|
if (!featureFlags) {
|
|
@@ -847,7 +878,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
847
878
|
});
|
|
848
879
|
});
|
|
849
880
|
};
|
|
850
|
-
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
881
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
851
882
|
if (groups === void 0) { groups = {}; }
|
|
852
883
|
if (personProperties === void 0) { personProperties = {}; }
|
|
853
884
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -855,7 +886,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
855
886
|
var payloads, response;
|
|
856
887
|
return __generator(this, function (_a) {
|
|
857
888
|
switch (_a.label) {
|
|
858
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
889
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
859
890
|
case 1:
|
|
860
891
|
payloads = _a.sent();
|
|
861
892
|
if (!payloads) {
|
|
@@ -871,7 +902,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
871
902
|
});
|
|
872
903
|
});
|
|
873
904
|
};
|
|
874
|
-
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
905
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
875
906
|
if (groups === void 0) { groups = {}; }
|
|
876
907
|
if (personProperties === void 0) { personProperties = {}; }
|
|
877
908
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -880,7 +911,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
880
911
|
var _this = this;
|
|
881
912
|
return __generator(this, function (_a) {
|
|
882
913
|
switch (_a.label) {
|
|
883
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
914
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
884
915
|
case 1:
|
|
885
916
|
payloads = (_a.sent()).payloads;
|
|
886
917
|
if (payloads) {
|
|
@@ -902,28 +933,33 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
902
933
|
return response;
|
|
903
934
|
}
|
|
904
935
|
};
|
|
905
|
-
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
936
|
+
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
906
937
|
if (groups === void 0) { groups = {}; }
|
|
907
938
|
if (personProperties === void 0) { personProperties = {}; }
|
|
908
939
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
909
940
|
return __awaiter(this, void 0, void 0, function () {
|
|
910
941
|
return __generator(this, function (_a) {
|
|
911
942
|
switch (_a.label) {
|
|
912
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
943
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
913
944
|
case 1: return [2 /*return*/, (_a.sent()).flags];
|
|
914
945
|
}
|
|
915
946
|
});
|
|
916
947
|
});
|
|
917
948
|
};
|
|
918
|
-
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
949
|
+
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
919
950
|
if (groups === void 0) { groups = {}; }
|
|
920
951
|
if (personProperties === void 0) { personProperties = {}; }
|
|
921
952
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
922
953
|
return __awaiter(this, void 0, void 0, function () {
|
|
923
|
-
var decideResponse, flags, payloads;
|
|
954
|
+
var extraPayload, decideResponse, flags, payloads;
|
|
924
955
|
return __generator(this, function (_a) {
|
|
925
956
|
switch (_a.label) {
|
|
926
|
-
case 0:
|
|
957
|
+
case 0:
|
|
958
|
+
extraPayload = {};
|
|
959
|
+
if (disableGeoip !== null && disableGeoip !== void 0 ? disableGeoip : this.disableGeoip) {
|
|
960
|
+
extraPayload['geoip_disable'] = true;
|
|
961
|
+
}
|
|
962
|
+
return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload)];
|
|
927
963
|
case 1:
|
|
928
964
|
decideResponse = _a.sent();
|
|
929
965
|
flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
|
|
@@ -941,11 +977,19 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
941
977
|
***/
|
|
942
978
|
PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
|
|
943
979
|
var _this = this;
|
|
980
|
+
var _a;
|
|
944
981
|
if (this.optedOut) {
|
|
945
982
|
this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
|
|
946
983
|
return;
|
|
947
984
|
}
|
|
948
985
|
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() });
|
|
986
|
+
var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
|
|
987
|
+
if (addGeoipDisableProperty) {
|
|
988
|
+
if (!message.properties) {
|
|
989
|
+
message.properties = {};
|
|
990
|
+
}
|
|
991
|
+
message['properties']['$geoip_disable'] = true;
|
|
992
|
+
}
|
|
949
993
|
if (message.distinctId) {
|
|
950
994
|
message.distinct_id = message.distinctId;
|
|
951
995
|
delete message.distinctId;
|
|
@@ -988,8 +1032,14 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
988
1032
|
batch: messages,
|
|
989
1033
|
sent_at: currentISOTime(),
|
|
990
1034
|
};
|
|
1035
|
+
var promiseUUID = generateUUID();
|
|
991
1036
|
var done = function (err) {
|
|
1037
|
+
if (err) {
|
|
1038
|
+
_this._events.emit('error', err);
|
|
1039
|
+
}
|
|
992
1040
|
callback === null || callback === void 0 ? void 0 : callback(err, messages);
|
|
1041
|
+
// remove promise from pendingPromises
|
|
1042
|
+
delete _this.pendingPromises[promiseUUID];
|
|
993
1043
|
_this._events.emit('flush', messages);
|
|
994
1044
|
};
|
|
995
1045
|
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
@@ -1014,13 +1064,11 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
1014
1064
|
headers: { 'Content-Type': 'application/json' },
|
|
1015
1065
|
body: payload,
|
|
1016
1066
|
};
|
|
1017
|
-
this.fetchWithRetry(url, fetchOptions)
|
|
1067
|
+
var requestPromise = this.fetchWithRetry(url, fetchOptions);
|
|
1068
|
+
this.pendingPromises[promiseUUID] = requestPromise;
|
|
1069
|
+
requestPromise
|
|
1018
1070
|
.then(function () { return done(); })
|
|
1019
1071
|
.catch(function (err) {
|
|
1020
|
-
if (err.response) {
|
|
1021
|
-
var error = new Error(err.response.statusText);
|
|
1022
|
-
return done(error);
|
|
1023
|
-
}
|
|
1024
1072
|
done(err);
|
|
1025
1073
|
});
|
|
1026
1074
|
};
|
|
@@ -1030,27 +1078,72 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
1030
1078
|
return __awaiter(this, void 0, void 0, function () {
|
|
1031
1079
|
var _this = this;
|
|
1032
1080
|
return __generator(this, function (_c) {
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1081
|
+
switch (_c.label) {
|
|
1082
|
+
case 0:
|
|
1083
|
+
(_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
|
|
1084
|
+
var ctrl = new AbortController();
|
|
1085
|
+
setTimeout(function () { return ctrl.abort(); }, ms);
|
|
1086
|
+
return ctrl.signal;
|
|
1087
|
+
});
|
|
1088
|
+
return [4 /*yield*/, retriable(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1089
|
+
var res, e_1;
|
|
1090
|
+
return __generator(this, function (_a) {
|
|
1091
|
+
switch (_a.label) {
|
|
1092
|
+
case 0:
|
|
1093
|
+
res = null;
|
|
1094
|
+
_a.label = 1;
|
|
1095
|
+
case 1:
|
|
1096
|
+
_a.trys.push([1, 3, , 4]);
|
|
1097
|
+
return [4 /*yield*/, this.fetch(url, __assign({ signal: AbortSignal.timeout(this.requestTimeout) }, options))];
|
|
1098
|
+
case 2:
|
|
1099
|
+
res = _a.sent();
|
|
1100
|
+
return [3 /*break*/, 4];
|
|
1101
|
+
case 3:
|
|
1102
|
+
e_1 = _a.sent();
|
|
1103
|
+
// fetch will only throw on network errors or on timeouts
|
|
1104
|
+
throw new PostHogFetchNetworkError(e_1);
|
|
1105
|
+
case 4:
|
|
1106
|
+
if (res.status < 200 || res.status >= 400) {
|
|
1107
|
+
throw new PostHogFetchHttpError(res);
|
|
1108
|
+
}
|
|
1109
|
+
return [2 /*return*/, res];
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
}); }, __assign(__assign({}, this._retryOptions), retryOptions))];
|
|
1113
|
+
case 1: return [2 /*return*/, _c.sent()];
|
|
1114
|
+
}
|
|
1041
1115
|
});
|
|
1042
1116
|
});
|
|
1043
1117
|
};
|
|
1044
1118
|
PostHogCoreStateless.prototype.shutdownAsync = function () {
|
|
1045
1119
|
return __awaiter(this, void 0, void 0, function () {
|
|
1120
|
+
var e_2;
|
|
1046
1121
|
return __generator(this, function (_a) {
|
|
1047
1122
|
switch (_a.label) {
|
|
1048
1123
|
case 0:
|
|
1049
1124
|
clearTimeout(this._flushTimer);
|
|
1050
|
-
|
|
1125
|
+
_a.label = 1;
|
|
1051
1126
|
case 1:
|
|
1127
|
+
_a.trys.push([1, 4, , 5]);
|
|
1128
|
+
return [4 /*yield*/, this.flushAsync()];
|
|
1129
|
+
case 2:
|
|
1052
1130
|
_a.sent();
|
|
1053
|
-
return [
|
|
1131
|
+
return [4 /*yield*/, Promise.all(Object.values(this.pendingPromises).map(function (x) {
|
|
1132
|
+
return x.catch(function () {
|
|
1133
|
+
// ignore errors as we are shutting down and can't deal with them anyways.
|
|
1134
|
+
});
|
|
1135
|
+
}))];
|
|
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*/];
|
|
1054
1147
|
}
|
|
1055
1148
|
});
|
|
1056
1149
|
});
|
|
@@ -1064,17 +1157,13 @@ var PostHogCore = /** @class */ (function (_super) {
|
|
|
1064
1157
|
__extends(PostHogCore, _super);
|
|
1065
1158
|
function PostHogCore(apiKey, options) {
|
|
1066
1159
|
var _this = this;
|
|
1067
|
-
var _a, _b;
|
|
1068
|
-
|
|
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;
|
|
1069
1164
|
_this.flagCallReported = {};
|
|
1070
|
-
_this.sendFeatureFlagEvent = (
|
|
1071
|
-
_this._sessionExpirationTimeSeconds = (
|
|
1072
|
-
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
1073
|
-
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
1074
|
-
safeSetTimeout(function () {
|
|
1075
|
-
_this.reloadFeatureFlags();
|
|
1076
|
-
}, 1);
|
|
1077
|
-
}
|
|
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
|
|
1078
1167
|
return _this;
|
|
1079
1168
|
}
|
|
1080
1169
|
PostHogCore.prototype.setupBootstrap = function (options) {
|
|
@@ -1189,9 +1278,7 @@ var PostHogCore = /** @class */ (function (_super) {
|
|
|
1189
1278
|
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
1190
1279
|
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
1191
1280
|
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
1192
|
-
|
|
1193
|
-
this.reloadFeatureFlags();
|
|
1194
|
-
}
|
|
1281
|
+
this.reloadFeatureFlags();
|
|
1195
1282
|
}
|
|
1196
1283
|
_super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
|
|
1197
1284
|
return this;
|
|
@@ -1231,7 +1318,7 @@ var PostHogCore = /** @class */ (function (_super) {
|
|
|
1231
1318
|
this.register({
|
|
1232
1319
|
$groups: __assign(__assign({}, existingGroups), groups),
|
|
1233
1320
|
});
|
|
1234
|
-
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })
|
|
1321
|
+
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })) {
|
|
1235
1322
|
this.reloadFeatureFlags();
|
|
1236
1323
|
}
|
|
1237
1324
|
return this;
|
|
@@ -1464,7 +1551,7 @@ var PostHogCore = /** @class */ (function (_super) {
|
|
|
1464
1551
|
return PostHogCore;
|
|
1465
1552
|
}(PostHogCoreStateless));
|
|
1466
1553
|
|
|
1467
|
-
var version = "2.
|
|
1554
|
+
var version = "2.4.0";
|
|
1468
1555
|
|
|
1469
1556
|
function getContext(window) {
|
|
1470
1557
|
var context = {};
|
|
@@ -1829,6 +1916,10 @@ function (_super) {
|
|
|
1829
1916
|
|
|
1830
1917
|
_this.setupBootstrap(options);
|
|
1831
1918
|
|
|
1919
|
+
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
1920
|
+
_this.reloadFeatureFlags();
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1832
1923
|
return _this;
|
|
1833
1924
|
}
|
|
1834
1925
|
|