posthog-js-lite 3.0.0-beta.2 → 3.0.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,17 +1,42 @@
1
+ # 3.0.0 - 2024-03-18
2
+
3
+ ## Added
4
+
5
+ 1. Adds a `disabled` option and the ability to change it later via `posthog.disabled = true`. Useful for disabling PostHog tracking for example in a testing environment without having complex conditional checking
6
+ 2. `shutdown` takes a `shutdownTimeoutMs` param with a default of 30000 (30s). This is the time to wait for flushing events before shutting down the client. If the timeout is reached, the client will be shut down regardless of pending events.
7
+ 3. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 10 seconds.
8
+ 4. Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
9
+ 5. Queued events are limited up to `maxQueueSize` (default 1000) and the oldest events are dropped when the limit is reached
10
+
11
+ ## Removed
12
+
13
+ 1. Removes the `enable` option. You can now specify `defaultOptIn: false` to start the SDK opted out of tracking
14
+ 2. `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
15
+
16
+ ## Changed
17
+
18
+ 1. `flush` and `shutdown` now being async methods.
19
+ 2. Many methods such as `capture` and `identify` no longer return the `this` object instead returning nothing
20
+
21
+ ## Fixed
22
+
23
+ 1. Fixed an issue where `shutdown` would potentially exit early if a flush was already in progress
24
+ 2. Fixes some typos in types
25
+
1
26
  # 3.0.0-beta.2 - 2024-03-12
2
27
 
3
- - `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
4
- - Fixed an issue where `shutdownAsync` would potentially exit early if a flush was already in progress
5
- - Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
28
+ 1. `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
29
+ 2. Fixed an issue where `shutdownAsync` would potentially exit early if a flush was already in progress
30
+ 3. Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
6
31
 
7
32
  # 3.0.0-beta.1 - 2024-03-04
8
33
 
9
- - Removes the `enable` option. You can now specify `defaultOptIn: false` to start the SDK opted out of tracking
10
- - Adds a `disabled` option and the ability to change it later via `posthog.disabled = true`. Useful for disabling PostHog tracking for example in a testing environment without having complex conditional checking
11
- - Many methods such as `capture` and `identify` no longer return the `this` object instead returning nothing
12
- - Fixes some typos in types
13
- - `shutdown` and `shutdownAsync` takes a `shutdownTimeoutMs` param with a default of 30000 (30s). This is the time to wait for flushing events before shutting down the client. If the timeout is reached, the client will be shut down regardless of pending events.
14
- - Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 10 seconds.
34
+ 1. Removes the `enable` option. You can now specify `defaultOptIn: false` to start the SDK opted out of tracking
35
+ 2. Adds a `disabled` option and the ability to change it later via `posthog.disabled = true`. Useful for disabling PostHog tracking for example in a testing environment without having complex conditional checking
36
+ 3. Many methods such as `capture` and `identify` no longer return the `this` object instead returning nothing
37
+ 4. Fixes some typos in types
38
+ 5. `shutdown` and `shutdownAsync` takes a `shutdownTimeoutMs` param with a default of 30000 (30s). This is the time to wait for flushing events before shutting down the client. If the timeout is reached, the client will be shut down regardless of pending events.
39
+ 6. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 10 seconds.
15
40
 
16
41
  # 2.6.2 - 2024-02-15
17
42
 
package/lib/index.cjs.js CHANGED
@@ -959,6 +959,7 @@ class PostHogCoreStateless {
959
959
  this.host = removeTrailingSlash(options?.host || 'https://app.posthog.com');
960
960
  this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
961
961
  this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
962
+ this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
962
963
  this.flushInterval = options?.flushInterval ?? 10000;
963
964
  this.captureMode = options?.captureMode || 'form';
964
965
  // If enable is explicitly set to false we override the optout
@@ -1107,7 +1108,7 @@ class PostHogCoreStateless {
1107
1108
  const url = `${this.host}/decide/?v=3`;
1108
1109
  const fetchOptions = {
1109
1110
  method: 'POST',
1110
- headers: { 'Content-Type': 'application/json' },
1111
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1111
1112
  body: JSON.stringify({
1112
1113
  token: this.apiKey,
1113
1114
  distinct_id: distinctId,
@@ -1217,6 +1218,10 @@ class PostHogCoreStateless {
1217
1218
  delete message.distinctId;
1218
1219
  }
1219
1220
  const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1221
+ if (queue.length >= this.maxQueueSize) {
1222
+ queue.shift();
1223
+ console.info('Queue is full, the oldest event is dropped.');
1224
+ }
1220
1225
  queue.push({ message });
1221
1226
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1222
1227
  this._events.emit(type, message);
@@ -1251,6 +1256,18 @@ class PostHogCoreStateless {
1251
1256
  }
1252
1257
  return this.flushPromise;
1253
1258
  }
1259
+ getCustomHeaders() {
1260
+ // Don't set the user agent if we're not on a browser. The latest spec allows
1261
+ // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1262
+ // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1263
+ // but browsers such as Chrome and Safari have not caught up.
1264
+ const customUserAgent = this.getCustomUserAgent();
1265
+ const headers = {};
1266
+ if (customUserAgent && customUserAgent !== '') {
1267
+ headers['User-Agent'] = customUserAgent;
1268
+ }
1269
+ return headers;
1270
+ }
1254
1271
  async _flush() {
1255
1272
  this.clearFlushTimer();
1256
1273
  await this._initPromise;
@@ -1269,11 +1286,6 @@ class PostHogCoreStateless {
1269
1286
  batch: messages,
1270
1287
  sent_at: currentISOTime(),
1271
1288
  };
1272
- // Don't set the user agent if we're not on a browser. The latest spec allows
1273
- // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1274
- // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1275
- // but browsers such as Chrome and Safari have not caught up.
1276
- this.getCustomUserAgent();
1277
1289
  const payload = JSON.stringify(data);
1278
1290
  const url = this.captureMode === 'form'
1279
1291
  ? `${this.host}/e/?ip=1&_=${currentTimestamp()}&v=${this.getLibraryVersion()}`
@@ -1283,12 +1295,12 @@ class PostHogCoreStateless {
1283
1295
  method: 'POST',
1284
1296
  mode: 'no-cors',
1285
1297
  credentials: 'omit',
1286
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1298
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' },
1287
1299
  body: `data=${encodeURIComponent(LZString.compressToBase64(payload))}&compression=lz64`,
1288
1300
  }
1289
1301
  : {
1290
1302
  method: 'POST',
1291
- headers: { 'Content-Type': 'application/json' },
1303
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1292
1304
  body: payload,
1293
1305
  };
1294
1306
  try {
@@ -1830,7 +1842,7 @@ class PostHogCore extends PostHogCoreStateless {
1830
1842
  }
1831
1843
  }
1832
1844
 
1833
- var version = "3.0.0-beta.2";
1845
+ var version = "3.0.0";
1834
1846
 
1835
1847
  function getContext(window) {
1836
1848
  let context = {};