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/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';
@@ -42,7 +44,10 @@ declare type PostHogFetchOptions = {
42
44
  body?: string;
43
45
  signal?: AbortSignal;
44
46
  };
45
- declare type PosthogCaptureOptions = {
47
+ declare type PostHogCaptureOptions = {
48
+ /** If provided overrides the auto-generated event ID */
49
+ uuid?: string;
50
+ /** If provided overrides the auto-generated timestamp */
46
51
  timestamp?: Date;
47
52
  disableGeoip?: boolean;
48
53
  };
@@ -111,9 +116,9 @@ declare abstract class PostHogCoreStateless {
111
116
  private captureMode;
112
117
  private removeDebugCallback?;
113
118
  private debugMode;
114
- private pendingPromises;
115
119
  private disableGeoip;
116
120
  private _optoutOverride;
121
+ private pendingPromises;
117
122
  protected _events: SimpleEventEmitter;
118
123
  protected _flushTimer?: any;
119
124
  protected _retryOptions: RetriableOptions;
@@ -131,20 +136,21 @@ declare abstract class PostHogCoreStateless {
131
136
  on(event: string, cb: (...args: any[]) => void): () => void;
132
137
  debug(enabled?: boolean): void;
133
138
  private buildPayload;
139
+ protected addPendingPromise(promise: Promise<any>): void;
134
140
  /***
135
141
  *** TRACKING
136
142
  ***/
137
- protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
143
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
138
144
  protected captureStateless(distinctId: string, event: string, properties?: {
139
145
  [key: string]: any;
140
- }, options?: PosthogCaptureOptions): this;
146
+ }, options?: PostHogCaptureOptions): this;
141
147
  protected aliasStateless(alias: string, distinctId: string, properties?: {
142
148
  [key: string]: any;
143
- }, options?: PosthogCaptureOptions): this;
149
+ }, options?: PostHogCaptureOptions): this;
144
150
  /***
145
151
  *** GROUPS
146
152
  ***/
147
- protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PosthogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
153
+ protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
148
154
  /***
149
155
  *** FEATURE FLAGS
150
156
  ***/
@@ -161,7 +167,7 @@ declare abstract class PostHogCoreStateless {
161
167
  /***
162
168
  *** QUEUEING AND FLUSHING
163
169
  ***/
164
- protected enqueue(type: string, _message: any, options?: PosthogCaptureOptions): void;
170
+ protected enqueue(type: string, _message: any, options?: PostHogCaptureOptions): void;
165
171
  flushAsync(): Promise<any>;
166
172
  flush(callback?: (err?: any, data?: any) => void): void;
167
173
  private fetchWithRetry;
@@ -173,6 +179,7 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
173
179
  private flagCallReported;
174
180
  protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
175
181
  protected _sessionExpirationTimeSeconds: number;
182
+ protected sessionProps: PostHogEventProperties;
176
183
  constructor(apiKey: string, options?: PosthogCoreOptions);
177
184
  protected setupBootstrap(options?: Partial<PosthogCoreOptions>): void;
178
185
  private get props();
@@ -182,38 +189,52 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
182
189
  on(event: string, cb: (...args: any[]) => void): () => void;
183
190
  reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
184
191
  protected getCommonEventProperties(): any;
185
- private enrichProperties;
192
+ enrichProperties(properties?: PostHogEventProperties): any;
186
193
  getSessionId(): string | undefined;
187
194
  resetSessionId(): void;
188
195
  getAnonymousId(): string;
189
196
  getDistinctId(): string;
197
+ unregister(property: string): void;
190
198
  register(properties: {
191
199
  [key: string]: any;
192
200
  }): void;
193
- unregister(property: string): void;
201
+ registerForSession(properties: {
202
+ [key: string]: any;
203
+ }): void;
204
+ unregisterForSession(property: string): void;
194
205
  /***
195
206
  *** TRACKING
196
207
  ***/
197
- identify(distinctId?: string, properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
208
+ identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
198
209
  capture(event: string, properties?: {
199
210
  [key: string]: any;
200
- }, options?: PosthogCaptureOptions): this;
211
+ }, options?: PostHogCaptureOptions): this;
201
212
  alias(alias: string): this;
202
- autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
213
+ autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
203
214
  /***
204
215
  *** GROUPS
205
216
  ***/
206
217
  groups(groups: {
207
218
  [type: string]: string | number;
208
219
  }): this;
209
- group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
210
- groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
220
+ group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
221
+ groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
211
222
  /***
212
223
  * PROPERTIES
213
224
  ***/
225
+ setPersonPropertiesForFlags(properties: {
226
+ [type: string]: string;
227
+ }): this;
228
+ resetPersonPropertiesForFlags(): void;
229
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
214
230
  personProperties(properties: {
215
231
  [type: string]: string;
216
232
  }): this;
233
+ setGroupPropertiesForFlags(properties: {
234
+ [type: string]: Record<string, string>;
235
+ }): this;
236
+ resetGroupPropertiesForFlags(): void;
237
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
217
238
  groupProperties(properties: {
218
239
  [type: string]: Record<string, string>;
219
240
  }): 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;
@@ -726,8 +731,8 @@ var PostHogCoreStateless = /** @class */ (function () {
726
731
  function PostHogCoreStateless(apiKey, options) {
727
732
  var _a, _b, _c, _d, _e;
728
733
  this.debugMode = false;
729
- this.pendingPromises = {};
730
734
  this.disableGeoip = true;
735
+ this.pendingPromises = {};
731
736
  // internal
732
737
  this._events = new SimpleEventEmitter();
733
738
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -785,6 +790,14 @@ var PostHogCoreStateless = /** @class */ (function () {
785
790
  properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
786
791
  };
787
792
  };
793
+ PostHogCoreStateless.prototype.addPendingPromise = function (promise) {
794
+ var _this = this;
795
+ var promiseUUID = generateUUID();
796
+ this.pendingPromises[promiseUUID] = promise;
797
+ promise.finally(function () {
798
+ delete _this.pendingPromises[promiseUUID];
799
+ });
800
+ };
788
801
  /***
789
802
  *** TRACKING
790
803
  ***/
@@ -835,6 +848,7 @@ var PostHogCoreStateless = /** @class */ (function () {
835
848
  if (extraPayload === void 0) { extraPayload = {}; }
836
849
  return __awaiter(this, void 0, void 0, function () {
837
850
  var url, fetchOptions;
851
+ var _this = this;
838
852
  return __generator(this, function (_a) {
839
853
  url = "".concat(this.host, "/decide/?v=3");
840
854
  fetchOptions = {
@@ -845,7 +859,7 @@ var PostHogCoreStateless = /** @class */ (function () {
845
859
  return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
846
860
  .then(function (response) { return response.json(); })
847
861
  .catch(function (error) {
848
- console.error('Error fetching feature flags', error);
862
+ _this._events.emit('error', error);
849
863
  return undefined;
850
864
  })];
851
865
  });
@@ -979,10 +993,10 @@ var PostHogCoreStateless = /** @class */ (function () {
979
993
  var _this = this;
980
994
  var _a;
981
995
  if (this.optedOut) {
982
- this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
996
+ this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
983
997
  return;
984
998
  }
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() });
999
+ 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) });
986
1000
  var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
987
1001
  if (addGeoipDisableProperty) {
988
1002
  if (!message.properties) {
@@ -1032,14 +1046,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1032
1046
  batch: messages,
1033
1047
  sent_at: currentISOTime(),
1034
1048
  };
1035
- var promiseUUID = generateUUID();
1036
1049
  var done = function (err) {
1037
1050
  if (err) {
1038
1051
  _this._events.emit('error', err);
1039
1052
  }
1040
1053
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1041
- // remove promise from pendingPromises
1042
- delete _this.pendingPromises[promiseUUID];
1043
1054
  _this._events.emit('flush', messages);
1044
1055
  };
1045
1056
  // Don't set the user agent if we're not on a browser. The latest spec allows
@@ -1065,12 +1076,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1065
1076
  body: payload,
1066
1077
  };
1067
1078
  var requestPromise = this.fetchWithRetry(url, fetchOptions);
1068
- this.pendingPromises[promiseUUID] = requestPromise;
1069
- requestPromise
1079
+ this.addPendingPromise(requestPromise
1070
1080
  .then(function () { return done(); })
1071
1081
  .catch(function (err) {
1072
1082
  done(err);
1073
- });
1083
+ }));
1074
1084
  };
1075
1085
  PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
1076
1086
  var _a;
@@ -1124,7 +1134,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1124
1134
  clearTimeout(this._flushTimer);
1125
1135
  _a.label = 1;
1126
1136
  case 1:
1127
- _a.trys.push([1, 4, , 5]);
1137
+ _a.trys.push([1, 5, , 6]);
1128
1138
  return [4 /*yield*/, this.flushAsync()];
1129
1139
  case 2:
1130
1140
  _a.sent();
@@ -1132,18 +1142,31 @@ var PostHogCoreStateless = /** @class */ (function () {
1132
1142
  return x.catch(function () {
1133
1143
  // ignore errors as we are shutting down and can't deal with them anyways.
1134
1144
  });
1135
- }))];
1145
+ }))
1146
+ // flush again to make sure we send all events, some of which might've been added
1147
+ // while we were waiting for the pending promises to resolve
1148
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1149
+ ];
1136
1150
  case 3:
1137
1151
  _a.sent();
1138
- return [3 /*break*/, 5];
1152
+ // flush again to make sure we send all events, some of which might've been added
1153
+ // while we were waiting for the pending promises to resolve
1154
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1155
+ return [4 /*yield*/, this.flushAsync()];
1139
1156
  case 4:
1157
+ // flush again to make sure we send all events, some of which might've been added
1158
+ // while we were waiting for the pending promises to resolve
1159
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1160
+ _a.sent();
1161
+ return [3 /*break*/, 6];
1162
+ case 5:
1140
1163
  e_2 = _a.sent();
1141
1164
  if (!isPostHogFetchError(e_2)) {
1142
1165
  throw e_2;
1143
1166
  }
1144
1167
  console.error('Error while shutting down PostHog', e_2);
1145
- return [3 /*break*/, 5];
1146
- case 5: return [2 /*return*/];
1168
+ return [3 /*break*/, 6];
1169
+ case 6: return [2 /*return*/];
1147
1170
  }
1148
1171
  });
1149
1172
  });
@@ -1162,6 +1185,7 @@ var PostHogCore = /** @class */ (function (_super) {
1162
1185
  var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1163
1186
  _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1164
1187
  _this.flagCallReported = {};
1188
+ _this.sessionProps = {};
1165
1189
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1166
1190
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1167
1191
  return _this;
@@ -1203,6 +1227,7 @@ var PostHogCore = /** @class */ (function (_super) {
1203
1227
  });
1204
1228
  PostHogCore.prototype.clearProps = function () {
1205
1229
  this.props = undefined;
1230
+ this.sessionProps = {};
1206
1231
  };
1207
1232
  PostHogCore.prototype.on = function (event, cb) {
1208
1233
  return this._events.on(event, cb);
@@ -1230,7 +1255,7 @@ var PostHogCore = /** @class */ (function (_super) {
1230
1255
  return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
1231
1256
  };
1232
1257
  PostHogCore.prototype.enrichProperties = function (properties) {
1233
- return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1258
+ return __assign(__assign(__assign(__assign(__assign({}, this.props), this.sessionProps), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1234
1259
  };
1235
1260
  PostHogCore.prototype.getSessionId = function () {
1236
1261
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
@@ -1256,13 +1281,19 @@ var PostHogCore = /** @class */ (function (_super) {
1256
1281
  PostHogCore.prototype.getDistinctId = function () {
1257
1282
  return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1258
1283
  };
1284
+ PostHogCore.prototype.unregister = function (property) {
1285
+ delete this.props[property];
1286
+ this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1287
+ };
1259
1288
  PostHogCore.prototype.register = function (properties) {
1260
1289
  this.props = __assign(__assign({}, this.props), properties);
1261
1290
  this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1262
1291
  };
1263
- PostHogCore.prototype.unregister = function (property) {
1264
- delete this.props[property];
1265
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1292
+ PostHogCore.prototype.registerForSession = function (properties) {
1293
+ this.sessionProps = __assign(__assign({}, this.sessionProps), properties);
1294
+ };
1295
+ PostHogCore.prototype.unregisterForSession = function (property) {
1296
+ delete this.sessionProps[property];
1266
1297
  };
1267
1298
  /***
1268
1299
  *** TRACKING
@@ -1342,13 +1373,20 @@ var PostHogCore = /** @class */ (function (_super) {
1342
1373
  /***
1343
1374
  * PROPERTIES
1344
1375
  ***/
1345
- PostHogCore.prototype.personProperties = function (properties) {
1376
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1346
1377
  // Get persisted person properties
1347
1378
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1348
1379
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1349
1380
  return this;
1350
1381
  };
1351
- PostHogCore.prototype.groupProperties = function (properties) {
1382
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1383
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1384
+ };
1385
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1386
+ PostHogCore.prototype.personProperties = function (properties) {
1387
+ return this.setPersonPropertiesForFlags(properties);
1388
+ };
1389
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1352
1390
  // Get persisted group properties
1353
1391
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1354
1392
  if (Object.keys(existingProperties).length !== 0) {
@@ -1360,6 +1398,13 @@ var PostHogCore = /** @class */ (function (_super) {
1360
1398
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1361
1399
  return this;
1362
1400
  };
1401
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1402
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1403
+ };
1404
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1405
+ PostHogCore.prototype.groupProperties = function (properties) {
1406
+ return this.setGroupPropertiesForFlags(properties);
1407
+ };
1363
1408
  /***
1364
1409
  *** FEATURE FLAGS
1365
1410
  ***/
@@ -1551,7 +1596,7 @@ var PostHogCore = /** @class */ (function (_super) {
1551
1596
  return PostHogCore;
1552
1597
  }(PostHogCoreStateless));
1553
1598
 
1554
- var version = "2.4.0";
1599
+ var version = "2.6.0";
1555
1600
 
1556
1601
  function getContext(window) {
1557
1602
  var context = {};