posthog-node 2.6.0 → 3.1.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/lib/index.d.ts CHANGED
@@ -17,6 +17,7 @@ declare type PosthogCoreOptions = {
17
17
  requestTimeout?: number;
18
18
  sessionExpirationTimeSeconds?: number;
19
19
  captureMode?: 'json' | 'form';
20
+ disableGeoip?: boolean;
20
21
  };
21
22
  declare enum PostHogPersistedProperty {
22
23
  AnonymousId = "anonymous_id",
@@ -44,6 +45,7 @@ declare type PostHogFetchOptions = {
44
45
  };
45
46
  declare type PosthogCaptureOptions = {
46
47
  timestamp?: Date;
48
+ disableGeoip?: boolean;
47
49
  };
48
50
  declare type PostHogFetchResponse = {
49
51
  status: number;
@@ -83,7 +85,7 @@ declare type JsonType = string | number | boolean | null | {
83
85
  interface RetriableOptions {
84
86
  retryCount?: number;
85
87
  retryDelay?: number;
86
- retryCheck?: (err: any) => true;
88
+ retryCheck?: (err: any) => boolean;
87
89
  }
88
90
 
89
91
  declare class SimpleEventEmitter {
@@ -105,6 +107,7 @@ declare abstract class PostHogCoreStateless {
105
107
  private removeDebugCallback?;
106
108
  private debugMode;
107
109
  private pendingPromises;
110
+ private disableGeoip;
108
111
  private _optoutOverride;
109
112
  protected _events: SimpleEventEmitter;
110
113
  protected _flushTimer?: any;
@@ -132,7 +135,7 @@ declare abstract class PostHogCoreStateless {
132
135
  }, options?: PosthogCaptureOptions): this;
133
136
  protected aliasStateless(alias: string, distinctId: string, properties?: {
134
137
  [key: string]: any;
135
- }): this;
138
+ }, options?: PosthogCaptureOptions): this;
136
139
  /***
137
140
  *** GROUPS
138
141
  ***/
@@ -141,12 +144,12 @@ declare abstract class PostHogCoreStateless {
141
144
  *** FEATURE FLAGS
142
145
  ***/
143
146
  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>;
144
- protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<boolean | string | undefined>;
145
- protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<JsonType | undefined>;
146
- protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
147
+ 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>;
148
+ 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>;
149
+ protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
147
150
  protected _parsePayload(response: any): any;
148
- protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
149
- protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<{
151
+ 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>;
152
+ protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
150
153
  flags: PostHogDecideResponse['featureFlags'] | undefined;
151
154
  payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
152
155
  }>;
@@ -164,6 +167,7 @@ declare abstract class PostHogCoreStateless {
164
167
  interface IdentifyMessageV1 {
165
168
  distinctId: string;
166
169
  properties?: Record<string | number, any>;
170
+ disableGeoip?: boolean;
167
171
  }
168
172
  interface EventMessageV1 extends IdentifyMessageV1 {
169
173
  event: string;
@@ -176,6 +180,7 @@ interface GroupIdentifyMessage {
176
180
  groupKey: string;
177
181
  properties?: Record<string | number, any>;
178
182
  distinctId?: string;
183
+ disableGeoip?: boolean;
179
184
  }
180
185
  declare type PostHogNodeV1 = {
181
186
  /**
@@ -321,11 +326,12 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
321
326
  enable(): void;
322
327
  disable(): void;
323
328
  debug(enabled?: boolean): void;
324
- capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp }: EventMessageV1): void;
325
- identify({ distinctId, properties }: IdentifyMessageV1): void;
329
+ capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip }: EventMessageV1): void;
330
+ identify({ distinctId, properties, disableGeoip }: IdentifyMessageV1): void;
326
331
  alias(data: {
327
332
  distinctId: string;
328
333
  alias: string;
334
+ disableGeoip?: boolean;
329
335
  }): void;
330
336
  getFeatureFlag(key: string, distinctId: string, options?: {
331
337
  groups?: Record<string, string>;
@@ -333,6 +339,7 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
333
339
  groupProperties?: Record<string, Record<string, string>>;
334
340
  onlyEvaluateLocally?: boolean;
335
341
  sendFeatureFlagEvents?: boolean;
342
+ disableGeoip?: boolean;
336
343
  }): Promise<string | boolean | undefined>;
337
344
  getFeatureFlagPayload(key: string, distinctId: string, matchValue?: string | boolean, options?: {
338
345
  groups?: Record<string, string>;
@@ -340,6 +347,7 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
340
347
  groupProperties?: Record<string, Record<string, string>>;
341
348
  onlyEvaluateLocally?: boolean;
342
349
  sendFeatureFlagEvents?: boolean;
350
+ disableGeoip?: boolean;
343
351
  }): Promise<JsonType | undefined>;
344
352
  isFeatureEnabled(key: string, distinctId: string, options?: {
345
353
  groups?: Record<string, string>;
@@ -347,20 +355,23 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
347
355
  groupProperties?: Record<string, Record<string, string>>;
348
356
  onlyEvaluateLocally?: boolean;
349
357
  sendFeatureFlagEvents?: boolean;
358
+ disableGeoip?: boolean;
350
359
  }): Promise<boolean | undefined>;
351
360
  getAllFlags(distinctId: string, options?: {
352
361
  groups?: Record<string, string>;
353
362
  personProperties?: Record<string, string>;
354
363
  groupProperties?: Record<string, Record<string, string>>;
355
364
  onlyEvaluateLocally?: boolean;
365
+ disableGeoip?: boolean;
356
366
  }): Promise<Record<string, string | boolean>>;
357
367
  getAllFlagsAndPayloads(distinctId: string, options?: {
358
368
  groups?: Record<string, string>;
359
369
  personProperties?: Record<string, string>;
360
370
  groupProperties?: Record<string, Record<string, string>>;
361
371
  onlyEvaluateLocally?: boolean;
372
+ disableGeoip?: boolean;
362
373
  }): Promise<PosthogFlagsAndPayloadsResponse>;
363
- groupIdentify({ groupType, groupKey, properties, distinctId }: GroupIdentifyMessage): void;
374
+ groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }: GroupIdentifyMessage): void;
364
375
  reloadFeatureFlags(): Promise<void>;
365
376
  shutdown(): void;
366
377
  shutdownAsync(): Promise<void>;
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.6.0";
158
+ var version = "3.1.0";
159
159
 
160
160
  var PostHogPersistedProperty;
161
161
  (function (PostHogPersistedProperty) {
@@ -193,29 +193,31 @@ function retriable(fn, props) {
193
193
  i = 0;
194
194
  _d.label = 1;
195
195
  case 1:
196
- if (!(i < retryCount + 1)) return [3 /*break*/, 8];
197
- _d.label = 2;
196
+ if (!(i < retryCount + 1)) return [3 /*break*/, 7];
197
+ if (!(i > 0)) return [3 /*break*/, 3];
198
+ // don't wait when it's the last try
199
+ return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
198
200
  case 2:
199
- _d.trys.push([2, 4, , 5]);
200
- return [4 /*yield*/, fn()];
201
+ // don't wait when it's the last try
202
+ _d.sent();
203
+ _d.label = 3;
201
204
  case 3:
205
+ _d.trys.push([3, 5, , 6]);
206
+ return [4 /*yield*/, fn()];
207
+ case 4:
202
208
  res = _d.sent();
203
209
  return [2 /*return*/, res];
204
- case 4:
210
+ case 5:
205
211
  e_1 = _d.sent();
206
212
  lastError = e_1;
207
213
  if (!retryCheck(e_1)) {
208
214
  throw e_1;
209
215
  }
210
- return [3 /*break*/, 5];
211
- case 5: return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
216
+ return [3 /*break*/, 6];
212
217
  case 6:
213
- _d.sent();
214
- _d.label = 7;
215
- case 7:
216
218
  i++;
217
219
  return [3 /*break*/, 1];
218
- case 8: throw lastError;
220
+ case 7: throw lastError;
219
221
  }
220
222
  });
221
223
  });
@@ -714,11 +716,35 @@ var SimpleEventEmitter = /** @class */ (function () {
714
716
  return SimpleEventEmitter;
715
717
  }());
716
718
 
719
+ var PostHogFetchHttpError = /** @class */ (function (_super) {
720
+ __extends(PostHogFetchHttpError, _super);
721
+ function PostHogFetchHttpError(response) {
722
+ var _this = _super.call(this, 'HTTP error while fetching PostHog: ' + response.status) || this;
723
+ _this.response = response;
724
+ _this.name = 'PostHogFetchHttpError';
725
+ return _this;
726
+ }
727
+ return PostHogFetchHttpError;
728
+ }(Error));
729
+ var PostHogFetchNetworkError = /** @class */ (function (_super) {
730
+ __extends(PostHogFetchNetworkError, _super);
731
+ function PostHogFetchNetworkError(error) {
732
+ var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
733
+ _this.error = error;
734
+ _this.name = 'PostHogFetchNetworkError';
735
+ return _this;
736
+ }
737
+ return PostHogFetchNetworkError;
738
+ }(Error));
739
+ function isPostHogFetchError(err) {
740
+ return typeof err === 'object' && (err.name === 'PostHogFetchHttpError' || err.name === 'PostHogFetchNetworkError');
741
+ }
717
742
  var PostHogCoreStateless = /** @class */ (function () {
718
743
  function PostHogCoreStateless(apiKey, options) {
719
- var _a, _b, _c, _d;
744
+ var _a, _b, _c, _d, _e;
720
745
  this.debugMode = false;
721
746
  this.pendingPromises = {};
747
+ this.disableGeoip = true;
722
748
  // internal
723
749
  this._events = new SimpleEventEmitter();
724
750
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -732,8 +758,10 @@ var PostHogCoreStateless = /** @class */ (function () {
732
758
  this._retryOptions = {
733
759
  retryCount: (_b = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _b !== void 0 ? _b : 3,
734
760
  retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
761
+ retryCheck: isPostHogFetchError,
735
762
  };
736
763
  this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
764
+ this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
737
765
  }
738
766
  PostHogCoreStateless.prototype.getCommonEventProperties = function () {
739
767
  return {
@@ -793,13 +821,13 @@ var PostHogCoreStateless = /** @class */ (function () {
793
821
  this.enqueue('capture', payload, options);
794
822
  return this;
795
823
  };
796
- PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties) {
824
+ PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties, options) {
797
825
  var payload = this.buildPayload({
798
826
  event: '$create_alias',
799
827
  distinct_id: distinctId,
800
828
  properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
801
829
  });
802
- this.enqueue('alias', payload);
830
+ this.enqueue('alias', payload, options);
803
831
  return this;
804
832
  };
805
833
  /***
@@ -840,7 +868,7 @@ var PostHogCoreStateless = /** @class */ (function () {
840
868
  });
841
869
  });
842
870
  };
843
- PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties) {
871
+ PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
844
872
  if (groups === void 0) { groups = {}; }
845
873
  if (personProperties === void 0) { personProperties = {}; }
846
874
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -848,7 +876,7 @@ var PostHogCoreStateless = /** @class */ (function () {
848
876
  var featureFlags, response;
849
877
  return __generator(this, function (_a) {
850
878
  switch (_a.label) {
851
- case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties)];
879
+ case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
852
880
  case 1:
853
881
  featureFlags = _a.sent();
854
882
  if (!featureFlags) {
@@ -867,7 +895,7 @@ var PostHogCoreStateless = /** @class */ (function () {
867
895
  });
868
896
  });
869
897
  };
870
- PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties) {
898
+ PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
871
899
  if (groups === void 0) { groups = {}; }
872
900
  if (personProperties === void 0) { personProperties = {}; }
873
901
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -875,7 +903,7 @@ var PostHogCoreStateless = /** @class */ (function () {
875
903
  var payloads, response;
876
904
  return __generator(this, function (_a) {
877
905
  switch (_a.label) {
878
- case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
906
+ case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
879
907
  case 1:
880
908
  payloads = _a.sent();
881
909
  if (!payloads) {
@@ -891,7 +919,7 @@ var PostHogCoreStateless = /** @class */ (function () {
891
919
  });
892
920
  });
893
921
  };
894
- PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
922
+ PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
895
923
  if (groups === void 0) { groups = {}; }
896
924
  if (personProperties === void 0) { personProperties = {}; }
897
925
  if (groupProperties === void 0) { groupProperties = {}; }
@@ -900,7 +928,7 @@ var PostHogCoreStateless = /** @class */ (function () {
900
928
  var _this = this;
901
929
  return __generator(this, function (_a) {
902
930
  switch (_a.label) {
903
- case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
931
+ case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
904
932
  case 1:
905
933
  payloads = (_a.sent()).payloads;
906
934
  if (payloads) {
@@ -922,28 +950,33 @@ var PostHogCoreStateless = /** @class */ (function () {
922
950
  return response;
923
951
  }
924
952
  };
925
- PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties) {
953
+ PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
926
954
  if (groups === void 0) { groups = {}; }
927
955
  if (personProperties === void 0) { personProperties = {}; }
928
956
  if (groupProperties === void 0) { groupProperties = {}; }
929
957
  return __awaiter(this, void 0, void 0, function () {
930
958
  return __generator(this, function (_a) {
931
959
  switch (_a.label) {
932
- case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
960
+ case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
933
961
  case 1: return [2 /*return*/, (_a.sent()).flags];
934
962
  }
935
963
  });
936
964
  });
937
965
  };
938
- PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
966
+ PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
939
967
  if (groups === void 0) { groups = {}; }
940
968
  if (personProperties === void 0) { personProperties = {}; }
941
969
  if (groupProperties === void 0) { groupProperties = {}; }
942
970
  return __awaiter(this, void 0, void 0, function () {
943
- var decideResponse, flags, payloads;
971
+ var extraPayload, decideResponse, flags, payloads;
944
972
  return __generator(this, function (_a) {
945
973
  switch (_a.label) {
946
- case 0: return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties)];
974
+ case 0:
975
+ extraPayload = {};
976
+ if (disableGeoip !== null && disableGeoip !== void 0 ? disableGeoip : this.disableGeoip) {
977
+ extraPayload['geoip_disable'] = true;
978
+ }
979
+ return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload)];
947
980
  case 1:
948
981
  decideResponse = _a.sent();
949
982
  flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
@@ -961,11 +994,19 @@ var PostHogCoreStateless = /** @class */ (function () {
961
994
  ***/
962
995
  PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
963
996
  var _this = this;
997
+ var _a;
964
998
  if (this.optedOut) {
965
999
  this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
966
1000
  return;
967
1001
  }
968
1002
  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() });
1003
+ var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
1004
+ if (addGeoipDisableProperty) {
1005
+ if (!message.properties) {
1006
+ message.properties = {};
1007
+ }
1008
+ message['properties']['$geoip_disable'] = true;
1009
+ }
969
1010
  if (message.distinctId) {
970
1011
  message.distinct_id = message.distinctId;
971
1012
  delete message.distinctId;
@@ -1010,6 +1051,9 @@ var PostHogCoreStateless = /** @class */ (function () {
1010
1051
  };
1011
1052
  var promiseUUID = generateUUID();
1012
1053
  var done = function (err) {
1054
+ if (err) {
1055
+ _this._events.emit('error', err);
1056
+ }
1013
1057
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1014
1058
  // remove promise from pendingPromises
1015
1059
  delete _this.pendingPromises[promiseUUID];
@@ -1042,10 +1086,6 @@ var PostHogCoreStateless = /** @class */ (function () {
1042
1086
  requestPromise
1043
1087
  .then(function () { return done(); })
1044
1088
  .catch(function (err) {
1045
- if (err.response) {
1046
- var error = new Error(err.response.statusText);
1047
- return done(error);
1048
- }
1049
1089
  done(err);
1050
1090
  });
1051
1091
  };
@@ -1055,30 +1095,68 @@ var PostHogCoreStateless = /** @class */ (function () {
1055
1095
  return __awaiter(this, void 0, void 0, function () {
1056
1096
  var _this = this;
1057
1097
  return __generator(this, function (_c) {
1058
- (_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
1059
- var ctrl = new AbortController();
1060
- setTimeout(function () { return ctrl.abort(); }, ms);
1061
- return ctrl.signal;
1062
- });
1063
- return [2 /*return*/, retriable(function () {
1064
- return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
1065
- }, retryOptions || this._retryOptions)];
1098
+ switch (_c.label) {
1099
+ case 0:
1100
+ (_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
1101
+ var ctrl = new AbortController();
1102
+ setTimeout(function () { return ctrl.abort(); }, ms);
1103
+ return ctrl.signal;
1104
+ });
1105
+ return [4 /*yield*/, retriable(function () { return __awaiter(_this, void 0, void 0, function () {
1106
+ var res, e_1;
1107
+ return __generator(this, function (_a) {
1108
+ switch (_a.label) {
1109
+ case 0:
1110
+ res = null;
1111
+ _a.label = 1;
1112
+ case 1:
1113
+ _a.trys.push([1, 3, , 4]);
1114
+ return [4 /*yield*/, this.fetch(url, __assign({ signal: AbortSignal.timeout(this.requestTimeout) }, options))];
1115
+ case 2:
1116
+ res = _a.sent();
1117
+ return [3 /*break*/, 4];
1118
+ case 3:
1119
+ e_1 = _a.sent();
1120
+ // fetch will only throw on network errors or on timeouts
1121
+ throw new PostHogFetchNetworkError(e_1);
1122
+ case 4:
1123
+ if (res.status < 200 || res.status >= 400) {
1124
+ throw new PostHogFetchHttpError(res);
1125
+ }
1126
+ return [2 /*return*/, res];
1127
+ }
1128
+ });
1129
+ }); }, __assign(__assign({}, this._retryOptions), retryOptions))];
1130
+ case 1: return [2 /*return*/, _c.sent()];
1131
+ }
1066
1132
  });
1067
1133
  });
1068
1134
  };
1069
1135
  PostHogCoreStateless.prototype.shutdownAsync = function () {
1070
1136
  return __awaiter(this, void 0, void 0, function () {
1137
+ var e_2;
1071
1138
  return __generator(this, function (_a) {
1072
1139
  switch (_a.label) {
1073
1140
  case 0:
1074
1141
  clearTimeout(this._flushTimer);
1075
- return [4 /*yield*/, this.flushAsync()];
1142
+ _a.label = 1;
1076
1143
  case 1:
1144
+ _a.trys.push([1, 4, , 5]);
1145
+ return [4 /*yield*/, this.flushAsync()];
1146
+ case 2:
1077
1147
  _a.sent();
1078
1148
  return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
1079
- case 2:
1149
+ case 3:
1080
1150
  _a.sent();
1081
- return [2 /*return*/];
1151
+ return [3 /*break*/, 5];
1152
+ case 4:
1153
+ e_2 = _a.sent();
1154
+ if (!isPostHogFetchError(e_2)) {
1155
+ throw e_2;
1156
+ }
1157
+ console.error('Error while shutting down PostHog', e_2);
1158
+ return [3 /*break*/, 5];
1159
+ case 5: return [2 /*return*/];
1082
1160
  }
1083
1161
  });
1084
1162
  });
@@ -1092,11 +1170,13 @@ var PostHogCoreStateless = /** @class */ (function () {
1092
1170
  __extends(PostHogCore, _super);
1093
1171
  function PostHogCore(apiKey, options) {
1094
1172
  var _this = this;
1095
- var _a, _b;
1096
- _this = _super.call(this, apiKey, options) || this;
1173
+ var _a, _b, _c;
1174
+ // Default for stateful mode is to not disable geoip. Only override if explicitly set
1175
+ var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1176
+ _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1097
1177
  _this.flagCallReported = {};
1098
- _this.sendFeatureFlagEvent = (_a = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _a !== void 0 ? _a : true;
1099
- _this._sessionExpirationTimeSeconds = (_b = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _b !== void 0 ? _b : 1800; // 30 minutes
1178
+ _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1179
+ _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1100
1180
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1101
1181
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1102
1182
  safeSetTimeout(function () {
@@ -1520,7 +1600,11 @@ var fetch = function (url, options) {
1520
1600
  headers: options.headers,
1521
1601
  method: options.method.toLowerCase(),
1522
1602
  data: options.body,
1523
- signal: options.signal
1603
+ signal: options.signal,
1604
+ // fetch only throws on network errors, not on HTTP errors
1605
+ validateStatus: function () {
1606
+ return true;
1607
+ }
1524
1608
  })];
1525
1609
 
1526
1610
  case 1:
@@ -1530,10 +1614,22 @@ var fetch = function (url, options) {
1530
1614
  , {
1531
1615
  status: res.status,
1532
1616
  text: function () {
1533
- return res.data;
1617
+ return __awaiter(void 0, void 0, void 0, function () {
1618
+ return __generator(this, function (_a) {
1619
+ return [2
1620
+ /*return*/
1621
+ , res.data];
1622
+ });
1623
+ });
1534
1624
  },
1535
1625
  json: function () {
1536
- return res.data;
1626
+ return __awaiter(void 0, void 0, void 0, function () {
1627
+ return __generator(this, function (_a) {
1628
+ return [2
1629
+ /*return*/
1630
+ , res.data];
1631
+ });
1632
+ });
1537
1633
  }
1538
1634
  }];
1539
1635
  }
@@ -2153,7 +2249,7 @@ function () {
2153
2249
 
2154
2250
  case 3:
2155
2251
  err_2 = _a.sent();
2156
- throw new Error("Request failed with error: ".concat(err_2));
2252
+ throw err_2;
2157
2253
 
2158
2254
  case 4:
2159
2255
  clearTimeout(abortTimeout);
@@ -2483,16 +2579,18 @@ function (_super) {
2483
2579
  properties = _a.properties,
2484
2580
  groups = _a.groups,
2485
2581
  sendFeatureFlags = _a.sendFeatureFlags,
2486
- timestamp = _a.timestamp;
2582
+ timestamp = _a.timestamp,
2583
+ disableGeoip = _a.disableGeoip;
2487
2584
 
2488
2585
  var _capture = function (props) {
2489
2586
  _super.prototype.captureStateless.call(_this, distinctId, event, props, {
2490
- timestamp: timestamp
2587
+ timestamp: timestamp,
2588
+ disableGeoip: disableGeoip
2491
2589
  });
2492
2590
  };
2493
2591
 
2494
2592
  if (sendFeatureFlags) {
2495
- _super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups).then(function (flags) {
2593
+ _super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups, undefined, undefined, disableGeoip).then(function (flags) {
2496
2594
  var featureVariantProperties = {};
2497
2595
 
2498
2596
  if (flags) {
@@ -2528,29 +2626,34 @@ function (_super) {
2528
2626
 
2529
2627
  PostHog.prototype.identify = function (_a) {
2530
2628
  var distinctId = _a.distinctId,
2531
- properties = _a.properties; // Catch properties passed as $set and move them to the top level
2629
+ properties = _a.properties,
2630
+ disableGeoip = _a.disableGeoip; // Catch properties passed as $set and move them to the top level
2532
2631
 
2533
2632
  var personProperties = (properties === null || properties === void 0 ? void 0 : properties.$set) || properties;
2534
2633
 
2535
2634
  _super.prototype.identifyStateless.call(this, distinctId, {
2536
2635
  $set: personProperties
2636
+ }, {
2637
+ disableGeoip: disableGeoip
2537
2638
  });
2538
2639
  };
2539
2640
 
2540
2641
  PostHog.prototype.alias = function (data) {
2541
- _super.prototype.aliasStateless.call(this, data.alias, data.distinctId);
2642
+ _super.prototype.aliasStateless.call(this, data.alias, data.distinctId, undefined, {
2643
+ disableGeoip: data.disableGeoip
2644
+ });
2542
2645
  };
2543
2646
 
2544
2647
  PostHog.prototype.getFeatureFlag = function (key, distinctId, options) {
2545
2648
  var _a;
2546
2649
 
2547
2650
  return __awaiter(this, void 0, void 0, function () {
2548
- var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2651
+ var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2549
2652
 
2550
2653
  return __generator(this, function (_d) {
2551
2654
  switch (_d.label) {
2552
2655
  case 0:
2553
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2656
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2554
2657
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, sendFeatureFlagEvents = _c.sendFeatureFlagEvents; // set defaults
2555
2658
 
2556
2659
  if (onlyEvaluateLocally == undefined) {
@@ -2573,7 +2676,7 @@ function (_super) {
2573
2676
  , 3];
2574
2677
  return [4
2575
2678
  /*yield*/
2576
- , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
2679
+ , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2577
2680
 
2578
2681
  case 2:
2579
2682
  response = _d.sent();
@@ -2601,7 +2704,8 @@ function (_super) {
2601
2704
  $feature_flag_response: response,
2602
2705
  locally_evaluated: flagWasLocallyEvaluated
2603
2706
  },
2604
- groups: groups
2707
+ groups: groups,
2708
+ disableGeoip: disableGeoip
2605
2709
  });
2606
2710
  }
2607
2711
 
@@ -2617,12 +2721,12 @@ function (_super) {
2617
2721
  var _a;
2618
2722
 
2619
2723
  return __awaiter(this, void 0, void 0, function () {
2620
- var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
2724
+ var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
2621
2725
 
2622
2726
  return __generator(this, function (_d) {
2623
2727
  switch (_d.label) {
2624
2728
  case 0:
2625
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2729
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2626
2730
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, _c.sendFeatureFlagEvents;
2627
2731
  response = undefined;
2628
2732
  if (!!matchValue) return [3
@@ -2667,7 +2771,7 @@ function (_super) {
2667
2771
  , 6];
2668
2772
  return [4
2669
2773
  /*yield*/
2670
- , _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
2774
+ , _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2671
2775
 
2672
2776
  case 5:
2673
2777
  response = _d.sent();
@@ -2743,12 +2847,12 @@ function (_super) {
2743
2847
  var _a;
2744
2848
 
2745
2849
  return __awaiter(this, void 0, void 0, function () {
2746
- var _b, groups, personProperties, groupProperties, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
2850
+ var _b, groups, personProperties, groupProperties, disableGeoip, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
2747
2851
 
2748
2852
  return __generator(this, function (_c) {
2749
2853
  switch (_c.label) {
2750
2854
  case 0:
2751
- _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
2855
+ _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2752
2856
  onlyEvaluateLocally = (options || {}).onlyEvaluateLocally; // set defaults
2753
2857
 
2754
2858
  if (onlyEvaluateLocally == undefined) {
@@ -2776,7 +2880,7 @@ function (_super) {
2776
2880
  , 3];
2777
2881
  return [4
2778
2882
  /*yield*/
2779
- , _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties)];
2883
+ , _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2780
2884
 
2781
2885
  case 2:
2782
2886
  remoteEvaluationResult = _c.sent();
@@ -2800,9 +2904,12 @@ function (_super) {
2800
2904
  var groupType = _a.groupType,
2801
2905
  groupKey = _a.groupKey,
2802
2906
  properties = _a.properties,
2803
- distinctId = _a.distinctId;
2907
+ distinctId = _a.distinctId,
2908
+ disableGeoip = _a.disableGeoip;
2804
2909
 
2805
- _super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties, undefined, distinctId);
2910
+ _super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties, {
2911
+ disableGeoip: disableGeoip
2912
+ }, distinctId);
2806
2913
  };
2807
2914
 
2808
2915
  PostHog.prototype.reloadFeatureFlags = function () {