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/lib/index.d.ts CHANGED
@@ -8,6 +8,10 @@ declare type PostHogCoreOptions = {
8
8
  flushInterval?: number;
9
9
  /** The maximum number of queued messages to be flushed as part of a single batch (must be higher than `flushAt`) */
10
10
  maxBatchSize?: number;
11
+ /** The maximum number of cached messages either in memory or on the local storage.
12
+ * Defaults to 1000, (must be higher than `flushAt`)
13
+ */
14
+ maxQueueSize?: number;
11
15
  /** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
12
16
  disabled?: boolean;
13
17
  /** If set to false the SDK will not track until the `optIn` function is called. */
@@ -125,6 +129,7 @@ declare abstract class PostHogCoreStateless {
125
129
  host: string;
126
130
  private flushAt;
127
131
  private maxBatchSize;
132
+ private maxQueueSize;
128
133
  private flushInterval;
129
134
  private flushPromise;
130
135
  private requestTimeout;
@@ -195,6 +200,9 @@ declare abstract class PostHogCoreStateless {
195
200
  */
196
201
  private flushBackground;
197
202
  flush(): Promise<any[]>;
203
+ protected getCustomHeaders(): {
204
+ [key: string]: string;
205
+ };
198
206
  private _flush;
199
207
  private fetchWithRetry;
200
208
  shutdown(shutdownTimeoutMs?: number): Promise<void>;
package/lib/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'rusha';
2
2
 
3
- var version = "4.0.0-beta.2";
3
+ var version = "4.0.0";
4
4
 
5
5
  var PostHogPersistedProperty;
6
6
  (function (PostHogPersistedProperty) {
@@ -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 {
@@ -1449,6 +1461,7 @@ class FeatureFlagsPoller {
1449
1461
  projectApiKey,
1450
1462
  timeout,
1451
1463
  host,
1464
+ customHeaders,
1452
1465
  ...options
1453
1466
  }) {
1454
1467
  this.debugMode = false;
@@ -1466,6 +1479,7 @@ class FeatureFlagsPoller {
1466
1479
 
1467
1480
  this.fetch = options.fetch || fetch$1;
1468
1481
  this.onError = options.onError;
1482
+ this.customHeaders = customHeaders;
1469
1483
  void this.loadFeatureFlags();
1470
1484
  }
1471
1485
 
@@ -1768,10 +1782,9 @@ class FeatureFlagsPoller {
1768
1782
  const url = `${this.host}/api/feature_flag/local_evaluation?token=${this.projectApiKey}&send_cohorts`;
1769
1783
  const options = {
1770
1784
  method: 'GET',
1771
- headers: {
1785
+ headers: { ...this.customHeaders,
1772
1786
  'Content-Type': 'application/json',
1773
- Authorization: `Bearer ${this.personalApiKey}`,
1774
- 'user-agent': `posthog-node/${version}`
1787
+ Authorization: `Bearer ${this.personalApiKey}`
1775
1788
  }
1776
1789
  };
1777
1790
  let abortTimeout = null;
@@ -2113,7 +2126,8 @@ class PostHog extends PostHogCoreStateless {
2113
2126
  fetch: options.fetch,
2114
2127
  onError: err => {
2115
2128
  this._events.emit('error', err);
2116
- }
2129
+ },
2130
+ customHeaders: this.getCustomHeaders()
2117
2131
  });
2118
2132
  }
2119
2133
 
@@ -2142,7 +2156,7 @@ class PostHog extends PostHogCoreStateless {
2142
2156
  }
2143
2157
 
2144
2158
  getCustomUserAgent() {
2145
- return `posthog-node/${version}`;
2159
+ return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
2146
2160
  }
2147
2161
 
2148
2162
  enable() {