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 +34 -9
- package/lib/index.cjs.js +21 -9
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +8 -0
- package/lib/index.esm.js +21 -9
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +4 -0
- package/lib/posthog-core/src/types.d.ts +4 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ declare type PostHogCoreOptions = {
|
|
|
7
7
|
flushInterval?: number;
|
|
8
8
|
/** The maximum number of queued messages to be flushed as part of a single batch (must be higher than `flushAt`) */
|
|
9
9
|
maxBatchSize?: number;
|
|
10
|
+
/** The maximum number of cached messages either in memory or on the local storage.
|
|
11
|
+
* Defaults to 1000, (must be higher than `flushAt`)
|
|
12
|
+
*/
|
|
13
|
+
maxQueueSize?: number;
|
|
10
14
|
/** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
|
|
11
15
|
disabled?: boolean;
|
|
12
16
|
/** If set to false the SDK will not track until the `optIn` function is called. */
|
|
@@ -130,6 +134,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
130
134
|
host: string;
|
|
131
135
|
private flushAt;
|
|
132
136
|
private maxBatchSize;
|
|
137
|
+
private maxQueueSize;
|
|
133
138
|
private flushInterval;
|
|
134
139
|
private flushPromise;
|
|
135
140
|
private requestTimeout;
|
|
@@ -200,6 +205,9 @@ declare abstract class PostHogCoreStateless {
|
|
|
200
205
|
*/
|
|
201
206
|
private flushBackground;
|
|
202
207
|
flush(): Promise<any[]>;
|
|
208
|
+
protected getCustomHeaders(): {
|
|
209
|
+
[key: string]: string;
|
|
210
|
+
};
|
|
203
211
|
private _flush;
|
|
204
212
|
private fetchWithRetry;
|
|
205
213
|
shutdown(shutdownTimeoutMs?: number): Promise<void>;
|
package/lib/index.esm.js
CHANGED
|
@@ -955,6 +955,7 @@ class PostHogCoreStateless {
|
|
|
955
955
|
this.host = removeTrailingSlash(options?.host || 'https://app.posthog.com');
|
|
956
956
|
this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
|
|
957
957
|
this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
|
|
958
|
+
this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
|
|
958
959
|
this.flushInterval = options?.flushInterval ?? 10000;
|
|
959
960
|
this.captureMode = options?.captureMode || 'form';
|
|
960
961
|
// If enable is explicitly set to false we override the optout
|
|
@@ -1103,7 +1104,7 @@ class PostHogCoreStateless {
|
|
|
1103
1104
|
const url = `${this.host}/decide/?v=3`;
|
|
1104
1105
|
const fetchOptions = {
|
|
1105
1106
|
method: 'POST',
|
|
1106
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1107
|
+
headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
|
|
1107
1108
|
body: JSON.stringify({
|
|
1108
1109
|
token: this.apiKey,
|
|
1109
1110
|
distinct_id: distinctId,
|
|
@@ -1213,6 +1214,10 @@ class PostHogCoreStateless {
|
|
|
1213
1214
|
delete message.distinctId;
|
|
1214
1215
|
}
|
|
1215
1216
|
const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1217
|
+
if (queue.length >= this.maxQueueSize) {
|
|
1218
|
+
queue.shift();
|
|
1219
|
+
console.info('Queue is full, the oldest event is dropped.');
|
|
1220
|
+
}
|
|
1216
1221
|
queue.push({ message });
|
|
1217
1222
|
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
1218
1223
|
this._events.emit(type, message);
|
|
@@ -1247,6 +1252,18 @@ class PostHogCoreStateless {
|
|
|
1247
1252
|
}
|
|
1248
1253
|
return this.flushPromise;
|
|
1249
1254
|
}
|
|
1255
|
+
getCustomHeaders() {
|
|
1256
|
+
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1257
|
+
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1258
|
+
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1259
|
+
// but browsers such as Chrome and Safari have not caught up.
|
|
1260
|
+
const customUserAgent = this.getCustomUserAgent();
|
|
1261
|
+
const headers = {};
|
|
1262
|
+
if (customUserAgent && customUserAgent !== '') {
|
|
1263
|
+
headers['User-Agent'] = customUserAgent;
|
|
1264
|
+
}
|
|
1265
|
+
return headers;
|
|
1266
|
+
}
|
|
1250
1267
|
async _flush() {
|
|
1251
1268
|
this.clearFlushTimer();
|
|
1252
1269
|
await this._initPromise;
|
|
@@ -1265,11 +1282,6 @@ class PostHogCoreStateless {
|
|
|
1265
1282
|
batch: messages,
|
|
1266
1283
|
sent_at: currentISOTime(),
|
|
1267
1284
|
};
|
|
1268
|
-
// Don't set the user agent if we're not on a browser. The latest spec allows
|
|
1269
|
-
// the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
|
|
1270
|
-
// and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
|
|
1271
|
-
// but browsers such as Chrome and Safari have not caught up.
|
|
1272
|
-
this.getCustomUserAgent();
|
|
1273
1285
|
const payload = JSON.stringify(data);
|
|
1274
1286
|
const url = this.captureMode === 'form'
|
|
1275
1287
|
? `${this.host}/e/?ip=1&_=${currentTimestamp()}&v=${this.getLibraryVersion()}`
|
|
@@ -1279,12 +1291,12 @@ class PostHogCoreStateless {
|
|
|
1279
1291
|
method: 'POST',
|
|
1280
1292
|
mode: 'no-cors',
|
|
1281
1293
|
credentials: 'omit',
|
|
1282
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1294
|
+
headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
1283
1295
|
body: `data=${encodeURIComponent(LZString.compressToBase64(payload))}&compression=lz64`,
|
|
1284
1296
|
}
|
|
1285
1297
|
: {
|
|
1286
1298
|
method: 'POST',
|
|
1287
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1299
|
+
headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
|
|
1288
1300
|
body: payload,
|
|
1289
1301
|
};
|
|
1290
1302
|
try {
|
|
@@ -1826,7 +1838,7 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
1826
1838
|
}
|
|
1827
1839
|
}
|
|
1828
1840
|
|
|
1829
|
-
var version = "3.0.0
|
|
1841
|
+
var version = "3.0.0";
|
|
1830
1842
|
|
|
1831
1843
|
function getContext(window) {
|
|
1832
1844
|
let context = {};
|