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