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/lib/index.d.ts CHANGED
@@ -44,7 +44,10 @@ declare type PostHogFetchOptions = {
44
44
  body?: string;
45
45
  signal?: AbortSignal;
46
46
  };
47
- 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 */
48
51
  timestamp?: Date;
49
52
  disableGeoip?: boolean;
50
53
  };
@@ -113,9 +116,9 @@ declare abstract class PostHogCoreStateless {
113
116
  private captureMode;
114
117
  private removeDebugCallback?;
115
118
  private debugMode;
116
- private pendingPromises;
117
119
  private disableGeoip;
118
120
  private _optoutOverride;
121
+ private pendingPromises;
119
122
  protected _events: SimpleEventEmitter;
120
123
  protected _flushTimer?: any;
121
124
  protected _retryOptions: RetriableOptions;
@@ -133,20 +136,21 @@ declare abstract class PostHogCoreStateless {
133
136
  on(event: string, cb: (...args: any[]) => void): () => void;
134
137
  debug(enabled?: boolean): void;
135
138
  private buildPayload;
139
+ protected addPendingPromise(promise: Promise<any>): void;
136
140
  /***
137
141
  *** TRACKING
138
142
  ***/
139
- protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
143
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
140
144
  protected captureStateless(distinctId: string, event: string, properties?: {
141
145
  [key: string]: any;
142
- }, options?: PosthogCaptureOptions): this;
146
+ }, options?: PostHogCaptureOptions): this;
143
147
  protected aliasStateless(alias: string, distinctId: string, properties?: {
144
148
  [key: string]: any;
145
- }, options?: PosthogCaptureOptions): this;
149
+ }, options?: PostHogCaptureOptions): this;
146
150
  /***
147
151
  *** GROUPS
148
152
  ***/
149
- 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;
150
154
  /***
151
155
  *** FEATURE FLAGS
152
156
  ***/
@@ -163,7 +167,7 @@ declare abstract class PostHogCoreStateless {
163
167
  /***
164
168
  *** QUEUEING AND FLUSHING
165
169
  ***/
166
- protected enqueue(type: string, _message: any, options?: PosthogCaptureOptions): void;
170
+ protected enqueue(type: string, _message: any, options?: PostHogCaptureOptions): void;
167
171
  flushAsync(): Promise<any>;
168
172
  flush(callback?: (err?: any, data?: any) => void): void;
169
173
  private fetchWithRetry;
@@ -201,20 +205,20 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
201
205
  /***
202
206
  *** TRACKING
203
207
  ***/
204
- identify(distinctId?: string, properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
208
+ identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
205
209
  capture(event: string, properties?: {
206
210
  [key: string]: any;
207
- }, options?: PosthogCaptureOptions): this;
211
+ }, options?: PostHogCaptureOptions): this;
208
212
  alias(alias: string): this;
209
- autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
213
+ autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
210
214
  /***
211
215
  *** GROUPS
212
216
  ***/
213
217
  groups(groups: {
214
218
  [type: string]: string | number;
215
219
  }): this;
216
- group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
217
- 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;
218
222
  /***
219
223
  * PROPERTIES
220
224
  ***/
package/lib/index.esm.js CHANGED
@@ -731,8 +731,8 @@ var PostHogCoreStateless = /** @class */ (function () {
731
731
  function PostHogCoreStateless(apiKey, options) {
732
732
  var _a, _b, _c, _d, _e;
733
733
  this.debugMode = false;
734
- this.pendingPromises = {};
735
734
  this.disableGeoip = true;
735
+ this.pendingPromises = {};
736
736
  // internal
737
737
  this._events = new SimpleEventEmitter();
738
738
  assert(apiKey, "You must pass your PostHog project's api key.");
@@ -790,6 +790,14 @@ var PostHogCoreStateless = /** @class */ (function () {
790
790
  properties: __assign(__assign({}, (payload.properties || {})), this.getCommonEventProperties()),
791
791
  };
792
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
+ };
793
801
  /***
794
802
  *** TRACKING
795
803
  ***/
@@ -840,6 +848,7 @@ var PostHogCoreStateless = /** @class */ (function () {
840
848
  if (extraPayload === void 0) { extraPayload = {}; }
841
849
  return __awaiter(this, void 0, void 0, function () {
842
850
  var url, fetchOptions;
851
+ var _this = this;
843
852
  return __generator(this, function (_a) {
844
853
  url = "".concat(this.host, "/decide/?v=3");
845
854
  fetchOptions = {
@@ -850,7 +859,7 @@ var PostHogCoreStateless = /** @class */ (function () {
850
859
  return [2 /*return*/, this.fetchWithRetry(url, fetchOptions)
851
860
  .then(function (response) { return response.json(); })
852
861
  .catch(function (error) {
853
- console.error('Error fetching feature flags', error);
862
+ _this._events.emit('error', error);
854
863
  return undefined;
855
864
  })];
856
865
  });
@@ -984,10 +993,10 @@ var PostHogCoreStateless = /** @class */ (function () {
984
993
  var _this = this;
985
994
  var _a;
986
995
  if (this.optedOut) {
987
- 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()");
988
997
  return;
989
998
  }
990
- 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) });
991
1000
  var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
992
1001
  if (addGeoipDisableProperty) {
993
1002
  if (!message.properties) {
@@ -1037,14 +1046,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1037
1046
  batch: messages,
1038
1047
  sent_at: currentISOTime(),
1039
1048
  };
1040
- var promiseUUID = generateUUID();
1041
1049
  var done = function (err) {
1042
1050
  if (err) {
1043
1051
  _this._events.emit('error', err);
1044
1052
  }
1045
1053
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1046
- // remove promise from pendingPromises
1047
- delete _this.pendingPromises[promiseUUID];
1048
1054
  _this._events.emit('flush', messages);
1049
1055
  };
1050
1056
  // Don't set the user agent if we're not on a browser. The latest spec allows
@@ -1070,12 +1076,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1070
1076
  body: payload,
1071
1077
  };
1072
1078
  var requestPromise = this.fetchWithRetry(url, fetchOptions);
1073
- this.pendingPromises[promiseUUID] = requestPromise;
1074
- requestPromise
1079
+ this.addPendingPromise(requestPromise
1075
1080
  .then(function () { return done(); })
1076
1081
  .catch(function (err) {
1077
1082
  done(err);
1078
- });
1083
+ }));
1079
1084
  };
1080
1085
  PostHogCoreStateless.prototype.fetchWithRetry = function (url, options, retryOptions) {
1081
1086
  var _a;
@@ -1129,7 +1134,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1129
1134
  clearTimeout(this._flushTimer);
1130
1135
  _a.label = 1;
1131
1136
  case 1:
1132
- _a.trys.push([1, 4, , 5]);
1137
+ _a.trys.push([1, 5, , 6]);
1133
1138
  return [4 /*yield*/, this.flushAsync()];
1134
1139
  case 2:
1135
1140
  _a.sent();
@@ -1137,18 +1142,31 @@ var PostHogCoreStateless = /** @class */ (function () {
1137
1142
  return x.catch(function () {
1138
1143
  // ignore errors as we are shutting down and can't deal with them anyways.
1139
1144
  });
1140
- }))];
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
+ ];
1141
1150
  case 3:
1142
1151
  _a.sent();
1143
- 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()];
1144
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:
1145
1163
  e_2 = _a.sent();
1146
1164
  if (!isPostHogFetchError(e_2)) {
1147
1165
  throw e_2;
1148
1166
  }
1149
1167
  console.error('Error while shutting down PostHog', e_2);
1150
- return [3 /*break*/, 5];
1151
- case 5: return [2 /*return*/];
1168
+ return [3 /*break*/, 6];
1169
+ case 6: return [2 /*return*/];
1152
1170
  }
1153
1171
  });
1154
1172
  });
@@ -1578,7 +1596,7 @@ var PostHogCore = /** @class */ (function (_super) {
1578
1596
  return PostHogCore;
1579
1597
  }(PostHogCoreStateless));
1580
1598
 
1581
- var version = "2.5.0";
1599
+ var version = "2.6.0";
1582
1600
 
1583
1601
  function getContext(window) {
1584
1602
  var context = {};