posthog-node 4.0.0-beta.2 → 4.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,15 +1,44 @@
1
+ # 4.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. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
7
+ 3. `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.
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
+ 6. Sets `User-Agent` headers with SDK name and version for RN
11
+
12
+ ## Removed
13
+
14
+ 1. `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
+
20
+ ## Fixed
21
+
22
+ 1. Fixed an issue where `shutdown` would potentially exit early if a flush was already in progress
23
+ 2. Fixes some typos in types
24
+
25
+
26
+ # 4.0.0-beta.3 - 2024-03-13
27
+
28
+ 1. Sets `User-Agent` headers with SDK name and version for RN
29
+
1
30
  # 4.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 `shutdown` 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 `shutdown` 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
  # 4.0.0-beta.1 - 2024-03-04
8
37
 
9
- - 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
- - Fixes some typos in types
11
- - `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.
12
- - Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
38
+ 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
39
+ 2. Fixes some typos in types
40
+ 3. `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.
41
+ 4. Adds a new `featureFlagsRequestTimeoutMs` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
13
42
 
14
43
  # 3.6.3 - 2024-02-15
15
44
 
package/lib/index.cjs.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
6
 
7
- var version = "4.0.0-beta.2";
7
+ var version = "4.0.0";
8
8
 
9
9
  var PostHogPersistedProperty;
10
10
  (function (PostHogPersistedProperty) {
@@ -963,6 +963,7 @@ class PostHogCoreStateless {
963
963
  this.host = removeTrailingSlash(options?.host || 'https://app.posthog.com');
964
964
  this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
965
965
  this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
966
+ this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
966
967
  this.flushInterval = options?.flushInterval ?? 10000;
967
968
  this.captureMode = options?.captureMode || 'form';
968
969
  // If enable is explicitly set to false we override the optout
@@ -1111,7 +1112,7 @@ class PostHogCoreStateless {
1111
1112
  const url = `${this.host}/decide/?v=3`;
1112
1113
  const fetchOptions = {
1113
1114
  method: 'POST',
1114
- headers: { 'Content-Type': 'application/json' },
1115
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1115
1116
  body: JSON.stringify({
1116
1117
  token: this.apiKey,
1117
1118
  distinct_id: distinctId,
@@ -1221,6 +1222,10 @@ class PostHogCoreStateless {
1221
1222
  delete message.distinctId;
1222
1223
  }
1223
1224
  const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1225
+ if (queue.length >= this.maxQueueSize) {
1226
+ queue.shift();
1227
+ console.info('Queue is full, the oldest event is dropped.');
1228
+ }
1224
1229
  queue.push({ message });
1225
1230
  this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1226
1231
  this._events.emit(type, message);
@@ -1255,6 +1260,18 @@ class PostHogCoreStateless {
1255
1260
  }
1256
1261
  return this.flushPromise;
1257
1262
  }
1263
+ getCustomHeaders() {
1264
+ // Don't set the user agent if we're not on a browser. The latest spec allows
1265
+ // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1266
+ // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1267
+ // but browsers such as Chrome and Safari have not caught up.
1268
+ const customUserAgent = this.getCustomUserAgent();
1269
+ const headers = {};
1270
+ if (customUserAgent && customUserAgent !== '') {
1271
+ headers['User-Agent'] = customUserAgent;
1272
+ }
1273
+ return headers;
1274
+ }
1258
1275
  async _flush() {
1259
1276
  this.clearFlushTimer();
1260
1277
  await this._initPromise;
@@ -1273,11 +1290,6 @@ class PostHogCoreStateless {
1273
1290
  batch: messages,
1274
1291
  sent_at: currentISOTime(),
1275
1292
  };
1276
- // Don't set the user agent if we're not on a browser. The latest spec allows
1277
- // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1278
- // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1279
- // but browsers such as Chrome and Safari have not caught up.
1280
- this.getCustomUserAgent();
1281
1293
  const payload = JSON.stringify(data);
1282
1294
  const url = this.captureMode === 'form'
1283
1295
  ? `${this.host}/e/?ip=1&_=${currentTimestamp()}&v=${this.getLibraryVersion()}`
@@ -1287,12 +1299,12 @@ class PostHogCoreStateless {
1287
1299
  method: 'POST',
1288
1300
  mode: 'no-cors',
1289
1301
  credentials: 'omit',
1290
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1302
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' },
1291
1303
  body: `data=${encodeURIComponent(LZString.compressToBase64(payload))}&compression=lz64`,
1292
1304
  }
1293
1305
  : {
1294
1306
  method: 'POST',
1295
- headers: { 'Content-Type': 'application/json' },
1307
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1296
1308
  body: payload,
1297
1309
  };
1298
1310
  try {
@@ -1453,6 +1465,7 @@ class FeatureFlagsPoller {
1453
1465
  projectApiKey,
1454
1466
  timeout,
1455
1467
  host,
1468
+ customHeaders,
1456
1469
  ...options
1457
1470
  }) {
1458
1471
  this.debugMode = false;
@@ -1470,6 +1483,7 @@ class FeatureFlagsPoller {
1470
1483
 
1471
1484
  this.fetch = options.fetch || fetch$1;
1472
1485
  this.onError = options.onError;
1486
+ this.customHeaders = customHeaders;
1473
1487
  void this.loadFeatureFlags();
1474
1488
  }
1475
1489
 
@@ -1772,10 +1786,9 @@ class FeatureFlagsPoller {
1772
1786
  const url = `${this.host}/api/feature_flag/local_evaluation?token=${this.projectApiKey}&send_cohorts`;
1773
1787
  const options = {
1774
1788
  method: 'GET',
1775
- headers: {
1789
+ headers: { ...this.customHeaders,
1776
1790
  'Content-Type': 'application/json',
1777
- Authorization: `Bearer ${this.personalApiKey}`,
1778
- 'user-agent': `posthog-node/${version}`
1791
+ Authorization: `Bearer ${this.personalApiKey}`
1779
1792
  }
1780
1793
  };
1781
1794
  let abortTimeout = null;
@@ -2117,7 +2130,8 @@ class PostHog extends PostHogCoreStateless {
2117
2130
  fetch: options.fetch,
2118
2131
  onError: err => {
2119
2132
  this._events.emit('error', err);
2120
- }
2133
+ },
2134
+ customHeaders: this.getCustomHeaders()
2121
2135
  });
2122
2136
  }
2123
2137
 
@@ -2146,7 +2160,7 @@ class PostHog extends PostHogCoreStateless {
2146
2160
  }
2147
2161
 
2148
2162
  getCustomUserAgent() {
2149
- return `posthog-node/${version}`;
2163
+ return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
2150
2164
  }
2151
2165
 
2152
2166
  enable() {