posthog-js-lite 2.3.0 → 2.5.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
@@ -30,7 +30,9 @@ declare enum PostHogPersistedProperty {
30
30
  SessionId = "session_id",
31
31
  SessionLastTimestamp = "session_timestamp",
32
32
  PersonProperties = "person_properties",
33
- GroupProperties = "group_properties"
33
+ GroupProperties = "group_properties",
34
+ InstalledAppBuild = "installed_app_build",
35
+ InstalledAppVersion = "installed_app_version"
34
36
  }
35
37
  declare type PostHogFetchOptions = {
36
38
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
@@ -173,6 +175,7 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
173
175
  private flagCallReported;
174
176
  protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
175
177
  protected _sessionExpirationTimeSeconds: number;
178
+ protected sessionProps: PostHogEventProperties;
176
179
  constructor(apiKey: string, options?: PosthogCoreOptions);
177
180
  protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
178
181
  private get props();
@@ -182,15 +185,19 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
182
185
  on(event: string, cb: (...args: any[]) => void): () => void;
183
186
  reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
184
187
  protected getCommonEventProperties(): any;
185
- private enrichProperties;
188
+ enrichProperties(properties?: PostHogEventProperties): any;
186
189
  getSessionId(): string | undefined;
187
190
  resetSessionId(): void;
188
191
  getAnonymousId(): string;
189
192
  getDistinctId(): string;
193
+ unregister(property: string): void;
190
194
  register(properties: {
191
195
  [key: string]: any;
192
196
  }): void;
193
- unregister(property: string): void;
197
+ registerForSession(properties: {
198
+ [key: string]: any;
199
+ }): void;
200
+ unregisterForSession(property: string): void;
194
201
  /***
195
202
  *** TRACKING
196
203
  ***/
@@ -211,9 +218,19 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
211
218
  /***
212
219
  * PROPERTIES
213
220
  ***/
221
+ setPersonPropertiesForFlags(properties: {
222
+ [type: string]: string;
223
+ }): this;
224
+ resetPersonPropertiesForFlags(): void;
225
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
214
226
  personProperties(properties: {
215
227
  [type: string]: string;
216
228
  }): this;
229
+ setGroupPropertiesForFlags(properties: {
230
+ [type: string]: Record<string, string>;
231
+ }): this;
232
+ resetGroupPropertiesForFlags(): void;
233
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
217
234
  groupProperties(properties: {
218
235
  [type: string]: Record<string, string>;
219
236
  }): this;
package/lib/index.esm.js CHANGED
@@ -154,6 +154,8 @@ var PostHogPersistedProperty;
154
154
  PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
155
155
  PostHogPersistedProperty["PersonProperties"] = "person_properties";
156
156
  PostHogPersistedProperty["GroupProperties"] = "group_properties";
157
+ PostHogPersistedProperty["InstalledAppBuild"] = "installed_app_build";
158
+ PostHogPersistedProperty["InstalledAppVersion"] = "installed_app_version";
157
159
  })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
158
160
 
159
161
  function assert(truthyValue, message) {
@@ -712,7 +714,10 @@ var PostHogFetchHttpError = /** @class */ (function (_super) {
712
714
  var PostHogFetchNetworkError = /** @class */ (function (_super) {
713
715
  __extends(PostHogFetchNetworkError, _super);
714
716
  function PostHogFetchNetworkError(error) {
715
- var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
717
+ var _this =
718
+ // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue.
719
+ // @ts-ignore
720
+ _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
716
721
  _this.error = error;
717
722
  _this.name = 'PostHogFetchNetworkError';
718
723
  return _this;
@@ -1128,7 +1133,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1128
1133
  return [4 /*yield*/, this.flushAsync()];
1129
1134
  case 2:
1130
1135
  _a.sent();
1131
- return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
1136
+ return [4 /*yield*/, Promise.all(Object.values(this.pendingPromises).map(function (x) {
1137
+ return x.catch(function () {
1138
+ // ignore errors as we are shutting down and can't deal with them anyways.
1139
+ });
1140
+ }))];
1132
1141
  case 3:
1133
1142
  _a.sent();
1134
1143
  return [3 /*break*/, 5];
@@ -1158,14 +1167,9 @@ var PostHogCore = /** @class */ (function (_super) {
1158
1167
  var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1159
1168
  _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1160
1169
  _this.flagCallReported = {};
1170
+ _this.sessionProps = {};
1161
1171
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1162
1172
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1163
- // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1164
- if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1165
- safeSetTimeout(function () {
1166
- _this.reloadFeatureFlags();
1167
- }, 1);
1168
- }
1169
1173
  return _this;
1170
1174
  }
1171
1175
  PostHogCore.prototype.setupBootstrap = function (options) {
@@ -1205,6 +1209,7 @@ var PostHogCore = /** @class */ (function (_super) {
1205
1209
  });
1206
1210
  PostHogCore.prototype.clearProps = function () {
1207
1211
  this.props = undefined;
1212
+ this.sessionProps = {};
1208
1213
  };
1209
1214
  PostHogCore.prototype.on = function (event, cb) {
1210
1215
  return this._events.on(event, cb);
@@ -1232,7 +1237,7 @@ var PostHogCore = /** @class */ (function (_super) {
1232
1237
  return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
1233
1238
  };
1234
1239
  PostHogCore.prototype.enrichProperties = function (properties) {
1235
- return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1240
+ return __assign(__assign(__assign(__assign(__assign({}, this.props), this.sessionProps), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1236
1241
  };
1237
1242
  PostHogCore.prototype.getSessionId = function () {
1238
1243
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
@@ -1258,13 +1263,19 @@ var PostHogCore = /** @class */ (function (_super) {
1258
1263
  PostHogCore.prototype.getDistinctId = function () {
1259
1264
  return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1260
1265
  };
1266
+ PostHogCore.prototype.unregister = function (property) {
1267
+ delete this.props[property];
1268
+ this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1269
+ };
1261
1270
  PostHogCore.prototype.register = function (properties) {
1262
1271
  this.props = __assign(__assign({}, this.props), properties);
1263
1272
  this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1264
1273
  };
1265
- PostHogCore.prototype.unregister = function (property) {
1266
- delete this.props[property];
1267
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1274
+ PostHogCore.prototype.registerForSession = function (properties) {
1275
+ this.sessionProps = __assign(__assign({}, this.sessionProps), properties);
1276
+ };
1277
+ PostHogCore.prototype.unregisterForSession = function (property) {
1278
+ delete this.sessionProps[property];
1268
1279
  };
1269
1280
  /***
1270
1281
  *** TRACKING
@@ -1280,9 +1291,7 @@ var PostHogCore = /** @class */ (function (_super) {
1280
1291
  // We keep the AnonymousId to be used by decide calls and identify to link the previousId
1281
1292
  this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
1282
1293
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1283
- if (this.getFeatureFlags()) {
1284
- this.reloadFeatureFlags();
1285
- }
1294
+ this.reloadFeatureFlags();
1286
1295
  }
1287
1296
  _super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
1288
1297
  return this;
@@ -1322,7 +1331,7 @@ var PostHogCore = /** @class */ (function (_super) {
1322
1331
  this.register({
1323
1332
  $groups: __assign(__assign({}, existingGroups), groups),
1324
1333
  });
1325
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
1334
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })) {
1326
1335
  this.reloadFeatureFlags();
1327
1336
  }
1328
1337
  return this;
@@ -1346,13 +1355,20 @@ var PostHogCore = /** @class */ (function (_super) {
1346
1355
  /***
1347
1356
  * PROPERTIES
1348
1357
  ***/
1349
- PostHogCore.prototype.personProperties = function (properties) {
1358
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1350
1359
  // Get persisted person properties
1351
1360
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1352
1361
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1353
1362
  return this;
1354
1363
  };
1355
- PostHogCore.prototype.groupProperties = function (properties) {
1364
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1365
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1366
+ };
1367
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1368
+ PostHogCore.prototype.personProperties = function (properties) {
1369
+ return this.setPersonPropertiesForFlags(properties);
1370
+ };
1371
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1356
1372
  // Get persisted group properties
1357
1373
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1358
1374
  if (Object.keys(existingProperties).length !== 0) {
@@ -1364,6 +1380,13 @@ var PostHogCore = /** @class */ (function (_super) {
1364
1380
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1365
1381
  return this;
1366
1382
  };
1383
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1384
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1385
+ };
1386
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1387
+ PostHogCore.prototype.groupProperties = function (properties) {
1388
+ return this.setGroupPropertiesForFlags(properties);
1389
+ };
1367
1390
  /***
1368
1391
  *** FEATURE FLAGS
1369
1392
  ***/
@@ -1555,7 +1578,7 @@ var PostHogCore = /** @class */ (function (_super) {
1555
1578
  return PostHogCore;
1556
1579
  }(PostHogCoreStateless));
1557
1580
 
1558
- var version = "2.3.0";
1581
+ var version = "2.5.0";
1559
1582
 
1560
1583
  function getContext(window) {
1561
1584
  var context = {};
@@ -1920,6 +1943,10 @@ function (_super) {
1920
1943
 
1921
1944
  _this.setupBootstrap(options);
1922
1945
 
1946
+ if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1947
+ _this.reloadFeatureFlags();
1948
+ }
1949
+
1923
1950
  return _this;
1924
1951
  }
1925
1952