posthog-js-lite 3.0.0-beta.2 → 3.0.1

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,46 @@
1
+ # 3.0.1 - 2024-04-25
2
+
3
+ 1. Prevent double JSON parsing of feature flag payloads, which would convert the payload [1] into 1.
4
+
5
+ # 3.0.0 - 2024-03-18
6
+
7
+ ## Added
8
+
9
+ 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
10
+ 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.
11
+ 3. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 10 seconds.
12
+ 4. Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
13
+ 5. Queued events are limited up to `maxQueueSize` (default 1000) and the oldest events are dropped when the limit is reached
14
+
15
+ ## Removed
16
+
17
+ 1. Removes the `enable` option. You can now specify `defaultOptIn: false` to start the SDK opted out of tracking
18
+ 2. `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
19
+
20
+ ## Changed
21
+
22
+ 1. `flush` and `shutdown` now being async methods.
23
+ 2. Many methods such as `capture` and `identify` no longer return the `this` object instead returning nothing
24
+
25
+ ## Fixed
26
+
27
+ 1. Fixed an issue where `shutdown` would potentially exit early if a flush was already in progress
28
+ 2. Fixes some typos in types
29
+
1
30
  # 3.0.0-beta.2 - 2024-03-12
2
31
 
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
32
+ 1. `flushAsync` and `shutdownAsync` are removed with `flush` and `shutdown` now being the async methods.
33
+ 2. Fixed an issue where `shutdownAsync` would potentially exit early if a flush was already in progress
34
+ 3. Flushes will now try to flush up to `maxBatchSize` (default 100) in one go
6
35
 
7
36
  # 3.0.0-beta.1 - 2024-03-04
8
37
 
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.
38
+ 1. Removes the `enable` option. You can now specify `defaultOptIn: false` to start the SDK opted out of tracking
39
+ 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
40
+ 3. Many methods such as `capture` and `identify` no longer return the `this` object instead returning nothing
41
+ 4. Fixes some typos in types
42
+ 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.
43
+ 6. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 10 seconds.
15
44
 
16
45
  # 2.6.2 - 2024-02-15
17
46
 
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,
@@ -1152,14 +1153,11 @@ class PostHogCoreStateless {
1152
1153
  if (response === undefined) {
1153
1154
  return null;
1154
1155
  }
1155
- return this._parsePayload(response);
1156
+ return response;
1156
1157
  }
1157
1158
  async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1158
1159
  await this._initPromise;
1159
1160
  const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)).payloads;
1160
- if (payloads) {
1161
- return Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1162
- }
1163
1161
  return payloads;
1164
1162
  }
1165
1163
  _parsePayload(response) {
@@ -1183,9 +1181,13 @@ class PostHogCoreStateless {
1183
1181
  const decideResponse = await this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload);
1184
1182
  const flags = decideResponse?.featureFlags;
1185
1183
  const payloads = decideResponse?.featureFlagPayloads;
1184
+ let parsedPayloads = payloads;
1185
+ if (payloads) {
1186
+ parsedPayloads = Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1187
+ }
1186
1188
  return {
1187
1189
  flags,
1188
- payloads,
1190
+ payloads: parsedPayloads,
1189
1191
  };
1190
1192
  }
1191
1193
  /***
@@ -1217,6 +1219,10 @@ class PostHogCoreStateless {
1217
1219
  delete message.distinctId;
1218
1220
  }
1219
1221
  const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1222
+ if (queue.length >= this.maxQueueSize) {
1223
+ queue.shift();
1224
+ console.info('Queue is full, the oldest event is dropped.');
1225
+ }
1220
1226
  queue.push({ message });
1221
1227
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1222
1228
  this._events.emit(type, message);
@@ -1251,6 +1257,18 @@ class PostHogCoreStateless {
1251
1257
  }
1252
1258
  return this.flushPromise;
1253
1259
  }
1260
+ getCustomHeaders() {
1261
+ // Don't set the user agent if we're not on a browser. The latest spec allows
1262
+ // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1263
+ // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1264
+ // but browsers such as Chrome and Safari have not caught up.
1265
+ const customUserAgent = this.getCustomUserAgent();
1266
+ const headers = {};
1267
+ if (customUserAgent && customUserAgent !== '') {
1268
+ headers['User-Agent'] = customUserAgent;
1269
+ }
1270
+ return headers;
1271
+ }
1254
1272
  async _flush() {
1255
1273
  this.clearFlushTimer();
1256
1274
  await this._initPromise;
@@ -1269,11 +1287,6 @@ class PostHogCoreStateless {
1269
1287
  batch: messages,
1270
1288
  sent_at: currentISOTime(),
1271
1289
  };
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
1290
  const payload = JSON.stringify(data);
1278
1291
  const url = this.captureMode === 'form'
1279
1292
  ? `${this.host}/e/?ip=1&_=${currentTimestamp()}&v=${this.getLibraryVersion()}`
@@ -1283,12 +1296,12 @@ class PostHogCoreStateless {
1283
1296
  method: 'POST',
1284
1297
  mode: 'no-cors',
1285
1298
  credentials: 'omit',
1286
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1299
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' },
1287
1300
  body: `data=${encodeURIComponent(LZString.compressToBase64(payload))}&compression=lz64`,
1288
1301
  }
1289
1302
  : {
1290
1303
  method: 'POST',
1291
- headers: { 'Content-Type': 'application/json' },
1304
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1292
1305
  body: payload,
1293
1306
  };
1294
1307
  try {
@@ -1692,7 +1705,7 @@ class PostHogCore extends PostHogCoreStateless {
1692
1705
  newFeatureFlagPayloads = { ...currentFlagPayloads, ...res.featureFlagPayloads };
1693
1706
  }
1694
1707
  this.setKnownFeatureFlags(newFeatureFlags);
1695
- this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads);
1708
+ this.setKnownFeatureFlagPayloads(Object.fromEntries(Object.entries(newFeatureFlagPayloads || {}).map(([k, v]) => [k, this._parsePayload(v)])));
1696
1709
  }
1697
1710
  return res;
1698
1711
  });
@@ -1745,13 +1758,10 @@ class PostHogCore extends PostHogCoreStateless {
1745
1758
  if (response === undefined) {
1746
1759
  return null;
1747
1760
  }
1748
- return this._parsePayload(response);
1761
+ return response;
1749
1762
  }
1750
1763
  getFeatureFlagPayloads() {
1751
1764
  const payloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1752
- if (payloads) {
1753
- return Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1754
- }
1755
1765
  return payloads;
1756
1766
  }
1757
1767
  getFeatureFlags() {
@@ -1830,7 +1840,7 @@ class PostHogCore extends PostHogCoreStateless {
1830
1840
  }
1831
1841
  }
1832
1842
 
1833
- var version = "3.0.0-beta.2";
1843
+ var version = "3.0.1";
1834
1844
 
1835
1845
  function getContext(window) {
1836
1846
  let context = {};