posthog-js-lite 2.5.0 → 2.6.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 CHANGED
@@ -1,3 +1,7 @@
1
+ # 2.6.0 - 2024-01-18
2
+
3
+ 1. Adds support for overriding the event `uuid` via capture options
4
+
1
5
  # 2.5.0 - 2023-12-04
2
6
 
3
7
  1. Renamed `personProperties` to `setPersonPropertiesForFlags` to match `posthog-js` and more clearly indicated what it does
package/lib/index.cjs.js CHANGED
@@ -735,8 +735,8 @@ var PostHogCoreStateless = /** @class */ (function () {
735
735
  function PostHogCoreStateless(apiKey, options) {
736
736
  var _a, _b, _c, _d, _e;
737
737
  this.debugMode = false;
738
- this.pendingPromises = {};
739
738
  this.disableGeoip = true;
739
+ this.pendingPromises = {};
740
740
  // internal
741
741
  this._events = new SimpleEventEmitter();
742
742
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -794,6 +794,14 @@ var PostHogCoreStateless = /** @class */ (function () {
794
794
  properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
795
795
  };
796
796
  };
797
+ PostHogCoreStateless.prototype.addPendingPromise = function (promise) {
798
+ var _this = this;
799
+ var promiseUUID = generateUUID();
800
+ this.pendingPromises[promiseUUID] = promise;
801
+ promise.finally(function () {
802
+ delete _this.pendingPromises[promiseUUID];
803
+ });
804
+ };
797
805
  /***
798
806
  *** TRACKING
799
807
  ***/
@@ -844,6 +852,7 @@ var PostHogCoreStateless = /** @class */ (function () {
844
852
  if (extraPayload === void 0) { extraPayload = {}; }
845
853
  return __awaiter(this, void 0, void 0, function () {
846
854
  var url, fetchOptions;
855
+ var _this = this;
847
856
  return __generator(this, function (_a) {
848
857
  url = "".concat(this.host, "/decide/?v=3");
849
858
  fetchOptions = {
@@ -854,7 +863,7 @@ var PostHogCoreStateless = /** @class */ (function () {
854
863
  return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
855
864
  .then(function (response) { return response.json(); })
856
865
  .catch(function (error) {
857
- console.error('Error fetching feature flags', error);
866
+ _this._events.emit('error', error);
858
867
  return undefined;
859
868
  })];
860
869
  });
@@ -988,10 +997,10 @@ var PostHogCoreStateless = /** @class */ (function () {
988
997
  var _this = this;
989
998
  var _a;
990
999
  if (this.optedOut) {
991
- this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
1000
+ this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
992
1001
  return;
993
1002
  }
994
- 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 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(), uuid: (options === null || options === void 0 ? void 0 : options.uuid) ? options.uuid : generateUUID(globalThis) });
995
1004
  var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
996
1005
  if (addGeoipDisableProperty) {
997
1006
  if (!message.properties) {
@@ -1041,14 +1050,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1041
1050
  batch: messages,
1042
1051
  sent_at: currentISOTime(),
1043
1052
  };
1044
- var promiseUUID = generateUUID();
1045
1053
  var done = function (err) {
1046
1054
  if (err) {
1047
1055
  _this._events.emit('error', err);
1048
1056
  }
1049
1057
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1050
- // remove promise from pendingPromises
1051
- delete _this.pendingPromises[promiseUUID];
1052
1058
  _this._events.emit('flush', messages);
1053
1059
  };
1054
1060
  // Don't set the user agent if we're not on a browser. The latest spec allows
@@ -1074,12 +1080,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1074
1080
  body: payload,
1075
1081
  };
1076
1082
  var requestPromise = this.fetchWithRetry(url, fetchOptions);
1077
- this.pendingPromises[promiseUUID] = requestPromise;
1078
- requestPromise
1083
+ this.addPendingPromise(requestPromise
1079
1084
  .then(function () { return done(); })
1080
1085
  .catch(function (err) {
1081
1086
  done(err);
1082
- });
1087
+ }));
1083
1088
  };
1084
1089
  PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
1085
1090
  var _a;
@@ -1133,7 +1138,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1133
1138
  clearTimeout(this._flushTimer);
1134
1139
  _a.label = 1;
1135
1140
  case 1:
1136
- _a.trys.push([1, 4, , 5]);
1141
+ _a.trys.push([1, 5, , 6]);
1137
1142
  return [4 /*yield*/, this.flushAsync()];
1138
1143
  case 2:
1139
1144
  _a.sent();
@@ -1141,18 +1146,31 @@ var PostHogCoreStateless = /** @class */ (function () {
1141
1146
  return x.catch(function () {
1142
1147
  // ignore errors as we are shutting down and can't deal with them anyways.
1143
1148
  });
1144
- }))];
1149
+ }))
1150
+ // flush again to make sure we send all events, some of which might've been added
1151
+ // while we were waiting for the pending promises to resolve
1152
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1153
+ ];
1145
1154
  case 3:
1146
1155
  _a.sent();
1147
- return [3 /*break*/, 5];
1156
+ // flush again to make sure we send all events, some of which might've been added
1157
+ // while we were waiting for the pending promises to resolve
1158
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1159
+ return [4 /*yield*/, this.flushAsync()];
1148
1160
  case 4:
1161
+ // flush again to make sure we send all events, some of which might've been added
1162
+ // while we were waiting for the pending promises to resolve
1163
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1164
+ _a.sent();
1165
+ return [3 /*break*/, 6];
1166
+ case 5:
1149
1167
  e_2 = _a.sent();
1150
1168
  if (!isPostHogFetchError(e_2)) {
1151
1169
  throw e_2;
1152
1170
  }
1153
1171
  console.error('Error while shutting down PostHog', e_2);
1154
- return [3 /*break*/, 5];
1155
- case 5: return [2 /*return*/];
1172
+ return [3 /*break*/, 6];
1173
+ case 6: return [2 /*return*/];
1156
1174
  }
1157
1175
  });
1158
1176
  });
@@ -1582,7 +1600,7 @@ var PostHogCore = /** @class */ (function (_super) {
1582
1600
  return PostHogCore;
1583
1601
  }(PostHogCoreStateless));
1584
1602
 
1585
- var version = "2.5.0";
1603
+ var version = "2.6.0";
1586
1604
 
1587
1605
  function getContext(window) {
1588
1606
  var context = {};