posthog-js-lite 4.1.0 → 4.1.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/lib/index.cjs DELETED
@@ -1,2641 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var version = "4.1.0";
6
-
7
- var PostHogPersistedProperty;
8
- (function (PostHogPersistedProperty) {
9
- PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
10
- PostHogPersistedProperty["DistinctId"] = "distinct_id";
11
- PostHogPersistedProperty["Props"] = "props";
12
- PostHogPersistedProperty["FeatureFlagDetails"] = "feature_flag_details";
13
- PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
14
- PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
15
- PostHogPersistedProperty["BootstrapFeatureFlagDetails"] = "bootstrap_feature_flag_details";
16
- PostHogPersistedProperty["BootstrapFeatureFlags"] = "bootstrap_feature_flags";
17
- PostHogPersistedProperty["BootstrapFeatureFlagPayloads"] = "bootstrap_feature_flag_payloads";
18
- PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
19
- PostHogPersistedProperty["Queue"] = "queue";
20
- PostHogPersistedProperty["OptedOut"] = "opted_out";
21
- PostHogPersistedProperty["SessionId"] = "session_id";
22
- PostHogPersistedProperty["SessionStartTimestamp"] = "session_start_timestamp";
23
- PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
24
- PostHogPersistedProperty["PersonProperties"] = "person_properties";
25
- PostHogPersistedProperty["GroupProperties"] = "group_properties";
26
- PostHogPersistedProperty["InstalledAppBuild"] = "installed_app_build";
27
- PostHogPersistedProperty["InstalledAppVersion"] = "installed_app_version";
28
- PostHogPersistedProperty["SessionReplay"] = "session_replay";
29
- PostHogPersistedProperty["SurveyLastSeenDate"] = "survey_last_seen_date";
30
- PostHogPersistedProperty["SurveysSeen"] = "surveys_seen";
31
- PostHogPersistedProperty["Surveys"] = "surveys";
32
- PostHogPersistedProperty["RemoteConfig"] = "remote_config";
33
- PostHogPersistedProperty["FlagsEndpointWasHit"] = "flags_endpoint_was_hit";
34
- })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
35
- // Any key prefixed with `attr__` can be added
36
- var Compression;
37
- (function (Compression) {
38
- Compression["GZipJS"] = "gzip-js";
39
- Compression["Base64"] = "base64";
40
- })(Compression || (Compression = {}));
41
- var SurveyPosition;
42
- (function (SurveyPosition) {
43
- SurveyPosition["Left"] = "left";
44
- SurveyPosition["Right"] = "right";
45
- SurveyPosition["Center"] = "center";
46
- })(SurveyPosition || (SurveyPosition = {}));
47
- var SurveyWidgetType;
48
- (function (SurveyWidgetType) {
49
- SurveyWidgetType["Button"] = "button";
50
- SurveyWidgetType["Tab"] = "tab";
51
- SurveyWidgetType["Selector"] = "selector";
52
- })(SurveyWidgetType || (SurveyWidgetType = {}));
53
- var SurveyType;
54
- (function (SurveyType) {
55
- SurveyType["Popover"] = "popover";
56
- SurveyType["API"] = "api";
57
- SurveyType["Widget"] = "widget";
58
- })(SurveyType || (SurveyType = {}));
59
- var SurveyQuestionDescriptionContentType;
60
- (function (SurveyQuestionDescriptionContentType) {
61
- SurveyQuestionDescriptionContentType["Html"] = "html";
62
- SurveyQuestionDescriptionContentType["Text"] = "text";
63
- })(SurveyQuestionDescriptionContentType || (SurveyQuestionDescriptionContentType = {}));
64
- var SurveyRatingDisplay;
65
- (function (SurveyRatingDisplay) {
66
- SurveyRatingDisplay["Number"] = "number";
67
- SurveyRatingDisplay["Emoji"] = "emoji";
68
- })(SurveyRatingDisplay || (SurveyRatingDisplay = {}));
69
- var SurveyQuestionType;
70
- (function (SurveyQuestionType) {
71
- SurveyQuestionType["Open"] = "open";
72
- SurveyQuestionType["MultipleChoice"] = "multiple_choice";
73
- SurveyQuestionType["SingleChoice"] = "single_choice";
74
- SurveyQuestionType["Rating"] = "rating";
75
- SurveyQuestionType["Link"] = "link";
76
- })(SurveyQuestionType || (SurveyQuestionType = {}));
77
- var SurveyQuestionBranchingType;
78
- (function (SurveyQuestionBranchingType) {
79
- SurveyQuestionBranchingType["NextQuestion"] = "next_question";
80
- SurveyQuestionBranchingType["End"] = "end";
81
- SurveyQuestionBranchingType["ResponseBased"] = "response_based";
82
- SurveyQuestionBranchingType["SpecificQuestion"] = "specific_question";
83
- })(SurveyQuestionBranchingType || (SurveyQuestionBranchingType = {}));
84
- var SurveyMatchType;
85
- (function (SurveyMatchType) {
86
- SurveyMatchType["Regex"] = "regex";
87
- SurveyMatchType["NotRegex"] = "not_regex";
88
- SurveyMatchType["Exact"] = "exact";
89
- SurveyMatchType["IsNot"] = "is_not";
90
- SurveyMatchType["Icontains"] = "icontains";
91
- SurveyMatchType["NotIcontains"] = "not_icontains";
92
- })(SurveyMatchType || (SurveyMatchType = {}));
93
- /** Sync with plugin-server/src/types.ts */
94
- var ActionStepStringMatching;
95
- (function (ActionStepStringMatching) {
96
- ActionStepStringMatching["Contains"] = "contains";
97
- ActionStepStringMatching["Exact"] = "exact";
98
- ActionStepStringMatching["Regex"] = "regex";
99
- })(ActionStepStringMatching || (ActionStepStringMatching = {}));
100
-
101
- const normalizeFlagsResponse = (flagsResponse) => {
102
- if ('flags' in flagsResponse) {
103
- // Convert v2 format to v1 format
104
- const featureFlags = getFlagValuesFromFlags(flagsResponse.flags);
105
- const featureFlagPayloads = getPayloadsFromFlags(flagsResponse.flags);
106
- return {
107
- ...flagsResponse,
108
- featureFlags,
109
- featureFlagPayloads,
110
- };
111
- }
112
- else {
113
- // Convert v1 format to v2 format
114
- const featureFlags = flagsResponse.featureFlags ?? {};
115
- const featureFlagPayloads = Object.fromEntries(Object.entries(flagsResponse.featureFlagPayloads || {}).map(([k, v]) => [k, parsePayload(v)]));
116
- const flags = Object.fromEntries(Object.entries(featureFlags).map(([key, value]) => [
117
- key,
118
- getFlagDetailFromFlagAndPayload(key, value, featureFlagPayloads[key]),
119
- ]));
120
- return {
121
- ...flagsResponse,
122
- featureFlags,
123
- featureFlagPayloads,
124
- flags,
125
- };
126
- }
127
- };
128
- function getFlagDetailFromFlagAndPayload(key, value, payload) {
129
- return {
130
- key: key,
131
- enabled: typeof value === 'string' ? true : value,
132
- variant: typeof value === 'string' ? value : undefined,
133
- reason: undefined,
134
- metadata: {
135
- id: undefined,
136
- version: undefined,
137
- payload: payload ? JSON.stringify(payload) : undefined,
138
- description: undefined,
139
- },
140
- };
141
- }
142
- /**
143
- * Get the flag values from the flags v4 response.
144
- * @param flags - The flags
145
- * @returns The flag values
146
- */
147
- const getFlagValuesFromFlags = (flags) => {
148
- return Object.fromEntries(Object.entries(flags ?? {})
149
- .map(([key, detail]) => [key, getFeatureFlagValue(detail)])
150
- .filter(([, value]) => value !== undefined));
151
- };
152
- /**
153
- * Get the payloads from the flags v4 response.
154
- * @param flags - The flags
155
- * @returns The payloads
156
- */
157
- const getPayloadsFromFlags = (flags) => {
158
- const safeFlags = flags ?? {};
159
- return Object.fromEntries(Object.keys(safeFlags)
160
- .filter((flag) => {
161
- const details = safeFlags[flag];
162
- return details.enabled && details.metadata && details.metadata.payload !== undefined;
163
- })
164
- .map((flag) => {
165
- const payload = safeFlags[flag].metadata?.payload;
166
- return [flag, payload ? parsePayload(payload) : undefined];
167
- }));
168
- };
169
- const getFeatureFlagValue = (detail) => {
170
- return detail === undefined ? undefined : detail.variant ?? detail.enabled;
171
- };
172
- const parsePayload = (response) => {
173
- if (typeof response !== 'string') {
174
- return response;
175
- }
176
- try {
177
- return JSON.parse(response);
178
- }
179
- catch {
180
- return response;
181
- }
182
- };
183
- /**
184
- * Get the normalized flag details from the flags and payloads.
185
- * This is used to convert things like boostrap and stored feature flags and payloads to the v4 format.
186
- * This helps us ensure backwards compatibility.
187
- * If a key exists in the featureFlagPayloads that is not in the featureFlags, we treat it as a true feature flag.
188
- *
189
- * @param featureFlags - The feature flags
190
- * @param featureFlagPayloads - The feature flag payloads
191
- * @returns The normalized flag details
192
- */
193
- const createFlagsResponseFromFlagsAndPayloads = (featureFlags, featureFlagPayloads) => {
194
- // If a feature flag payload key is not in the feature flags, we treat it as true feature flag.
195
- const allKeys = [...new Set([...Object.keys(featureFlags ?? {}), ...Object.keys(featureFlagPayloads ?? {})])];
196
- const enabledFlags = allKeys
197
- .filter((flag) => !!featureFlags[flag] || !!featureFlagPayloads[flag])
198
- .reduce((res, key) => ((res[key] = featureFlags[key] ?? true), res), {});
199
- const flagDetails = {
200
- featureFlags: enabledFlags,
201
- featureFlagPayloads: featureFlagPayloads ?? {},
202
- };
203
- return normalizeFlagsResponse(flagDetails);
204
- };
205
- const updateFlagValue = (flag, value) => {
206
- return {
207
- ...flag,
208
- enabled: getEnabledFromValue(value),
209
- variant: getVariantFromValue(value),
210
- };
211
- };
212
- function getEnabledFromValue(value) {
213
- return typeof value === 'string' ? true : value;
214
- }
215
- function getVariantFromValue(value) {
216
- return typeof value === 'string' ? value : undefined;
217
- }
218
-
219
- const STRING_FORMAT = 'utf8';
220
- function assert(truthyValue, message) {
221
- if (!truthyValue || typeof truthyValue !== 'string' || isEmpty(truthyValue)) {
222
- throw new Error(message);
223
- }
224
- }
225
- function isEmpty(truthyValue) {
226
- if (truthyValue.trim().length === 0) {
227
- return true;
228
- }
229
- return false;
230
- }
231
- function removeTrailingSlash(url) {
232
- return url?.replace(/\/+$/, '');
233
- }
234
- async function retriable(fn, props) {
235
- let lastError = null;
236
- for (let i = 0; i < props.retryCount + 1; i++) {
237
- if (i > 0) {
238
- // don't wait when it's the last try
239
- await new Promise((r) => setTimeout(r, props.retryDelay));
240
- }
241
- try {
242
- const res = await fn();
243
- return res;
244
- }
245
- catch (e) {
246
- lastError = e;
247
- if (!props.retryCheck(e)) {
248
- throw e;
249
- }
250
- }
251
- }
252
- throw lastError;
253
- }
254
- function currentTimestamp() {
255
- return new Date().getTime();
256
- }
257
- function currentISOTime() {
258
- return new Date().toISOString();
259
- }
260
- function safeSetTimeout(fn, timeout) {
261
- // NOTE: we use this so rarely that it is totally fine to do `safeSetTimeout(fn, 0)``
262
- // rather than setImmediate.
263
- const t = setTimeout(fn, timeout);
264
- // We unref if available to prevent Node.js hanging on exit
265
- t?.unref && t?.unref();
266
- return t;
267
- }
268
- const isError = (x) => {
269
- return x instanceof Error;
270
- };
271
- function getFetch() {
272
- return typeof fetch !== 'undefined' ? fetch : typeof globalThis.fetch !== 'undefined' ? globalThis.fetch : undefined;
273
- }
274
- function allSettled(promises) {
275
- return Promise.all(promises.map((p) => (p ?? Promise.resolve()).then((value) => ({ status: 'fulfilled', value }), (reason) => ({ status: 'rejected', reason }))));
276
- }
277
-
278
- /**
279
- * Older browsers and some runtimes don't support this yet
280
- * This API (as of 2025-05-07) is not available on React Native.
281
- */
282
- function isGzipSupported() {
283
- return 'CompressionStream' in globalThis;
284
- }
285
- /**
286
- * Gzip a string using Compression Streams API if it's available
287
- */
288
- async function gzipCompress(input, isDebug = true) {
289
- try {
290
- // Turn the string into a stream using a Blob, and then compress it
291
- const dataStream = new Blob([input], {
292
- type: 'text/plain',
293
- }).stream();
294
- const compressedStream = dataStream.pipeThrough(new CompressionStream('gzip'));
295
- // Using a Response to easily extract the readablestream value. Decoding into a string for fetch
296
- return await new Response(compressedStream).blob();
297
- }
298
- catch (error) {
299
- if (isDebug) {
300
- console.error('Failed to gzip compress data', error);
301
- }
302
- return null;
303
- }
304
- }
305
-
306
- class SimpleEventEmitter {
307
- constructor() {
308
- this.events = {};
309
- this.events = {};
310
- }
311
- on(event, listener) {
312
- if (!this.events[event]) {
313
- this.events[event] = [];
314
- }
315
- this.events[event].push(listener);
316
- return () => {
317
- this.events[event] = this.events[event].filter((x) => x !== listener);
318
- };
319
- }
320
- emit(event, payload) {
321
- for (const listener of this.events[event] || []) {
322
- listener(payload);
323
- }
324
- for (const listener of this.events['*'] || []) {
325
- listener(event, payload);
326
- }
327
- }
328
- }
329
-
330
- // vendor from: https://github.com/LiosK/uuidv7/blob/f30b7a7faff73afbce0b27a46c638310f96912ba/src/index.ts
331
- // https://github.com/LiosK/uuidv7#license
332
- /**
333
- * uuidv7: An experimental implementation of the proposed UUID Version 7
334
- *
335
- * @license Apache-2.0
336
- * @copyright 2021-2023 LiosK
337
- * @packageDocumentation
338
- */
339
- const DIGITS = "0123456789abcdef";
340
- /** Represents a UUID as a 16-byte byte array. */
341
- class UUID {
342
- /** @param bytes - The 16-byte byte array representation. */
343
- constructor(bytes) {
344
- this.bytes = bytes;
345
- }
346
- /**
347
- * Creates an object from the internal representation, a 16-byte byte array
348
- * containing the binary UUID representation in the big-endian byte order.
349
- *
350
- * This method does NOT shallow-copy the argument, and thus the created object
351
- * holds the reference to the underlying buffer.
352
- *
353
- * @throws TypeError if the length of the argument is not 16.
354
- */
355
- static ofInner(bytes) {
356
- if (bytes.length !== 16) {
357
- throw new TypeError("not 128-bit length");
358
- }
359
- else {
360
- return new UUID(bytes);
361
- }
362
- }
363
- /**
364
- * Builds a byte array from UUIDv7 field values.
365
- *
366
- * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
367
- * @param randA - A 12-bit `rand_a` field value.
368
- * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
369
- * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
370
- * @throws RangeError if any field value is out of the specified range.
371
- */
372
- static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
373
- if (!Number.isInteger(unixTsMs) ||
374
- !Number.isInteger(randA) ||
375
- !Number.isInteger(randBHi) ||
376
- !Number.isInteger(randBLo) ||
377
- unixTsMs < 0 ||
378
- randA < 0 ||
379
- randBHi < 0 ||
380
- randBLo < 0 ||
381
- unixTsMs > 281474976710655 ||
382
- randA > 0xfff ||
383
- randBHi > 1073741823 ||
384
- randBLo > 4294967295) {
385
- throw new RangeError("invalid field value");
386
- }
387
- const bytes = new Uint8Array(16);
388
- bytes[0] = unixTsMs / 2 ** 40;
389
- bytes[1] = unixTsMs / 2 ** 32;
390
- bytes[2] = unixTsMs / 2 ** 24;
391
- bytes[3] = unixTsMs / 2 ** 16;
392
- bytes[4] = unixTsMs / 2 ** 8;
393
- bytes[5] = unixTsMs;
394
- bytes[6] = 0x70 | (randA >>> 8);
395
- bytes[7] = randA;
396
- bytes[8] = 0x80 | (randBHi >>> 24);
397
- bytes[9] = randBHi >>> 16;
398
- bytes[10] = randBHi >>> 8;
399
- bytes[11] = randBHi;
400
- bytes[12] = randBLo >>> 24;
401
- bytes[13] = randBLo >>> 16;
402
- bytes[14] = randBLo >>> 8;
403
- bytes[15] = randBLo;
404
- return new UUID(bytes);
405
- }
406
- /**
407
- * Builds a byte array from a string representation.
408
- *
409
- * This method accepts the following formats:
410
- *
411
- * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
412
- * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
413
- * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
414
- * - RFC 4122 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
415
- *
416
- * Leading and trailing whitespaces represents an error.
417
- *
418
- * @throws SyntaxError if the argument could not parse as a valid UUID string.
419
- */
420
- static parse(uuid) {
421
- let hex = undefined;
422
- switch (uuid.length) {
423
- case 32:
424
- hex = /^[0-9a-f]{32}$/i.exec(uuid)?.[0];
425
- break;
426
- case 36:
427
- hex =
428
- /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
429
- .exec(uuid)
430
- ?.slice(1, 6)
431
- .join("");
432
- break;
433
- case 38:
434
- hex =
435
- /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
436
- .exec(uuid)
437
- ?.slice(1, 6)
438
- .join("");
439
- break;
440
- case 45:
441
- hex =
442
- /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
443
- .exec(uuid)
444
- ?.slice(1, 6)
445
- .join("");
446
- break;
447
- }
448
- if (hex) {
449
- const inner = new Uint8Array(16);
450
- for (let i = 0; i < 16; i += 4) {
451
- const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
452
- inner[i + 0] = n >>> 24;
453
- inner[i + 1] = n >>> 16;
454
- inner[i + 2] = n >>> 8;
455
- inner[i + 3] = n;
456
- }
457
- return new UUID(inner);
458
- }
459
- else {
460
- throw new SyntaxError("could not parse UUID string");
461
- }
462
- }
463
- /**
464
- * @returns The 8-4-4-4-12 canonical hexadecimal string representation
465
- * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
466
- */
467
- toString() {
468
- let text = "";
469
- for (let i = 0; i < this.bytes.length; i++) {
470
- text += DIGITS.charAt(this.bytes[i] >>> 4);
471
- text += DIGITS.charAt(this.bytes[i] & 0xf);
472
- if (i === 3 || i === 5 || i === 7 || i === 9) {
473
- text += "-";
474
- }
475
- }
476
- return text;
477
- }
478
- /**
479
- * @returns The 32-digit hexadecimal representation without hyphens
480
- * (`0189dcd553117d408db09496a2eef37b`).
481
- */
482
- toHex() {
483
- let text = "";
484
- for (let i = 0; i < this.bytes.length; i++) {
485
- text += DIGITS.charAt(this.bytes[i] >>> 4);
486
- text += DIGITS.charAt(this.bytes[i] & 0xf);
487
- }
488
- return text;
489
- }
490
- /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
491
- toJSON() {
492
- return this.toString();
493
- }
494
- /**
495
- * Reports the variant field value of the UUID or, if appropriate, "NIL" or
496
- * "MAX".
497
- *
498
- * For convenience, this method reports "NIL" or "MAX" if `this` represents
499
- * the Nil or Max UUID, although the Nil and Max UUIDs are technically
500
- * subsumed under the variants `0b0` and `0b111`, respectively.
501
- */
502
- getVariant() {
503
- const n = this.bytes[8] >>> 4;
504
- if (n < 0) {
505
- throw new Error("unreachable");
506
- }
507
- else if (n <= 0b0111) {
508
- return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
509
- }
510
- else if (n <= 0b1011) {
511
- return "VAR_10";
512
- }
513
- else if (n <= 0b1101) {
514
- return "VAR_110";
515
- }
516
- else if (n <= 0b1111) {
517
- return this.bytes.every((e) => e === 0xff) ? "MAX" : "VAR_RESERVED";
518
- }
519
- else {
520
- throw new Error("unreachable");
521
- }
522
- }
523
- /**
524
- * Returns the version field value of the UUID or `undefined` if the UUID does
525
- * not have the variant field value of `0b10`.
526
- */
527
- getVersion() {
528
- return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
529
- }
530
- /** Creates an object from `this`. */
531
- clone() {
532
- return new UUID(this.bytes.slice(0));
533
- }
534
- /** Returns true if `this` is equivalent to `other`. */
535
- equals(other) {
536
- return this.compareTo(other) === 0;
537
- }
538
- /**
539
- * Returns a negative integer, zero, or positive integer if `this` is less
540
- * than, equal to, or greater than `other`, respectively.
541
- */
542
- compareTo(other) {
543
- for (let i = 0; i < 16; i++) {
544
- const diff = this.bytes[i] - other.bytes[i];
545
- if (diff !== 0) {
546
- return Math.sign(diff);
547
- }
548
- }
549
- return 0;
550
- }
551
- }
552
- /**
553
- * Encapsulates the monotonic counter state.
554
- *
555
- * This class provides APIs to utilize a separate counter state from that of the
556
- * global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
557
- * the default {@link generate} method, this class has {@link generateOrAbort}
558
- * that is useful to absolutely guarantee the monotonically increasing order of
559
- * generated UUIDs. See their respective documentation for details.
560
- */
561
- class V7Generator {
562
- /**
563
- * Creates a generator object with the default random number generator, or
564
- * with the specified one if passed as an argument. The specified random
565
- * number generator should be cryptographically strong and securely seeded.
566
- */
567
- constructor(randomNumberGenerator) {
568
- this.timestamp = 0;
569
- this.counter = 0;
570
- this.random = randomNumberGenerator ?? getDefaultRandom();
571
- }
572
- /**
573
- * Generates a new UUIDv7 object from the current timestamp, or resets the
574
- * generator upon significant timestamp rollback.
575
- *
576
- * This method returns a monotonically increasing UUID by reusing the previous
577
- * timestamp even if the up-to-date timestamp is smaller than the immediately
578
- * preceding UUID's. However, when such a clock rollback is considered
579
- * significant (i.e., by more than ten seconds), this method resets the
580
- * generator and returns a new UUID based on the given timestamp, breaking the
581
- * increasing order of UUIDs.
582
- *
583
- * See {@link generateOrAbort} for the other mode of generation and
584
- * {@link generateOrResetCore} for the low-level primitive.
585
- */
586
- generate() {
587
- return this.generateOrResetCore(Date.now(), 10000);
588
- }
589
- /**
590
- * Generates a new UUIDv7 object from the current timestamp, or returns
591
- * `undefined` upon significant timestamp rollback.
592
- *
593
- * This method returns a monotonically increasing UUID by reusing the previous
594
- * timestamp even if the up-to-date timestamp is smaller than the immediately
595
- * preceding UUID's. However, when such a clock rollback is considered
596
- * significant (i.e., by more than ten seconds), this method aborts and
597
- * returns `undefined` immediately.
598
- *
599
- * See {@link generate} for the other mode of generation and
600
- * {@link generateOrAbortCore} for the low-level primitive.
601
- */
602
- generateOrAbort() {
603
- return this.generateOrAbortCore(Date.now(), 10000);
604
- }
605
- /**
606
- * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
607
- * generator upon significant timestamp rollback.
608
- *
609
- * This method is equivalent to {@link generate} except that it takes a custom
610
- * timestamp and clock rollback allowance.
611
- *
612
- * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
613
- * considered significant. A suggested value is `10_000` (milliseconds).
614
- * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
615
- */
616
- generateOrResetCore(unixTsMs, rollbackAllowance) {
617
- let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
618
- if (value === undefined) {
619
- // reset state and resume
620
- this.timestamp = 0;
621
- value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
622
- }
623
- return value;
624
- }
625
- /**
626
- * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
627
- * `undefined` upon significant timestamp rollback.
628
- *
629
- * This method is equivalent to {@link generateOrAbort} except that it takes a
630
- * custom timestamp and clock rollback allowance.
631
- *
632
- * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
633
- * considered significant. A suggested value is `10_000` (milliseconds).
634
- * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
635
- */
636
- generateOrAbortCore(unixTsMs, rollbackAllowance) {
637
- const MAX_COUNTER = 4398046511103;
638
- if (!Number.isInteger(unixTsMs) ||
639
- unixTsMs < 1 ||
640
- unixTsMs > 281474976710655) {
641
- throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
642
- }
643
- else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
644
- throw new RangeError("`rollbackAllowance` out of reasonable range");
645
- }
646
- if (unixTsMs > this.timestamp) {
647
- this.timestamp = unixTsMs;
648
- this.resetCounter();
649
- }
650
- else if (unixTsMs + rollbackAllowance >= this.timestamp) {
651
- // go on with previous timestamp if new one is not much smaller
652
- this.counter++;
653
- if (this.counter > MAX_COUNTER) {
654
- // increment timestamp at counter overflow
655
- this.timestamp++;
656
- this.resetCounter();
657
- }
658
- }
659
- else {
660
- // abort if clock went backwards to unbearable extent
661
- return undefined;
662
- }
663
- return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
664
- }
665
- /** Initializes the counter at a 42-bit random integer. */
666
- resetCounter() {
667
- this.counter =
668
- this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
669
- }
670
- /**
671
- * Generates a new UUIDv4 object utilizing the random number generator inside.
672
- *
673
- * @internal
674
- */
675
- generateV4() {
676
- const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
677
- bytes[6] = 0x40 | (bytes[6] >>> 4);
678
- bytes[8] = 0x80 | (bytes[8] >>> 2);
679
- return UUID.ofInner(bytes);
680
- }
681
- }
682
- /** A global flag to force use of cryptographically strong RNG. */
683
- // declare const UUIDV7_DENY_WEAK_RNG: boolean;
684
- /** Returns the default random number generator available in the environment. */
685
- const getDefaultRandom = () => {
686
- // fix: crypto isn't available in react-native, always use Math.random
687
- // // detect Web Crypto API
688
- // if (
689
- // typeof crypto !== "undefined" &&
690
- // typeof crypto.getRandomValues !== "undefined"
691
- // ) {
692
- // return new BufferedCryptoRandom();
693
- // } else {
694
- // // fall back on Math.random() unless the flag is set to true
695
- // if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
696
- // throw new Error("no cryptographically strong RNG available");
697
- // }
698
- // return {
699
- // nextUint32: (): number =>
700
- // Math.trunc(Math.random() * 0x1_0000) * 0x1_0000 +
701
- // Math.trunc(Math.random() * 0x1_0000),
702
- // };
703
- // }
704
- return {
705
- nextUint32: () => Math.trunc(Math.random() * 65536) * 65536 +
706
- Math.trunc(Math.random() * 65536),
707
- };
708
- };
709
- // /**
710
- // * Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
711
- // * buffer by default to avoid both unbearable throughput decline in some
712
- // * environments and the waste of time and space for unused values.
713
- // */
714
- // class BufferedCryptoRandom {
715
- // private readonly buffer = new Uint32Array(8);
716
- // private cursor = 0xffff;
717
- // nextUint32(): number {
718
- // if (this.cursor >= this.buffer.length) {
719
- // crypto.getRandomValues(this.buffer);
720
- // this.cursor = 0;
721
- // }
722
- // return this.buffer[this.cursor++];
723
- // }
724
- // }
725
- let defaultGenerator;
726
- /**
727
- * Generates a UUIDv7 string.
728
- *
729
- * @returns The 8-4-4-4-12 canonical hexadecimal string representation
730
- * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
731
- */
732
- const uuidv7 = () => uuidv7obj().toString();
733
- /** Generates a UUIDv7 object. */
734
- const uuidv7obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
735
-
736
- class PostHogFetchHttpError extends Error {
737
- constructor(response, reqByteLength) {
738
- super('HTTP error while fetching PostHog: status=' + response.status + ', reqByteLength=' + reqByteLength);
739
- this.response = response;
740
- this.reqByteLength = reqByteLength;
741
- this.name = 'PostHogFetchHttpError';
742
- }
743
- get status() {
744
- return this.response.status;
745
- }
746
- get text() {
747
- return this.response.text();
748
- }
749
- get json() {
750
- return this.response.json();
751
- }
752
- }
753
- class PostHogFetchNetworkError extends Error {
754
- constructor(error) {
755
- // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue.
756
- // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
757
- // @ts-ignore
758
- super('Network error while fetching PostHog', error instanceof Error ? { cause: error } : {});
759
- this.error = error;
760
- this.name = 'PostHogFetchNetworkError';
761
- }
762
- }
763
- const maybeAdd = (key, value) => value !== undefined ? { [key]: value } : {};
764
- async function logFlushError(err) {
765
- if (err instanceof PostHogFetchHttpError) {
766
- let text = '';
767
- try {
768
- text = await err.text;
769
- }
770
- catch { }
771
- console.error(`Error while flushing PostHog: message=${err.message}, response body=${text}`, err);
772
- }
773
- else {
774
- console.error('Error while flushing PostHog', err);
775
- }
776
- return Promise.resolve();
777
- }
778
- function isPostHogFetchError(err) {
779
- return typeof err === 'object' && (err instanceof PostHogFetchHttpError || err instanceof PostHogFetchNetworkError);
780
- }
781
- function isPostHogFetchContentTooLargeError(err) {
782
- return typeof err === 'object' && err instanceof PostHogFetchHttpError && err.status === 413;
783
- }
784
- var QuotaLimitedFeature;
785
- (function (QuotaLimitedFeature) {
786
- QuotaLimitedFeature["FeatureFlags"] = "feature_flags";
787
- QuotaLimitedFeature["Recordings"] = "recordings";
788
- })(QuotaLimitedFeature || (QuotaLimitedFeature = {}));
789
- class PostHogCoreStateless {
790
- constructor(apiKey, options) {
791
- this.flushPromise = null;
792
- this.shutdownPromise = null;
793
- this.pendingPromises = {};
794
- // internal
795
- this._events = new SimpleEventEmitter();
796
- this._isInitialized = false;
797
- assert(apiKey, "You must pass your PostHog project's api key.");
798
- this.apiKey = apiKey;
799
- this.host = removeTrailingSlash(options?.host || 'https://us.i.posthog.com');
800
- this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
801
- this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
802
- this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
803
- this.flushInterval = options?.flushInterval ?? 10000;
804
- this.preloadFeatureFlags = options?.preloadFeatureFlags ?? true;
805
- // If enable is explicitly set to false we override the optout
806
- this.defaultOptIn = options?.defaultOptIn ?? true;
807
- this.disableSurveys = options?.disableSurveys ?? false;
808
- this._retryOptions = {
809
- retryCount: options?.fetchRetryCount ?? 3,
810
- retryDelay: options?.fetchRetryDelay ?? 3000,
811
- retryCheck: isPostHogFetchError,
812
- };
813
- this.requestTimeout = options?.requestTimeout ?? 10000; // 10 seconds
814
- this.featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 3000; // 3 seconds
815
- this.remoteConfigRequestTimeoutMs = options?.remoteConfigRequestTimeoutMs ?? 3000; // 3 seconds
816
- this.disableGeoip = options?.disableGeoip ?? true;
817
- this.disabled = options?.disabled ?? false;
818
- this.historicalMigration = options?.historicalMigration ?? false;
819
- // Init promise allows the derived class to block calls until it is ready
820
- this._initPromise = Promise.resolve();
821
- this._isInitialized = true;
822
- this.disableCompression = !isGzipSupported() || (options?.disableCompression ?? false);
823
- }
824
- logMsgIfDebug(fn) {
825
- if (this.isDebug) {
826
- fn();
827
- }
828
- }
829
- wrap(fn) {
830
- if (this.disabled) {
831
- this.logMsgIfDebug(() => console.warn('[PostHog] The client is disabled'));
832
- return;
833
- }
834
- if (this._isInitialized) {
835
- // NOTE: We could also check for the "opt in" status here...
836
- return fn();
837
- }
838
- this._initPromise.then(() => fn());
839
- }
840
- getCommonEventProperties() {
841
- return {
842
- $lib: this.getLibraryId(),
843
- $lib_version: this.getLibraryVersion(),
844
- };
845
- }
846
- get optedOut() {
847
- return this.getPersistedProperty(PostHogPersistedProperty.OptedOut) ?? !this.defaultOptIn;
848
- }
849
- async optIn() {
850
- this.wrap(() => {
851
- this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
852
- });
853
- }
854
- async optOut() {
855
- this.wrap(() => {
856
- this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
857
- });
858
- }
859
- on(event, cb) {
860
- return this._events.on(event, cb);
861
- }
862
- debug(enabled = true) {
863
- this.removeDebugCallback?.();
864
- if (enabled) {
865
- const removeDebugCallback = this.on('*', (event, payload) => console.log('PostHog Debug', event, payload));
866
- this.removeDebugCallback = () => {
867
- removeDebugCallback();
868
- this.removeDebugCallback = undefined;
869
- };
870
- }
871
- }
872
- get isDebug() {
873
- return !!this.removeDebugCallback;
874
- }
875
- get isDisabled() {
876
- return this.disabled;
877
- }
878
- buildPayload(payload) {
879
- return {
880
- distinct_id: payload.distinct_id,
881
- event: payload.event,
882
- properties: {
883
- ...(payload.properties || {}),
884
- ...this.getCommonEventProperties(), // Common PH props
885
- },
886
- };
887
- }
888
- addPendingPromise(promise) {
889
- const promiseUUID = uuidv7();
890
- this.pendingPromises[promiseUUID] = promise;
891
- promise
892
- .catch(() => { })
893
- .finally(() => {
894
- delete this.pendingPromises[promiseUUID];
895
- });
896
- return promise;
897
- }
898
- /***
899
- *** TRACKING
900
- ***/
901
- identifyStateless(distinctId, properties, options) {
902
- this.wrap(() => {
903
- // The properties passed to identifyStateless are event properties.
904
- // To add person properties, pass in all person properties to the `$set` and `$set_once` keys.
905
- const payload = {
906
- ...this.buildPayload({
907
- distinct_id: distinctId,
908
- event: '$identify',
909
- properties,
910
- }),
911
- };
912
- this.enqueue('identify', payload, options);
913
- });
914
- }
915
- async identifyStatelessImmediate(distinctId, properties, options) {
916
- const payload = {
917
- ...this.buildPayload({
918
- distinct_id: distinctId,
919
- event: '$identify',
920
- properties,
921
- }),
922
- };
923
- await this.sendImmediate('identify', payload, options);
924
- }
925
- captureStateless(distinctId, event, properties, options) {
926
- this.wrap(() => {
927
- const payload = this.buildPayload({ distinct_id: distinctId, event, properties });
928
- this.enqueue('capture', payload, options);
929
- });
930
- }
931
- async captureStatelessImmediate(distinctId, event, properties, options) {
932
- const payload = this.buildPayload({ distinct_id: distinctId, event, properties });
933
- await this.sendImmediate('capture', payload, options);
934
- }
935
- aliasStateless(alias, distinctId, properties, options) {
936
- this.wrap(() => {
937
- const payload = this.buildPayload({
938
- event: '$create_alias',
939
- distinct_id: distinctId,
940
- properties: {
941
- ...(properties || {}),
942
- distinct_id: distinctId,
943
- alias,
944
- },
945
- });
946
- this.enqueue('alias', payload, options);
947
- });
948
- }
949
- async aliasStatelessImmediate(alias, distinctId, properties, options) {
950
- const payload = this.buildPayload({
951
- event: '$create_alias',
952
- distinct_id: distinctId,
953
- properties: {
954
- ...(properties || {}),
955
- distinct_id: distinctId,
956
- alias,
957
- },
958
- });
959
- await this.sendImmediate('alias', payload, options);
960
- }
961
- /***
962
- *** GROUPS
963
- ***/
964
- groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties) {
965
- this.wrap(() => {
966
- const payload = this.buildPayload({
967
- distinct_id: distinctId || `$${groupType}_${groupKey}`,
968
- event: '$groupidentify',
969
- properties: {
970
- $group_type: groupType,
971
- $group_key: groupKey,
972
- $group_set: groupProperties || {},
973
- ...(eventProperties || {}),
974
- },
975
- });
976
- this.enqueue('capture', payload, options);
977
- });
978
- }
979
- async getRemoteConfig() {
980
- await this._initPromise;
981
- let host = this.host;
982
- if (host === 'https://us.i.posthog.com') {
983
- host = 'https://us-assets.i.posthog.com';
984
- }
985
- else if (host === 'https://eu.i.posthog.com') {
986
- host = 'https://eu-assets.i.posthog.com';
987
- }
988
- const url = `${host}/array/${this.apiKey}/config`;
989
- const fetchOptions = {
990
- method: 'GET',
991
- headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
992
- };
993
- // Don't retry remote config API calls
994
- return this.fetchWithRetry(url, fetchOptions, { retryCount: 0 }, this.remoteConfigRequestTimeoutMs)
995
- .then((response) => response.json())
996
- .catch((error) => {
997
- this.logMsgIfDebug(() => console.error('Remote config could not be loaded', error));
998
- this._events.emit('error', error);
999
- return undefined;
1000
- });
1001
- }
1002
- /***
1003
- *** FEATURE FLAGS
1004
- ***/
1005
- async getFlags(distinctId, groups = {}, personProperties = {}, groupProperties = {}, extraPayload = {}) {
1006
- await this._initPromise;
1007
- const url = `${this.host}/flags/?v=2&config=true`;
1008
- const fetchOptions = {
1009
- method: 'POST',
1010
- headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1011
- body: JSON.stringify({
1012
- token: this.apiKey,
1013
- distinct_id: distinctId,
1014
- groups,
1015
- person_properties: personProperties,
1016
- group_properties: groupProperties,
1017
- ...extraPayload,
1018
- }),
1019
- };
1020
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Flags URL', url));
1021
- // Don't retry /flags API calls
1022
- return this.fetchWithRetry(url, fetchOptions, { retryCount: 0 }, this.featureFlagsRequestTimeoutMs)
1023
- .then((response) => response.json())
1024
- .then((response) => normalizeFlagsResponse(response))
1025
- .catch((error) => {
1026
- this._events.emit('error', error);
1027
- return undefined;
1028
- });
1029
- }
1030
- async getFeatureFlagStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1031
- await this._initPromise;
1032
- const flagDetailResponse = await this.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
1033
- if (flagDetailResponse === undefined) {
1034
- // If we haven't loaded flags yet, or errored out, we respond with undefined
1035
- return {
1036
- response: undefined,
1037
- requestId: undefined,
1038
- };
1039
- }
1040
- let response = getFeatureFlagValue(flagDetailResponse.response);
1041
- if (response === undefined) {
1042
- // For cases where the flag is unknown, return false
1043
- response = false;
1044
- }
1045
- // If we have flags we either return the value (true or string) or false
1046
- return {
1047
- response,
1048
- requestId: flagDetailResponse.requestId,
1049
- };
1050
- }
1051
- async getFeatureFlagDetailStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1052
- await this._initPromise;
1053
- const flagsResponse = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [key]);
1054
- if (flagsResponse === undefined) {
1055
- return undefined;
1056
- }
1057
- const featureFlags = flagsResponse.flags;
1058
- const flagDetail = featureFlags[key];
1059
- return {
1060
- response: flagDetail,
1061
- requestId: flagsResponse.requestId,
1062
- };
1063
- }
1064
- async getFeatureFlagPayloadStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1065
- await this._initPromise;
1066
- const payloads = await this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [key]);
1067
- if (!payloads) {
1068
- return undefined;
1069
- }
1070
- const response = payloads[key];
1071
- // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1072
- if (response === undefined) {
1073
- return null;
1074
- }
1075
- return response;
1076
- }
1077
- async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
1078
- await this._initPromise;
1079
- const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate)).payloads;
1080
- return payloads;
1081
- }
1082
- async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
1083
- await this._initPromise;
1084
- return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
1085
- }
1086
- async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
1087
- await this._initPromise;
1088
- const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
1089
- if (!featureFlagDetails) {
1090
- return {
1091
- flags: undefined,
1092
- payloads: undefined,
1093
- requestId: undefined,
1094
- };
1095
- }
1096
- return {
1097
- flags: featureFlagDetails.featureFlags,
1098
- payloads: featureFlagDetails.featureFlagPayloads,
1099
- requestId: featureFlagDetails.requestId,
1100
- };
1101
- }
1102
- async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
1103
- await this._initPromise;
1104
- const extraPayload = {};
1105
- if (disableGeoip ?? this.disableGeoip) {
1106
- extraPayload['geoip_disable'] = true;
1107
- }
1108
- if (flagKeysToEvaluate) {
1109
- extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
1110
- }
1111
- const flagsResponse = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
1112
- if (flagsResponse === undefined) {
1113
- // We probably errored out, so return undefined
1114
- return undefined;
1115
- }
1116
- // if there's an error on the flagsResponse, log a console error, but don't throw an error
1117
- if (flagsResponse.errorsWhileComputingFlags) {
1118
- console.error('[FEATURE FLAGS] Error while computing feature flags, some flags may be missing or incorrect. Learn more at https://posthog.com/docs/feature-flags/best-practices');
1119
- }
1120
- // Add check for quota limitation on feature flags
1121
- if (flagsResponse.quotaLimited?.includes(QuotaLimitedFeature.FeatureFlags)) {
1122
- console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - feature flags unavailable. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
1123
- return {
1124
- flags: {},
1125
- featureFlags: {},
1126
- featureFlagPayloads: {},
1127
- requestId: flagsResponse?.requestId,
1128
- };
1129
- }
1130
- return flagsResponse;
1131
- }
1132
- /***
1133
- *** SURVEYS
1134
- ***/
1135
- async getSurveysStateless() {
1136
- await this._initPromise;
1137
- if (this.disableSurveys === true) {
1138
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Loading surveys is disabled.'));
1139
- return [];
1140
- }
1141
- const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
1142
- const fetchOptions = {
1143
- method: 'GET',
1144
- headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1145
- };
1146
- const response = await this.fetchWithRetry(url, fetchOptions)
1147
- .then((response) => {
1148
- if (response.status !== 200 || !response.json) {
1149
- const msg = `Surveys API could not be loaded: ${response.status}`;
1150
- const error = new Error(msg);
1151
- this.logMsgIfDebug(() => console.error(error));
1152
- this._events.emit('error', new Error(msg));
1153
- return undefined;
1154
- }
1155
- return response.json();
1156
- })
1157
- .catch((error) => {
1158
- this.logMsgIfDebug(() => console.error('Surveys API could not be loaded', error));
1159
- this._events.emit('error', error);
1160
- return undefined;
1161
- });
1162
- const newSurveys = response?.surveys;
1163
- if (newSurveys) {
1164
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Surveys fetched from API: ', JSON.stringify(newSurveys)));
1165
- }
1166
- return newSurveys ?? [];
1167
- }
1168
- get props() {
1169
- if (!this._props) {
1170
- this._props = this.getPersistedProperty(PostHogPersistedProperty.Props);
1171
- }
1172
- return this._props || {};
1173
- }
1174
- set props(val) {
1175
- this._props = val;
1176
- }
1177
- async register(properties) {
1178
- this.wrap(() => {
1179
- this.props = {
1180
- ...this.props,
1181
- ...properties,
1182
- };
1183
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1184
- });
1185
- }
1186
- async unregister(property) {
1187
- this.wrap(() => {
1188
- delete this.props[property];
1189
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1190
- });
1191
- }
1192
- /***
1193
- *** QUEUEING AND FLUSHING
1194
- ***/
1195
- enqueue(type, _message, options) {
1196
- this.wrap(() => {
1197
- if (this.optedOut) {
1198
- this._events.emit(type, `Library is disabled. Not sending event. To re-enable, call posthog.optIn()`);
1199
- return;
1200
- }
1201
- const message = this.prepareMessage(type, _message, options);
1202
- const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1203
- if (queue.length >= this.maxQueueSize) {
1204
- queue.shift();
1205
- this.logMsgIfDebug(() => console.info('Queue is full, the oldest event is dropped.'));
1206
- }
1207
- queue.push({ message });
1208
- this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1209
- this._events.emit(type, message);
1210
- // Flush queued events if we meet the flushAt length
1211
- if (queue.length >= this.flushAt) {
1212
- this.flushBackground();
1213
- }
1214
- if (this.flushInterval && !this._flushTimer) {
1215
- this._flushTimer = safeSetTimeout(() => this.flushBackground(), this.flushInterval);
1216
- }
1217
- });
1218
- }
1219
- async sendImmediate(type, _message, options) {
1220
- if (this.disabled) {
1221
- this.logMsgIfDebug(() => console.warn('[PostHog] The client is disabled'));
1222
- return;
1223
- }
1224
- if (!this._isInitialized) {
1225
- await this._initPromise;
1226
- }
1227
- if (this.optedOut) {
1228
- this._events.emit(type, `Library is disabled. Not sending event. To re-enable, call posthog.optIn()`);
1229
- return;
1230
- }
1231
- const data = {
1232
- api_key: this.apiKey,
1233
- batch: [this.prepareMessage(type, _message, options)],
1234
- sent_at: currentISOTime(),
1235
- };
1236
- if (this.historicalMigration) {
1237
- data.historical_migration = true;
1238
- }
1239
- const payload = JSON.stringify(data);
1240
- const url = `${this.host}/batch/`;
1241
- const gzippedPayload = !this.disableCompression ? await gzipCompress(payload, this.isDebug) : null;
1242
- const fetchOptions = {
1243
- method: 'POST',
1244
- headers: {
1245
- ...this.getCustomHeaders(),
1246
- 'Content-Type': 'application/json',
1247
- ...(gzippedPayload !== null && { 'Content-Encoding': 'gzip' }),
1248
- },
1249
- body: gzippedPayload || payload,
1250
- };
1251
- try {
1252
- await this.fetchWithRetry(url, fetchOptions);
1253
- }
1254
- catch (err) {
1255
- this._events.emit('error', err);
1256
- }
1257
- }
1258
- prepareMessage(type, _message, options) {
1259
- const message = {
1260
- ..._message,
1261
- type: type,
1262
- library: this.getLibraryId(),
1263
- library_version: this.getLibraryVersion(),
1264
- timestamp: options?.timestamp ? options?.timestamp : currentISOTime(),
1265
- uuid: options?.uuid ? options.uuid : uuidv7(),
1266
- };
1267
- const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip;
1268
- if (addGeoipDisableProperty) {
1269
- if (!message.properties) {
1270
- message.properties = {};
1271
- }
1272
- message['properties']['$geoip_disable'] = true;
1273
- }
1274
- if (message.distinctId) {
1275
- message.distinct_id = message.distinctId;
1276
- delete message.distinctId;
1277
- }
1278
- return message;
1279
- }
1280
- clearFlushTimer() {
1281
- if (this._flushTimer) {
1282
- clearTimeout(this._flushTimer);
1283
- this._flushTimer = undefined;
1284
- }
1285
- }
1286
- /**
1287
- * Helper for flushing the queue in the background
1288
- * Avoids unnecessary promise errors
1289
- */
1290
- flushBackground() {
1291
- void this.flush().catch(async (err) => {
1292
- await logFlushError(err);
1293
- });
1294
- }
1295
- /**
1296
- * Flushes the queue
1297
- *
1298
- * This function will return a promise that will resolve when the flush is complete,
1299
- * or reject if there was an error (for example if the server or network is down).
1300
- *
1301
- * If there is already a flush in progress, this function will wait for that flush to complete.
1302
- *
1303
- * It's recommended to do error handling in the callback of the promise.
1304
- *
1305
- * @example
1306
- * posthog.flush().then(() => {
1307
- * console.log('Flush complete')
1308
- * }).catch((err) => {
1309
- * console.error('Flush failed', err)
1310
- * })
1311
- *
1312
- *
1313
- * @throws PostHogFetchHttpError
1314
- * @throws PostHogFetchNetworkError
1315
- * @throws Error
1316
- */
1317
- async flush() {
1318
- // Wait for the current flush operation to finish (regardless of success or failure), then try to flush again.
1319
- // Use allSettled instead of finally to be defensive around flush throwing errors immediately rather than rejecting.
1320
- // Use a custom allSettled implementation to avoid issues with patching Promise on RN
1321
- const nextFlushPromise = allSettled([this.flushPromise]).then(() => {
1322
- return this._flush();
1323
- });
1324
- this.flushPromise = nextFlushPromise;
1325
- void this.addPendingPromise(nextFlushPromise);
1326
- allSettled([nextFlushPromise]).then(() => {
1327
- // If there are no others waiting to flush, clear the promise.
1328
- // We don't strictly need to do this, but it could make debugging easier
1329
- if (this.flushPromise === nextFlushPromise) {
1330
- this.flushPromise = null;
1331
- }
1332
- });
1333
- return nextFlushPromise;
1334
- }
1335
- getCustomHeaders() {
1336
- // Don't set the user agent if we're not on a browser. The latest spec allows
1337
- // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1338
- // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1339
- // but browsers such as Chrome and Safari have not caught up.
1340
- const customUserAgent = this.getCustomUserAgent();
1341
- const headers = {};
1342
- if (customUserAgent && customUserAgent !== '') {
1343
- headers['User-Agent'] = customUserAgent;
1344
- }
1345
- return headers;
1346
- }
1347
- async _flush() {
1348
- this.clearFlushTimer();
1349
- await this._initPromise;
1350
- let queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1351
- if (!queue.length) {
1352
- return;
1353
- }
1354
- const sentMessages = [];
1355
- const originalQueueLength = queue.length;
1356
- while (queue.length > 0 && sentMessages.length < originalQueueLength) {
1357
- const batchItems = queue.slice(0, this.maxBatchSize);
1358
- const batchMessages = batchItems.map((item) => item.message);
1359
- const persistQueueChange = () => {
1360
- const refreshedQueue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1361
- const newQueue = refreshedQueue.slice(batchItems.length);
1362
- this.setPersistedProperty(PostHogPersistedProperty.Queue, newQueue);
1363
- queue = newQueue;
1364
- };
1365
- const data = {
1366
- api_key: this.apiKey,
1367
- batch: batchMessages,
1368
- sent_at: currentISOTime(),
1369
- };
1370
- if (this.historicalMigration) {
1371
- data.historical_migration = true;
1372
- }
1373
- const payload = JSON.stringify(data);
1374
- const url = `${this.host}/batch/`;
1375
- const gzippedPayload = !this.disableCompression ? await gzipCompress(payload, this.isDebug) : null;
1376
- const fetchOptions = {
1377
- method: 'POST',
1378
- headers: {
1379
- ...this.getCustomHeaders(),
1380
- 'Content-Type': 'application/json',
1381
- ...(gzippedPayload !== null && { 'Content-Encoding': 'gzip' }),
1382
- },
1383
- body: gzippedPayload || payload,
1384
- };
1385
- const retryOptions = {
1386
- retryCheck: (err) => {
1387
- // don't automatically retry on 413 errors, we want to reduce the batch size first
1388
- if (isPostHogFetchContentTooLargeError(err)) {
1389
- return false;
1390
- }
1391
- // otherwise, retry on network errors
1392
- return isPostHogFetchError(err);
1393
- },
1394
- };
1395
- try {
1396
- await this.fetchWithRetry(url, fetchOptions, retryOptions);
1397
- }
1398
- catch (err) {
1399
- if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
1400
- // if we get a 413 error, we want to reduce the batch size and try again
1401
- this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2));
1402
- this.logMsgIfDebug(() => console.warn(`Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`));
1403
- // do not persist the queue change, we want to retry the same batch
1404
- continue;
1405
- }
1406
- // depending on the error type, eg a malformed JSON or broken queue, it'll always return an error
1407
- // and this will be an endless loop, in this case, if the error isn't a network issue, we always remove the items from the queue
1408
- if (!(err instanceof PostHogFetchNetworkError)) {
1409
- persistQueueChange();
1410
- }
1411
- this._events.emit('error', err);
1412
- throw err;
1413
- }
1414
- persistQueueChange();
1415
- sentMessages.push(...batchMessages);
1416
- }
1417
- this._events.emit('flush', sentMessages);
1418
- }
1419
- async fetchWithRetry(url, options, retryOptions, requestTimeout) {
1420
- var _a;
1421
- (_a = AbortSignal).timeout ?? (_a.timeout = function timeout(ms) {
1422
- const ctrl = new AbortController();
1423
- setTimeout(() => ctrl.abort(), ms);
1424
- return ctrl.signal;
1425
- });
1426
- const body = options.body ? options.body : '';
1427
- let reqByteLength = -1;
1428
- try {
1429
- if (body instanceof Blob) {
1430
- reqByteLength = body.size;
1431
- }
1432
- else {
1433
- reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
1434
- }
1435
- }
1436
- catch {
1437
- if (body instanceof Blob) {
1438
- reqByteLength = body.size;
1439
- }
1440
- else {
1441
- const encoded = new TextEncoder().encode(body);
1442
- reqByteLength = encoded.length;
1443
- }
1444
- }
1445
- return await retriable(async () => {
1446
- let res = null;
1447
- try {
1448
- res = await this.fetch(url, {
1449
- signal: AbortSignal.timeout(requestTimeout ?? this.requestTimeout),
1450
- ...options,
1451
- });
1452
- }
1453
- catch (e) {
1454
- // fetch will only throw on network errors or on timeouts
1455
- throw new PostHogFetchNetworkError(e);
1456
- }
1457
- // If we're in no-cors mode, we can't access the response status
1458
- // We only throw on HTTP errors if we're not in no-cors mode
1459
- // https://developer.mozilla.org/en-US/docs/Web/API/Request/mode#no-cors
1460
- const isNoCors = options.mode === 'no-cors';
1461
- if (!isNoCors && (res.status < 200 || res.status >= 400)) {
1462
- throw new PostHogFetchHttpError(res, reqByteLength);
1463
- }
1464
- return res;
1465
- }, { ...this._retryOptions, ...retryOptions });
1466
- }
1467
- async _shutdown(shutdownTimeoutMs = 30000) {
1468
- // A little tricky - we want to have a max shutdown time and enforce it, even if that means we have some
1469
- // dangling promises. We'll keep track of the timeout and resolve/reject based on that.
1470
- await this._initPromise;
1471
- let hasTimedOut = false;
1472
- this.clearFlushTimer();
1473
- const doShutdown = async () => {
1474
- try {
1475
- await Promise.all(Object.values(this.pendingPromises));
1476
- while (true) {
1477
- const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1478
- if (queue.length === 0) {
1479
- break;
1480
- }
1481
- // flush again to make sure we send all events, some of which might've been added
1482
- // while we were waiting for the pending promises to resolve
1483
- // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1484
- await this.flush();
1485
- if (hasTimedOut) {
1486
- break;
1487
- }
1488
- }
1489
- }
1490
- catch (e) {
1491
- if (!isPostHogFetchError(e)) {
1492
- throw e;
1493
- }
1494
- await logFlushError(e);
1495
- }
1496
- };
1497
- return Promise.race([
1498
- new Promise((_, reject) => {
1499
- safeSetTimeout(() => {
1500
- this.logMsgIfDebug(() => console.error('Timed out while shutting down PostHog'));
1501
- hasTimedOut = true;
1502
- reject('Timeout while shutting down PostHog. Some events may not have been sent.');
1503
- }, shutdownTimeoutMs);
1504
- }),
1505
- doShutdown(),
1506
- ]);
1507
- }
1508
- /**
1509
- * Call shutdown() once before the node process exits, so ensure that all events have been sent and all promises
1510
- * have resolved. Do not use this function if you intend to keep using this PostHog instance after calling it.
1511
- * @param shutdownTimeoutMs
1512
- */
1513
- async shutdown(shutdownTimeoutMs = 30000) {
1514
- if (this.shutdownPromise) {
1515
- this.logMsgIfDebug(() => console.warn('shutdown() called while already shutting down. shutdown() is meant to be called once before process exit - use flush() for per-request cleanup'));
1516
- }
1517
- else {
1518
- this.shutdownPromise = this._shutdown(shutdownTimeoutMs).finally(() => {
1519
- this.shutdownPromise = null;
1520
- });
1521
- }
1522
- return this.shutdownPromise;
1523
- }
1524
- }
1525
- class PostHogCore extends PostHogCoreStateless {
1526
- constructor(apiKey, options) {
1527
- // Default for stateful mode is to not disable geoip. Only override if explicitly set
1528
- const disableGeoipOption = options?.disableGeoip ?? false;
1529
- // Default for stateful mode is to timeout at 10s. Only override if explicitly set
1530
- const featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 10000; // 10 seconds
1531
- super(apiKey, { ...options, disableGeoip: disableGeoipOption, featureFlagsRequestTimeoutMs });
1532
- this.flagCallReported = {};
1533
- this._sessionMaxLengthSeconds = 24 * 60 * 60; // 24 hours
1534
- this.sessionProps = {};
1535
- this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
1536
- this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800; // 30 minutes
1537
- }
1538
- setupBootstrap(options) {
1539
- const bootstrap = options?.bootstrap;
1540
- if (!bootstrap) {
1541
- return;
1542
- }
1543
- // bootstrap options are only set if no persisted values are found
1544
- // this is to prevent overwriting existing values
1545
- if (bootstrap.distinctId) {
1546
- if (bootstrap.isIdentifiedId) {
1547
- const distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
1548
- if (!distinctId) {
1549
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, bootstrap.distinctId);
1550
- }
1551
- }
1552
- else {
1553
- const anonymousId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
1554
- if (!anonymousId) {
1555
- this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, bootstrap.distinctId);
1556
- }
1557
- }
1558
- }
1559
- const bootstrapFeatureFlags = bootstrap.featureFlags;
1560
- const bootstrapFeatureFlagPayloads = bootstrap.featureFlagPayloads ?? {};
1561
- if (bootstrapFeatureFlags && Object.keys(bootstrapFeatureFlags).length) {
1562
- const normalizedBootstrapFeatureFlagDetails = createFlagsResponseFromFlagsAndPayloads(bootstrapFeatureFlags, bootstrapFeatureFlagPayloads);
1563
- if (Object.keys(normalizedBootstrapFeatureFlagDetails.flags).length > 0) {
1564
- this.setBootstrappedFeatureFlagDetails(normalizedBootstrapFeatureFlagDetails);
1565
- const currentFeatureFlagDetails = this.getKnownFeatureFlagDetails() || { flags: {}, requestId: undefined };
1566
- const newFeatureFlagDetails = {
1567
- flags: {
1568
- ...normalizedBootstrapFeatureFlagDetails.flags,
1569
- ...currentFeatureFlagDetails.flags,
1570
- },
1571
- requestId: normalizedBootstrapFeatureFlagDetails.requestId,
1572
- };
1573
- this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
1574
- }
1575
- }
1576
- }
1577
- clearProps() {
1578
- this.props = undefined;
1579
- this.sessionProps = {};
1580
- this.flagCallReported = {};
1581
- }
1582
- on(event, cb) {
1583
- return this._events.on(event, cb);
1584
- }
1585
- reset(propertiesToKeep) {
1586
- this.wrap(() => {
1587
- const allPropertiesToKeep = [PostHogPersistedProperty.Queue, ...(propertiesToKeep || [])];
1588
- // clean up props
1589
- this.clearProps();
1590
- for (const key of Object.keys(PostHogPersistedProperty)) {
1591
- if (!allPropertiesToKeep.includes(PostHogPersistedProperty[key])) {
1592
- this.setPersistedProperty(PostHogPersistedProperty[key], null);
1593
- }
1594
- }
1595
- this.reloadFeatureFlags();
1596
- });
1597
- }
1598
- getCommonEventProperties() {
1599
- const featureFlags = this.getFeatureFlags();
1600
- const featureVariantProperties = {};
1601
- if (featureFlags) {
1602
- for (const [feature, variant] of Object.entries(featureFlags)) {
1603
- featureVariantProperties[`$feature/${feature}`] = variant;
1604
- }
1605
- }
1606
- return {
1607
- ...maybeAdd('$active_feature_flags', featureFlags ? Object.keys(featureFlags) : undefined),
1608
- ...featureVariantProperties,
1609
- ...super.getCommonEventProperties(),
1610
- };
1611
- }
1612
- enrichProperties(properties) {
1613
- return {
1614
- ...this.props,
1615
- ...this.sessionProps,
1616
- ...(properties || {}),
1617
- ...this.getCommonEventProperties(),
1618
- $session_id: this.getSessionId(),
1619
- };
1620
- }
1621
- /**
1622
- * * @returns {string} The stored session ID for the current session. This may be an empty string if the client is not yet fully initialized.
1623
- */
1624
- getSessionId() {
1625
- if (!this._isInitialized) {
1626
- return '';
1627
- }
1628
- let sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
1629
- const sessionLastTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp) || 0;
1630
- const sessionStartTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp) || 0;
1631
- const now = Date.now();
1632
- const sessionLastDif = now - sessionLastTimestamp;
1633
- const sessionStartDif = now - sessionStartTimestamp;
1634
- if (!sessionId ||
1635
- sessionLastDif > this._sessionExpirationTimeSeconds * 1000 ||
1636
- sessionStartDif > this._sessionMaxLengthSeconds * 1000) {
1637
- sessionId = uuidv7();
1638
- this.setPersistedProperty(PostHogPersistedProperty.SessionId, sessionId);
1639
- this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, now);
1640
- }
1641
- this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, now);
1642
- return sessionId;
1643
- }
1644
- resetSessionId() {
1645
- this.wrap(() => {
1646
- this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
1647
- this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, null);
1648
- this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, null);
1649
- });
1650
- }
1651
- /**
1652
- * * @returns {string} The stored anonymous ID. This may be an empty string if the client is not yet fully initialized.
1653
- */
1654
- getAnonymousId() {
1655
- if (!this._isInitialized) {
1656
- return '';
1657
- }
1658
- let anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
1659
- if (!anonId) {
1660
- anonId = uuidv7();
1661
- this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
1662
- }
1663
- return anonId;
1664
- }
1665
- /**
1666
- * * @returns {string} The stored distinct ID. This may be an empty string if the client is not yet fully initialized.
1667
- */
1668
- getDistinctId() {
1669
- if (!this._isInitialized) {
1670
- return '';
1671
- }
1672
- return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1673
- }
1674
- registerForSession(properties) {
1675
- this.sessionProps = {
1676
- ...this.sessionProps,
1677
- ...properties,
1678
- };
1679
- }
1680
- unregisterForSession(property) {
1681
- delete this.sessionProps[property];
1682
- }
1683
- /***
1684
- *** TRACKING
1685
- ***/
1686
- identify(distinctId, properties, options) {
1687
- this.wrap(() => {
1688
- const previousDistinctId = this.getDistinctId();
1689
- distinctId = distinctId || previousDistinctId;
1690
- if (properties?.$groups) {
1691
- this.groups(properties.$groups);
1692
- }
1693
- // promote $set and $set_once to top level
1694
- const userPropsOnce = properties?.$set_once;
1695
- delete properties?.$set_once;
1696
- // if no $set is provided we assume all properties are $set
1697
- const userProps = properties?.$set || properties;
1698
- const allProperties = this.enrichProperties({
1699
- $anon_distinct_id: this.getAnonymousId(),
1700
- ...maybeAdd('$set', userProps),
1701
- ...maybeAdd('$set_once', userPropsOnce),
1702
- });
1703
- if (distinctId !== previousDistinctId) {
1704
- // We keep the AnonymousId to be used by flags calls and identify to link the previousId
1705
- this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
1706
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1707
- this.reloadFeatureFlags();
1708
- }
1709
- super.identifyStateless(distinctId, allProperties, options);
1710
- });
1711
- }
1712
- capture(event, properties, options) {
1713
- this.wrap(() => {
1714
- const distinctId = this.getDistinctId();
1715
- if (properties?.$groups) {
1716
- this.groups(properties.$groups);
1717
- }
1718
- const allProperties = this.enrichProperties(properties);
1719
- super.captureStateless(distinctId, event, allProperties, options);
1720
- });
1721
- }
1722
- alias(alias) {
1723
- this.wrap(() => {
1724
- const distinctId = this.getDistinctId();
1725
- const allProperties = this.enrichProperties({});
1726
- super.aliasStateless(alias, distinctId, allProperties);
1727
- });
1728
- }
1729
- autocapture(eventType, elements, properties = {}, options) {
1730
- this.wrap(() => {
1731
- const distinctId = this.getDistinctId();
1732
- const payload = {
1733
- distinct_id: distinctId,
1734
- event: '$autocapture',
1735
- properties: {
1736
- ...this.enrichProperties(properties),
1737
- $event_type: eventType,
1738
- $elements: elements,
1739
- },
1740
- };
1741
- this.enqueue('autocapture', payload, options);
1742
- });
1743
- }
1744
- /***
1745
- *** GROUPS
1746
- ***/
1747
- groups(groups) {
1748
- this.wrap(() => {
1749
- // Get persisted groups
1750
- const existingGroups = this.props.$groups || {};
1751
- this.register({
1752
- $groups: {
1753
- ...existingGroups,
1754
- ...groups,
1755
- },
1756
- });
1757
- if (Object.keys(groups).find((type) => existingGroups[type] !== groups[type])) {
1758
- this.reloadFeatureFlags();
1759
- }
1760
- });
1761
- }
1762
- group(groupType, groupKey, groupProperties, options) {
1763
- this.wrap(() => {
1764
- this.groups({
1765
- [groupType]: groupKey,
1766
- });
1767
- if (groupProperties) {
1768
- this.groupIdentify(groupType, groupKey, groupProperties, options);
1769
- }
1770
- });
1771
- }
1772
- groupIdentify(groupType, groupKey, groupProperties, options) {
1773
- this.wrap(() => {
1774
- const distinctId = this.getDistinctId();
1775
- const eventProperties = this.enrichProperties({});
1776
- super.groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties);
1777
- });
1778
- }
1779
- /***
1780
- * PROPERTIES
1781
- ***/
1782
- setPersonPropertiesForFlags(properties) {
1783
- this.wrap(() => {
1784
- // Get persisted person properties
1785
- const existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1786
- this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {
1787
- ...existingProperties,
1788
- ...properties,
1789
- });
1790
- });
1791
- }
1792
- resetPersonPropertiesForFlags() {
1793
- this.wrap(() => {
1794
- this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, null);
1795
- });
1796
- }
1797
- setGroupPropertiesForFlags(properties) {
1798
- this.wrap(() => {
1799
- // Get persisted group properties
1800
- const existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) ||
1801
- {};
1802
- if (Object.keys(existingProperties).length !== 0) {
1803
- Object.keys(existingProperties).forEach((groupType) => {
1804
- existingProperties[groupType] = {
1805
- ...existingProperties[groupType],
1806
- ...properties[groupType],
1807
- };
1808
- delete properties[groupType];
1809
- });
1810
- }
1811
- this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {
1812
- ...existingProperties,
1813
- ...properties,
1814
- });
1815
- });
1816
- }
1817
- resetGroupPropertiesForFlags() {
1818
- this.wrap(() => {
1819
- this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, null);
1820
- });
1821
- }
1822
- async remoteConfigAsync() {
1823
- await this._initPromise;
1824
- if (this._remoteConfigResponsePromise) {
1825
- return this._remoteConfigResponsePromise;
1826
- }
1827
- return this._remoteConfigAsync();
1828
- }
1829
- /***
1830
- *** FEATURE FLAGS
1831
- ***/
1832
- async flagsAsync(sendAnonDistinctId = true) {
1833
- await this._initPromise;
1834
- if (this._flagsResponsePromise) {
1835
- return this._flagsResponsePromise;
1836
- }
1837
- return this._flagsAsync(sendAnonDistinctId);
1838
- }
1839
- cacheSessionReplay(source, response) {
1840
- const sessionReplay = response?.sessionRecording;
1841
- if (sessionReplay) {
1842
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
1843
- this.logMsgIfDebug(() => console.log('PostHog Debug', `Session replay config from ${source}: `, JSON.stringify(sessionReplay)));
1844
- }
1845
- else if (typeof sessionReplay === 'boolean' && sessionReplay === false) {
1846
- // if session replay is disabled, we don't need to cache it
1847
- // we need to check for this because the response might be undefined (/flags does not return sessionRecording yet)
1848
- this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay config from ${source} disabled.`));
1849
- this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
1850
- }
1851
- }
1852
- async _remoteConfigAsync() {
1853
- this._remoteConfigResponsePromise = this._initPromise
1854
- .then(() => {
1855
- let remoteConfig = this.getPersistedProperty(PostHogPersistedProperty.RemoteConfig);
1856
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Cached remote config: ', JSON.stringify(remoteConfig)));
1857
- return super.getRemoteConfig().then((response) => {
1858
- if (response) {
1859
- const remoteConfigWithoutSurveys = { ...response };
1860
- delete remoteConfigWithoutSurveys.surveys;
1861
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Fetched remote config: ', JSON.stringify(remoteConfigWithoutSurveys)));
1862
- if (this.disableSurveys === false) {
1863
- const surveys = response.surveys;
1864
- let hasSurveys = true;
1865
- if (!Array.isArray(surveys)) {
1866
- // If surveys is not an array, it means there are no surveys (its a boolean instead)
1867
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'There are no surveys.'));
1868
- hasSurveys = false;
1869
- }
1870
- else {
1871
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Surveys fetched from remote config: ', JSON.stringify(surveys)));
1872
- }
1873
- if (hasSurveys) {
1874
- this.setPersistedProperty(PostHogPersistedProperty.Surveys, surveys);
1875
- }
1876
- else {
1877
- this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
1878
- }
1879
- }
1880
- else {
1881
- this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
1882
- }
1883
- // we cache the surveys in its own storage key
1884
- this.setPersistedProperty(PostHogPersistedProperty.RemoteConfig, remoteConfigWithoutSurveys);
1885
- this.cacheSessionReplay('remote config', response);
1886
- // we only dont load flags if the remote config has no feature flags
1887
- if (response.hasFeatureFlags === false) {
1888
- // resetting flags to empty object
1889
- this.setKnownFeatureFlagDetails({ flags: {} });
1890
- this.logMsgIfDebug(() => console.warn('Remote config has no feature flags, will not load feature flags.'));
1891
- }
1892
- else if (this.preloadFeatureFlags !== false) {
1893
- this.reloadFeatureFlags();
1894
- }
1895
- if (!response.supportedCompression?.includes(Compression.GZipJS)) {
1896
- this.disableCompression = true;
1897
- }
1898
- remoteConfig = response;
1899
- }
1900
- return remoteConfig;
1901
- });
1902
- })
1903
- .finally(() => {
1904
- this._remoteConfigResponsePromise = undefined;
1905
- });
1906
- return this._remoteConfigResponsePromise;
1907
- }
1908
- async _flagsAsync(sendAnonDistinctId = true) {
1909
- this._flagsResponsePromise = this._initPromise
1910
- .then(async () => {
1911
- const distinctId = this.getDistinctId();
1912
- const groups = this.props.$groups || {};
1913
- const personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1914
- const groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) ||
1915
- {};
1916
- const extraProperties = {
1917
- $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : undefined,
1918
- };
1919
- const res = await super.getFlags(distinctId, groups, personProperties, groupProperties, extraProperties);
1920
- // Add check for quota limitation on feature flags
1921
- if (res?.quotaLimited?.includes(QuotaLimitedFeature.FeatureFlags)) {
1922
- // Unset all feature flags by setting to null
1923
- this.setKnownFeatureFlagDetails(null);
1924
- console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
1925
- return res;
1926
- }
1927
- if (res?.featureFlags) {
1928
- // clear flag call reported if we have new flags since they might have changed
1929
- if (this.sendFeatureFlagEvent) {
1930
- this.flagCallReported = {};
1931
- }
1932
- let newFeatureFlagDetails = res;
1933
- if (res.errorsWhileComputingFlags) {
1934
- // if not all flags were computed, we upsert flags instead of replacing them
1935
- const currentFlagDetails = this.getKnownFeatureFlagDetails();
1936
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlagDetails)));
1937
- newFeatureFlagDetails = {
1938
- ...res,
1939
- flags: { ...currentFlagDetails?.flags, ...res.flags },
1940
- };
1941
- }
1942
- this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
1943
- // Mark that we hit the /flags endpoint so we can capture this in the $feature_flag_called event
1944
- this.setPersistedProperty(PostHogPersistedProperty.FlagsEndpointWasHit, true);
1945
- this.cacheSessionReplay('flags', res);
1946
- }
1947
- return res;
1948
- })
1949
- .finally(() => {
1950
- this._flagsResponsePromise = undefined;
1951
- });
1952
- return this._flagsResponsePromise;
1953
- }
1954
- // We only store the flags and request id in the feature flag details storage key
1955
- setKnownFeatureFlagDetails(flagsResponse) {
1956
- this.wrap(() => {
1957
- this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagDetails, flagsResponse);
1958
- this._events.emit('featureflags', getFlagValuesFromFlags(flagsResponse?.flags ?? {}));
1959
- });
1960
- }
1961
- getKnownFeatureFlagDetails() {
1962
- const storedDetails = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagDetails);
1963
- if (!storedDetails) {
1964
- // Rebuild from the stored feature flags and feature flag payloads
1965
- const featureFlags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
1966
- const featureFlagPayloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
1967
- if (featureFlags === undefined && featureFlagPayloads === undefined) {
1968
- return undefined;
1969
- }
1970
- return createFlagsResponseFromFlagsAndPayloads(featureFlags ?? {}, featureFlagPayloads ?? {});
1971
- }
1972
- return normalizeFlagsResponse(storedDetails);
1973
- }
1974
- getKnownFeatureFlags() {
1975
- const featureFlagDetails = this.getKnownFeatureFlagDetails();
1976
- if (!featureFlagDetails) {
1977
- return undefined;
1978
- }
1979
- return getFlagValuesFromFlags(featureFlagDetails.flags);
1980
- }
1981
- getKnownFeatureFlagPayloads() {
1982
- const featureFlagDetails = this.getKnownFeatureFlagDetails();
1983
- if (!featureFlagDetails) {
1984
- return undefined;
1985
- }
1986
- return getPayloadsFromFlags(featureFlagDetails.flags);
1987
- }
1988
- getBootstrappedFeatureFlagDetails() {
1989
- const details = this.getPersistedProperty(PostHogPersistedProperty.BootstrapFeatureFlagDetails);
1990
- if (!details) {
1991
- return undefined;
1992
- }
1993
- return details;
1994
- }
1995
- setBootstrappedFeatureFlagDetails(details) {
1996
- this.setPersistedProperty(PostHogPersistedProperty.BootstrapFeatureFlagDetails, details);
1997
- }
1998
- getBootstrappedFeatureFlags() {
1999
- const details = this.getBootstrappedFeatureFlagDetails();
2000
- if (!details) {
2001
- return undefined;
2002
- }
2003
- return getFlagValuesFromFlags(details.flags);
2004
- }
2005
- getBootstrappedFeatureFlagPayloads() {
2006
- const details = this.getBootstrappedFeatureFlagDetails();
2007
- if (!details) {
2008
- return undefined;
2009
- }
2010
- return getPayloadsFromFlags(details.flags);
2011
- }
2012
- getFeatureFlag(key) {
2013
- const details = this.getFeatureFlagDetails();
2014
- if (!details) {
2015
- // If we haven't loaded flags yet, or errored out, we respond with undefined
2016
- return undefined;
2017
- }
2018
- const featureFlag = details.flags[key];
2019
- let response = getFeatureFlagValue(featureFlag);
2020
- if (response === undefined) {
2021
- // For cases where the flag is unknown, return false
2022
- response = false;
2023
- }
2024
- if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
2025
- const bootstrappedResponse = this.getBootstrappedFeatureFlags()?.[key];
2026
- const bootstrappedPayload = this.getBootstrappedFeatureFlagPayloads()?.[key];
2027
- this.flagCallReported[key] = true;
2028
- this.capture('$feature_flag_called', {
2029
- $feature_flag: key,
2030
- $feature_flag_response: response,
2031
- ...maybeAdd('$feature_flag_id', featureFlag?.metadata?.id),
2032
- ...maybeAdd('$feature_flag_version', featureFlag?.metadata?.version),
2033
- ...maybeAdd('$feature_flag_reason', featureFlag?.reason?.description ?? featureFlag?.reason?.code),
2034
- ...maybeAdd('$feature_flag_bootstrapped_response', bootstrappedResponse),
2035
- ...maybeAdd('$feature_flag_bootstrapped_payload', bootstrappedPayload),
2036
- // If we haven't yet received a response from the /flags endpoint, we must have used the bootstrapped value
2037
- $used_bootstrap_value: !this.getPersistedProperty(PostHogPersistedProperty.FlagsEndpointWasHit),
2038
- ...maybeAdd('$feature_flag_request_id', details.requestId),
2039
- });
2040
- }
2041
- // If we have flags we either return the value (true or string) or false
2042
- return response;
2043
- }
2044
- getFeatureFlagPayload(key) {
2045
- const payloads = this.getFeatureFlagPayloads();
2046
- if (!payloads) {
2047
- return undefined;
2048
- }
2049
- const response = payloads[key];
2050
- // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
2051
- if (response === undefined) {
2052
- return null;
2053
- }
2054
- return response;
2055
- }
2056
- getFeatureFlagPayloads() {
2057
- return this.getFeatureFlagDetails()?.featureFlagPayloads;
2058
- }
2059
- getFeatureFlags() {
2060
- // NOTE: We don't check for _initPromise here as the function is designed to be
2061
- // callable before the state being loaded anyways
2062
- return this.getFeatureFlagDetails()?.featureFlags;
2063
- }
2064
- getFeatureFlagDetails() {
2065
- // NOTE: We don't check for _initPromise here as the function is designed to be
2066
- // callable before the state being loaded anyways
2067
- let details = this.getKnownFeatureFlagDetails();
2068
- const overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
2069
- if (!overriddenFlags) {
2070
- return details;
2071
- }
2072
- details = details ?? { featureFlags: {}, featureFlagPayloads: {}, flags: {} };
2073
- const flags = details.flags ?? {};
2074
- for (const key in overriddenFlags) {
2075
- if (!overriddenFlags[key]) {
2076
- delete flags[key];
2077
- }
2078
- else {
2079
- flags[key] = updateFlagValue(flags[key], overriddenFlags[key]);
2080
- }
2081
- }
2082
- const result = {
2083
- ...details,
2084
- flags,
2085
- };
2086
- return normalizeFlagsResponse(result);
2087
- }
2088
- getFeatureFlagsAndPayloads() {
2089
- const flags = this.getFeatureFlags();
2090
- const payloads = this.getFeatureFlagPayloads();
2091
- return {
2092
- flags,
2093
- payloads,
2094
- };
2095
- }
2096
- isFeatureEnabled(key) {
2097
- const response = this.getFeatureFlag(key);
2098
- if (response === undefined) {
2099
- return undefined;
2100
- }
2101
- return !!response;
2102
- }
2103
- // Used when we want to trigger the reload but we don't care about the result
2104
- reloadFeatureFlags(options) {
2105
- this.flagsAsync(true)
2106
- .then((res) => {
2107
- options?.cb?.(undefined, res?.featureFlags);
2108
- })
2109
- .catch((e) => {
2110
- options?.cb?.(e, undefined);
2111
- if (!options?.cb) {
2112
- this.logMsgIfDebug(() => console.log('PostHog Debug', 'Error reloading feature flags', e));
2113
- }
2114
- });
2115
- }
2116
- async reloadRemoteConfigAsync() {
2117
- return await this.remoteConfigAsync();
2118
- }
2119
- async reloadFeatureFlagsAsync(sendAnonDistinctId) {
2120
- return (await this.flagsAsync(sendAnonDistinctId ?? true))?.featureFlags;
2121
- }
2122
- onFeatureFlags(cb) {
2123
- return this.on('featureflags', async () => {
2124
- const flags = this.getFeatureFlags();
2125
- if (flags) {
2126
- cb(flags);
2127
- }
2128
- });
2129
- }
2130
- onFeatureFlag(key, cb) {
2131
- return this.on('featureflags', async () => {
2132
- const flagResponse = this.getFeatureFlag(key);
2133
- if (flagResponse !== undefined) {
2134
- cb(flagResponse);
2135
- }
2136
- });
2137
- }
2138
- async overrideFeatureFlag(flags) {
2139
- this.wrap(() => {
2140
- if (flags === null) {
2141
- return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
2142
- }
2143
- return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
2144
- });
2145
- }
2146
- /***
2147
- *** ERROR TRACKING
2148
- ***/
2149
- captureException(error, additionalProperties) {
2150
- const properties = {
2151
- $exception_level: 'error',
2152
- $exception_list: [
2153
- {
2154
- type: isError(error) ? error.name : 'Error',
2155
- value: isError(error) ? error.message : error,
2156
- mechanism: {
2157
- handled: true,
2158
- synthetic: false,
2159
- },
2160
- },
2161
- ],
2162
- ...additionalProperties,
2163
- };
2164
- properties.$exception_personURL = new URL(`/project/${this.apiKey}/person/${this.getDistinctId()}`, this.host).toString();
2165
- this.capture('$exception', properties);
2166
- }
2167
- /**
2168
- * Capture written user feedback for a LLM trace. Numeric values are converted to strings.
2169
- * @param traceId The trace ID to capture feedback for.
2170
- * @param userFeedback The feedback to capture.
2171
- */
2172
- captureTraceFeedback(traceId, userFeedback) {
2173
- this.capture('$ai_feedback', {
2174
- $ai_feedback_text: userFeedback,
2175
- $ai_trace_id: String(traceId),
2176
- });
2177
- }
2178
- /**
2179
- * Capture a metric for a LLM trace. Numeric values are converted to strings.
2180
- * @param traceId The trace ID to capture the metric for.
2181
- * @param metricName The name of the metric to capture.
2182
- * @param metricValue The value of the metric to capture.
2183
- */
2184
- captureTraceMetric(traceId, metricName, metricValue) {
2185
- this.capture('$ai_metric', {
2186
- $ai_metric_name: metricName,
2187
- $ai_metric_value: String(metricValue),
2188
- $ai_trace_id: String(traceId),
2189
- });
2190
- }
2191
- }
2192
-
2193
- function getContext(window) {
2194
- let context = {};
2195
- if (window?.navigator) {
2196
- const userAgent = window.navigator.userAgent;
2197
- const osValue = os(window);
2198
- context = {
2199
- ...context,
2200
- ...(osValue !== undefined && {
2201
- $os: osValue
2202
- }),
2203
- $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
2204
- $referrer: window.document.referrer,
2205
- $referring_domain: referringDomain(window.document.referrer),
2206
- $device: device(userAgent),
2207
- $current_url: window.location.href,
2208
- $host: window.location.host,
2209
- $pathname: window.location.pathname,
2210
- $browser_version: browserVersion(userAgent, window.navigator.vendor, !!window.opera),
2211
- $screen_height: window.screen.height,
2212
- $screen_width: window.screen.width,
2213
- $screen_dpr: window.devicePixelRatio
2214
- };
2215
- }
2216
- context = {
2217
- ...context,
2218
- $lib: 'js',
2219
- $lib_version: version,
2220
- $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
2221
- $time: currentTimestamp() / 1000 // epoch time in seconds
2222
- };
2223
- return context; // TODO: strip empty props?
2224
- }
2225
- function includes(haystack, needle) {
2226
- return haystack.indexOf(needle) >= 0;
2227
- }
2228
- function browser(userAgent, vendor, opera) {
2229
- vendor = vendor || ''; // vendor is undefined for at least IE9
2230
- if (opera || includes(userAgent, ' OPR/')) {
2231
- if (includes(userAgent, 'Mini')) {
2232
- return 'Opera Mini';
2233
- }
2234
- return 'Opera';
2235
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
2236
- return 'BlackBerry';
2237
- } else if (includes(userAgent, 'IEMobile') || includes(userAgent, 'WPDesktop')) {
2238
- return 'Internet Explorer Mobile';
2239
- } else if (includes(userAgent, 'SamsungBrowser/')) {
2240
- // https://developer.samsung.com/internet/user-agent-string-format
2241
- return 'Samsung Internet';
2242
- } else if (includes(userAgent, 'Edge') || includes(userAgent, 'Edg/')) {
2243
- return 'Microsoft Edge';
2244
- } else if (includes(userAgent, 'FBIOS')) {
2245
- return 'Facebook Mobile';
2246
- } else if (includes(userAgent, 'Chrome')) {
2247
- return 'Chrome';
2248
- } else if (includes(userAgent, 'CriOS')) {
2249
- return 'Chrome iOS';
2250
- } else if (includes(userAgent, 'UCWEB') || includes(userAgent, 'UCBrowser')) {
2251
- return 'UC Browser';
2252
- } else if (includes(userAgent, 'FxiOS')) {
2253
- return 'Firefox iOS';
2254
- } else if (includes(vendor, 'Apple')) {
2255
- if (includes(userAgent, 'Mobile')) {
2256
- return 'Mobile Safari';
2257
- }
2258
- return 'Safari';
2259
- } else if (includes(userAgent, 'Android')) {
2260
- return 'Android Mobile';
2261
- } else if (includes(userAgent, 'Konqueror')) {
2262
- return 'Konqueror';
2263
- } else if (includes(userAgent, 'Firefox')) {
2264
- return 'Firefox';
2265
- } else if (includes(userAgent, 'MSIE') || includes(userAgent, 'Trident/')) {
2266
- return 'Internet Explorer';
2267
- } else if (includes(userAgent, 'Gecko')) {
2268
- return 'Mozilla';
2269
- } else {
2270
- return '';
2271
- }
2272
- }
2273
- function browserVersion(userAgent, vendor, opera) {
2274
- const regexList = {
2275
- 'Internet Explorer Mobile': /rv:(\d+(\.\d+)?)/,
2276
- 'Microsoft Edge': /Edge?\/(\d+(\.\d+)?)/,
2277
- Chrome: /Chrome\/(\d+(\.\d+)?)/,
2278
- 'Chrome iOS': /CriOS\/(\d+(\.\d+)?)/,
2279
- 'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/,
2280
- Safari: /Version\/(\d+(\.\d+)?)/,
2281
- 'Mobile Safari': /Version\/(\d+(\.\d+)?)/,
2282
- Opera: /(Opera|OPR)\/(\d+(\.\d+)?)/,
2283
- Firefox: /Firefox\/(\d+(\.\d+)?)/,
2284
- 'Firefox iOS': /FxiOS\/(\d+(\.\d+)?)/,
2285
- Konqueror: /Konqueror:(\d+(\.\d+)?)/,
2286
- BlackBerry: /BlackBerry (\d+(\.\d+)?)/,
2287
- 'Android Mobile': /android\s(\d+(\.\d+)?)/,
2288
- 'Samsung Internet': /SamsungBrowser\/(\d+(\.\d+)?)/,
2289
- 'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)?)/,
2290
- Mozilla: /rv:(\d+(\.\d+)?)/
2291
- };
2292
- const browserString = browser(userAgent, vendor, opera);
2293
- const regex = regexList[browserString] || undefined;
2294
- if (regex === undefined) {
2295
- return null;
2296
- }
2297
- const matches = userAgent.match(regex);
2298
- if (!matches) {
2299
- return null;
2300
- }
2301
- return parseFloat(matches[matches.length - 2]);
2302
- }
2303
- function os(window) {
2304
- if (!window?.navigator) {
2305
- return undefined;
2306
- }
2307
- const a = window.navigator.userAgent;
2308
- if (/Windows/i.test(a)) {
2309
- if (/Phone/.test(a) || /WPDesktop/.test(a)) {
2310
- return 'Windows Phone';
2311
- }
2312
- return 'Windows';
2313
- } else if (/(iPhone|iPad|iPod)/.test(a)) {
2314
- return 'iOS';
2315
- } else if (/Android/.test(a)) {
2316
- return 'Android';
2317
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {
2318
- return 'BlackBerry';
2319
- } else if (/Mac/i.test(a)) {
2320
- return 'Mac OS X';
2321
- } else if (/Linux/.test(a)) {
2322
- return 'Linux';
2323
- } else if (/CrOS/.test(a)) {
2324
- return 'Chrome OS';
2325
- } else {
2326
- return undefined;
2327
- }
2328
- }
2329
- function device(userAgent) {
2330
- if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {
2331
- return 'Windows Phone';
2332
- } else if (/iPad/.test(userAgent)) {
2333
- return 'iPad';
2334
- } else if (/iPod/.test(userAgent)) {
2335
- return 'iPod Touch';
2336
- } else if (/iPhone/.test(userAgent)) {
2337
- return 'iPhone';
2338
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
2339
- return 'BlackBerry';
2340
- } else if (/Android/.test(userAgent)) {
2341
- return 'Android';
2342
- } else {
2343
- return '';
2344
- }
2345
- }
2346
- function referringDomain(referrer) {
2347
- const split = referrer.split('/');
2348
- if (split.length >= 3) {
2349
- return split[2];
2350
- }
2351
- return '';
2352
- }
2353
-
2354
- // Methods partially borrowed from quirksmode.org/js/cookies.html
2355
- const cookieStore = {
2356
- getItem(key) {
2357
- try {
2358
- const nameEQ = key + '=';
2359
- const ca = document.cookie.split(';');
2360
- for (let i = 0; i < ca.length; i++) {
2361
- let c = ca[i];
2362
- while (c.charAt(0) == ' ') {
2363
- c = c.substring(1, c.length);
2364
- }
2365
- if (c.indexOf(nameEQ) === 0) {
2366
- return decodeURIComponent(c.substring(nameEQ.length, c.length));
2367
- }
2368
- }
2369
- } catch (err) {}
2370
- return null;
2371
- },
2372
- setItem(key, value) {
2373
- try {
2374
- const cdomain = '',
2375
- expires = '',
2376
- secure = '';
2377
- const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure;
2378
- document.cookie = new_cookie_val;
2379
- } catch (err) {
2380
- return;
2381
- }
2382
- },
2383
- removeItem(name) {
2384
- try {
2385
- cookieStore.setItem(name, '');
2386
- } catch (err) {
2387
- return;
2388
- }
2389
- },
2390
- clear() {
2391
- document.cookie = '';
2392
- },
2393
- getAllKeys() {
2394
- const ca = document.cookie.split(';');
2395
- const keys = [];
2396
- for (let i = 0; i < ca.length; i++) {
2397
- let c = ca[i];
2398
- while (c.charAt(0) == ' ') {
2399
- c = c.substring(1, c.length);
2400
- }
2401
- keys.push(c.split('=')[0]);
2402
- }
2403
- return keys;
2404
- }
2405
- };
2406
- const createStorageLike = store => {
2407
- return {
2408
- getItem(key) {
2409
- return store.getItem(key);
2410
- },
2411
- setItem(key, value) {
2412
- store.setItem(key, value);
2413
- },
2414
- removeItem(key) {
2415
- store.removeItem(key);
2416
- },
2417
- clear() {
2418
- store.clear();
2419
- },
2420
- getAllKeys() {
2421
- const keys = [];
2422
- for (const key in localStorage) {
2423
- keys.push(key);
2424
- }
2425
- return keys;
2426
- }
2427
- };
2428
- };
2429
- const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
2430
- try {
2431
- const val = 'xyz';
2432
- storage.setItem(key, val);
2433
- if (storage.getItem(key) !== val) {
2434
- return false;
2435
- }
2436
- storage.removeItem(key);
2437
- return true;
2438
- } catch (err) {
2439
- return false;
2440
- }
2441
- };
2442
- let localStore = undefined;
2443
- let sessionStore = undefined;
2444
- const createMemoryStorage = () => {
2445
- const _cache = {};
2446
- const store = {
2447
- getItem(key) {
2448
- return _cache[key];
2449
- },
2450
- setItem(key, value) {
2451
- _cache[key] = value !== null ? value : undefined;
2452
- },
2453
- removeItem(key) {
2454
- delete _cache[key];
2455
- },
2456
- clear() {
2457
- for (const key in _cache) {
2458
- delete _cache[key];
2459
- }
2460
- },
2461
- getAllKeys() {
2462
- const keys = [];
2463
- for (const key in _cache) {
2464
- keys.push(key);
2465
- }
2466
- return keys;
2467
- }
2468
- };
2469
- return store;
2470
- };
2471
- const getStorage = (type, window) => {
2472
- if (window) {
2473
- if (!localStore) {
2474
- const _localStore = createStorageLike(window.localStorage);
2475
- localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined;
2476
- }
2477
- if (!sessionStore) {
2478
- const _sessionStore = createStorageLike(window.sessionStorage);
2479
- sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined;
2480
- }
2481
- }
2482
- switch (type) {
2483
- case 'cookie':
2484
- return cookieStore || localStore || sessionStore || createMemoryStorage();
2485
- case 'localStorage':
2486
- return localStore || sessionStore || createMemoryStorage();
2487
- case 'sessionStorage':
2488
- return sessionStore || createMemoryStorage();
2489
- case 'memory':
2490
- return createMemoryStorage();
2491
- default:
2492
- return createMemoryStorage();
2493
- }
2494
- };
2495
-
2496
- // import { patch } from 'rrweb/typings/utils'
2497
- // copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts
2498
- // which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129
2499
- // which was copied from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
2500
- // copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4
2501
- const isFunction = function (f) {
2502
- return typeof f === 'function';
2503
- };
2504
- function patch(source, name, replacement) {
2505
- try {
2506
- if (!(name in source)) {
2507
- return () => {
2508
- //
2509
- };
2510
- }
2511
- const original = source[name];
2512
- const wrapped = replacement(original);
2513
- // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work
2514
- // otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
2515
- if (isFunction(wrapped)) {
2516
- wrapped.prototype = wrapped.prototype || {};
2517
- Object.defineProperties(wrapped, {
2518
- __posthog_wrapped__: {
2519
- enumerable: false,
2520
- value: true
2521
- }
2522
- });
2523
- }
2524
- source[name] = wrapped;
2525
- return () => {
2526
- source[name] = original;
2527
- };
2528
- } catch {
2529
- return () => {
2530
- //
2531
- };
2532
- // This can throw if multiple fill happens on a global object like XMLHttpRequest
2533
- // Fixes https://github.com/getsentry/sentry-javascript/issues/2043
2534
- }
2535
- }
2536
-
2537
- class PostHog extends PostHogCore {
2538
- constructor(apiKey, options) {
2539
- super(apiKey, options);
2540
- this._lastPathname = '';
2541
- // posthog-js stores options in one object on
2542
- this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
2543
- this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
2544
- this.setupBootstrap(options);
2545
- if (options?.preloadFeatureFlags !== false) {
2546
- this.reloadFeatureFlags();
2547
- }
2548
- if (options?.captureHistoryEvents && typeof window !== 'undefined') {
2549
- this._lastPathname = window?.location?.pathname || '';
2550
- this.setupHistoryEventTracking();
2551
- }
2552
- }
2553
- getWindow() {
2554
- return typeof window !== 'undefined' ? window : undefined;
2555
- }
2556
- getPersistedProperty(key) {
2557
- if (!this._storageCache) {
2558
- this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
2559
- }
2560
- return this._storageCache[key];
2561
- }
2562
- setPersistedProperty(key, value) {
2563
- if (!this._storageCache) {
2564
- this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
2565
- }
2566
- if (value === null) {
2567
- delete this._storageCache[key];
2568
- } else {
2569
- this._storageCache[key] = value;
2570
- }
2571
- this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
2572
- }
2573
- fetch(url, options) {
2574
- const fetchFn = getFetch();
2575
- if (!fetchFn) {
2576
- // error will be handled by the caller (fetchWithRetry)
2577
- return Promise.reject(new Error('Fetch API is not available in this environment.'));
2578
- }
2579
- return fetchFn(url, options);
2580
- }
2581
- getLibraryId() {
2582
- return 'posthog-js-lite';
2583
- }
2584
- getLibraryVersion() {
2585
- return version;
2586
- }
2587
- getCustomUserAgent() {
2588
- return;
2589
- }
2590
- getCommonEventProperties() {
2591
- return {
2592
- ...super.getCommonEventProperties(),
2593
- ...getContext(this.getWindow())
2594
- };
2595
- }
2596
- setupHistoryEventTracking() {
2597
- const window = this.getWindow();
2598
- if (!window) {
2599
- return;
2600
- }
2601
- // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable
2602
- // eslint-disable-next-line @typescript-eslint/no-this-alias
2603
- const self = this;
2604
- patch(window.history, 'pushState', originalPushState => {
2605
- return function patchedPushState(state, title, url) {
2606
- ;
2607
- originalPushState.call(this, state, title, url);
2608
- self.captureNavigationEvent('pushState');
2609
- };
2610
- });
2611
- patch(window.history, 'replaceState', originalReplaceState => {
2612
- return function patchedReplaceState(state, title, url) {
2613
- ;
2614
- originalReplaceState.call(this, state, title, url);
2615
- self.captureNavigationEvent('replaceState');
2616
- };
2617
- });
2618
- // For popstate we need to listen to the event instead of overriding a method
2619
- window.addEventListener('popstate', () => {
2620
- this.captureNavigationEvent('popstate');
2621
- });
2622
- }
2623
- captureNavigationEvent(navigationType) {
2624
- const window = this.getWindow();
2625
- if (!window) {
2626
- return;
2627
- }
2628
- const currentPathname = window.location.pathname;
2629
- // Only capture pageview if the pathname has changed
2630
- if (currentPathname !== this._lastPathname) {
2631
- this.capture('$pageview', {
2632
- navigation_type: navigationType
2633
- });
2634
- this._lastPathname = currentPathname;
2635
- }
2636
- }
2637
- }
2638
-
2639
- exports.PostHog = PostHog;
2640
- exports["default"] = PostHog;
2641
- //# sourceMappingURL=index.cjs.map