posthog-js-lite 2.4.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,12 @@
1
+ # 2.6.0 - 2024-01-18
2
+
3
+ 1. Adds support for overriding the event `uuid` via capture options
4
+
5
+ # 2.5.0 - 2023-12-04
6
+
7
+ 1. Renamed `personProperties` to `setPersonPropertiesForFlags` to match `posthog-js` and more clearly indicated what it does
8
+ 2. Renamed `groupProperties` to `setGroupPropertiesForFlags` to match `posthog-js` and more clearly indicated what it does
9
+
1
10
  # 2.4.0 - 2023-04-20
2
11
 
3
12
  1. Fixes a race condition that could occur when initialising PostHog
package/lib/index.cjs.js CHANGED
@@ -158,6 +158,8 @@ var PostHogPersistedProperty;
158
158
  PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
159
159
  PostHogPersistedProperty["PersonProperties"] = "person_properties";
160
160
  PostHogPersistedProperty["GroupProperties"] = "group_properties";
161
+ PostHogPersistedProperty["InstalledAppBuild"] = "installed_app_build";
162
+ PostHogPersistedProperty["InstalledAppVersion"] = "installed_app_version";
161
163
  })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
162
164
 
163
165
  function assert(truthyValue, message) {
@@ -716,7 +718,10 @@ var PostHogFetchHttpError = /** @class */ (function (_super) {
716
718
  var PostHogFetchNetworkError = /** @class */ (function (_super) {
717
719
  __extends(PostHogFetchNetworkError, _super);
718
720
  function PostHogFetchNetworkError(error) {
719
- var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
721
+ var _this =
722
+ // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue.
723
+ // @ts-ignore
724
+ _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
720
725
  _this.error = error;
721
726
  _this.name = 'PostHogFetchNetworkError';
722
727
  return _this;
@@ -730,8 +735,8 @@ var PostHogCoreStateless = /** @class */ (function () {
730
735
  function PostHogCoreStateless(apiKey, options) {
731
736
  var _a, _b, _c, _d, _e;
732
737
  this.debugMode = false;
733
- this.pendingPromises = {};
734
738
  this.disableGeoip = true;
739
+ this.pendingPromises = {};
735
740
  // internal
736
741
  this._events = new SimpleEventEmitter();
737
742
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -789,6 +794,14 @@ var PostHogCoreStateless = /** @class */ (function () {
789
794
  properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
790
795
  };
791
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
+ };
792
805
  /***
793
806
  *** TRACKING
794
807
  ***/
@@ -839,6 +852,7 @@ var PostHogCoreStateless = /** @class */ (function () {
839
852
  if (extraPayload === void 0) { extraPayload = {}; }
840
853
  return __awaiter(this, void 0, void 0, function () {
841
854
  var url, fetchOptions;
855
+ var _this = this;
842
856
  return __generator(this, function (_a) {
843
857
  url = "".concat(this.host, "/decide/?v=3");
844
858
  fetchOptions = {
@@ -849,7 +863,7 @@ var PostHogCoreStateless = /** @class */ (function () {
849
863
  return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
850
864
  .then(function (response) { return response.json(); })
851
865
  .catch(function (error) {
852
- console.error('Error fetching feature flags', error);
866
+ _this._events.emit('error', error);
853
867
  return undefined;
854
868
  })];
855
869
  });
@@ -983,10 +997,10 @@ var PostHogCoreStateless = /** @class */ (function () {
983
997
  var _this = this;
984
998
  var _a;
985
999
  if (this.optedOut) {
986
- 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()");
987
1001
  return;
988
1002
  }
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() });
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) });
990
1004
  var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
991
1005
  if (addGeoipDisableProperty) {
992
1006
  if (!message.properties) {
@@ -1036,14 +1050,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1036
1050
  batch: messages,
1037
1051
  sent_at: currentISOTime(),
1038
1052
  };
1039
- var promiseUUID = generateUUID();
1040
1053
  var done = function (err) {
1041
1054
  if (err) {
1042
1055
  _this._events.emit('error', err);
1043
1056
  }
1044
1057
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1045
- // remove promise from pendingPromises
1046
- delete _this.pendingPromises[promiseUUID];
1047
1058
  _this._events.emit('flush', messages);
1048
1059
  };
1049
1060
  // Don't set the user agent if we're not on a browser. The latest spec allows
@@ -1069,12 +1080,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1069
1080
  body: payload,
1070
1081
  };
1071
1082
  var requestPromise = this.fetchWithRetry(url, fetchOptions);
1072
- this.pendingPromises[promiseUUID] = requestPromise;
1073
- requestPromise
1083
+ this.addPendingPromise(requestPromise
1074
1084
  .then(function () { return done(); })
1075
1085
  .catch(function (err) {
1076
1086
  done(err);
1077
- });
1087
+ }));
1078
1088
  };
1079
1089
  PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
1080
1090
  var _a;
@@ -1128,7 +1138,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1128
1138
  clearTimeout(this._flushTimer);
1129
1139
  _a.label = 1;
1130
1140
  case 1:
1131
- _a.trys.push([1, 4, , 5]);
1141
+ _a.trys.push([1, 5, , 6]);
1132
1142
  return [4 /*yield*/, this.flushAsync()];
1133
1143
  case 2:
1134
1144
  _a.sent();
@@ -1136,18 +1146,31 @@ var PostHogCoreStateless = /** @class */ (function () {
1136
1146
  return x.catch(function () {
1137
1147
  // ignore errors as we are shutting down and can't deal with them anyways.
1138
1148
  });
1139
- }))];
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
+ ];
1140
1154
  case 3:
1141
1155
  _a.sent();
1142
- 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()];
1143
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:
1144
1167
  e_2 = _a.sent();
1145
1168
  if (!isPostHogFetchError(e_2)) {
1146
1169
  throw e_2;
1147
1170
  }
1148
1171
  console.error('Error while shutting down PostHog', e_2);
1149
- return [3 /*break*/, 5];
1150
- case 5: return [2 /*return*/];
1172
+ return [3 /*break*/, 6];
1173
+ case 6: return [2 /*return*/];
1151
1174
  }
1152
1175
  });
1153
1176
  });
@@ -1166,6 +1189,7 @@ var PostHogCore = /** @class */ (function (_super) {
1166
1189
  var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1167
1190
  _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1168
1191
  _this.flagCallReported = {};
1192
+ _this.sessionProps = {};
1169
1193
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1170
1194
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1171
1195
  return _this;
@@ -1207,6 +1231,7 @@ var PostHogCore = /** @class */ (function (_super) {
1207
1231
  });
1208
1232
  PostHogCore.prototype.clearProps = function () {
1209
1233
  this.props = undefined;
1234
+ this.sessionProps = {};
1210
1235
  };
1211
1236
  PostHogCore.prototype.on = function (event, cb) {
1212
1237
  return this._events.on(event, cb);
@@ -1234,7 +1259,7 @@ var PostHogCore = /** @class */ (function (_super) {
1234
1259
  return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
1235
1260
  };
1236
1261
  PostHogCore.prototype.enrichProperties = function (properties) {
1237
- return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1262
+ return __assign(__assign(__assign(__assign(__assign({}, this.props), this.sessionProps), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1238
1263
  };
1239
1264
  PostHogCore.prototype.getSessionId = function () {
1240
1265
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
@@ -1260,13 +1285,19 @@ var PostHogCore = /** @class */ (function (_super) {
1260
1285
  PostHogCore.prototype.getDistinctId = function () {
1261
1286
  return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1262
1287
  };
1288
+ PostHogCore.prototype.unregister = function (property) {
1289
+ delete this.props[property];
1290
+ this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1291
+ };
1263
1292
  PostHogCore.prototype.register = function (properties) {
1264
1293
  this.props = __assign(__assign({}, this.props), properties);
1265
1294
  this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1266
1295
  };
1267
- PostHogCore.prototype.unregister = function (property) {
1268
- delete this.props[property];
1269
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1296
+ PostHogCore.prototype.registerForSession = function (properties) {
1297
+ this.sessionProps = __assign(__assign({}, this.sessionProps), properties);
1298
+ };
1299
+ PostHogCore.prototype.unregisterForSession = function (property) {
1300
+ delete this.sessionProps[property];
1270
1301
  };
1271
1302
  /***
1272
1303
  *** TRACKING
@@ -1346,13 +1377,20 @@ var PostHogCore = /** @class */ (function (_super) {
1346
1377
  /***
1347
1378
  * PROPERTIES
1348
1379
  ***/
1349
- PostHogCore.prototype.personProperties = function (properties) {
1380
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1350
1381
  // Get persisted person properties
1351
1382
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1352
1383
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1353
1384
  return this;
1354
1385
  };
1355
- PostHogCore.prototype.groupProperties = function (properties) {
1386
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1387
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1388
+ };
1389
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1390
+ PostHogCore.prototype.personProperties = function (properties) {
1391
+ return this.setPersonPropertiesForFlags(properties);
1392
+ };
1393
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1356
1394
  // Get persisted group properties
1357
1395
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1358
1396
  if (Object.keys(existingProperties).length !== 0) {
@@ -1364,6 +1402,13 @@ var PostHogCore = /** @class */ (function (_super) {
1364
1402
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1365
1403
  return this;
1366
1404
  };
1405
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1406
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1407
+ };
1408
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1409
+ PostHogCore.prototype.groupProperties = function (properties) {
1410
+ return this.setGroupPropertiesForFlags(properties);
1411
+ };
1367
1412
  /***
1368
1413
  *** FEATURE FLAGS
1369
1414
  ***/
@@ -1555,7 +1600,7 @@ var PostHogCore = /** @class */ (function (_super) {
1555
1600
  return PostHogCore;
1556
1601
  }(PostHogCoreStateless));
1557
1602
 
1558
- var version = "2.4.0";
1603
+ var version = "2.6.0";
1559
1604
 
1560
1605
  function getContext(window) {
1561
1606
  var context = {};