posthog-js-lite 3.5.1 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/index.cjs +89 -39
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.ts +44 -37
- package/lib/index.mjs +89 -39
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/posthog-web.ts +7 -2
package/lib/index.d.ts
CHANGED
|
@@ -70,6 +70,7 @@ declare enum PostHogPersistedProperty {
|
|
|
70
70
|
Queue = "queue",
|
|
71
71
|
OptedOut = "opted_out",
|
|
72
72
|
SessionId = "session_id",
|
|
73
|
+
SessionStartTimestamp = "session_start_timestamp",
|
|
73
74
|
SessionLastTimestamp = "session_timestamp",
|
|
74
75
|
PersonProperties = "person_properties",
|
|
75
76
|
GroupProperties = "group_properties",
|
|
@@ -105,7 +106,10 @@ type PostHogFetchResponse = {
|
|
|
105
106
|
json: () => Promise<any>;
|
|
106
107
|
};
|
|
107
108
|
type PostHogEventProperties = {
|
|
108
|
-
[key: string]:
|
|
109
|
+
[key: string]: JsonType;
|
|
110
|
+
};
|
|
111
|
+
type PostHogGroupProperties = {
|
|
112
|
+
[type: string]: string | number;
|
|
109
113
|
};
|
|
110
114
|
type PostHogAutocaptureElement = {
|
|
111
115
|
$el_text?: string;
|
|
@@ -114,9 +118,7 @@ type PostHogAutocaptureElement = {
|
|
|
114
118
|
nth_child?: number;
|
|
115
119
|
nth_of_type?: number;
|
|
116
120
|
order?: number;
|
|
117
|
-
} &
|
|
118
|
-
[key: string]: any;
|
|
119
|
-
};
|
|
121
|
+
} & PostHogEventProperties;
|
|
120
122
|
type PostHogRemoteConfig = {
|
|
121
123
|
sessionRecording?: boolean | {
|
|
122
124
|
[key: string]: JsonType;
|
|
@@ -182,7 +184,7 @@ type PartialWithRequired<T, K extends keyof T> = {
|
|
|
182
184
|
type PostHogFeatureFlagDetails = PartialWithRequired<PostHogDecideResponse, 'flags' | 'featureFlags' | 'featureFlagPayloads' | 'requestId'>;
|
|
183
185
|
type JsonType = string | number | boolean | null | {
|
|
184
186
|
[key: string]: JsonType;
|
|
185
|
-
} | Array<JsonType
|
|
187
|
+
} | Array<JsonType> | JsonType[];
|
|
186
188
|
type FeatureFlagDetail = {
|
|
187
189
|
key: string;
|
|
188
190
|
enabled: boolean;
|
|
@@ -430,7 +432,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
430
432
|
constructor(apiKey: string, options?: PostHogCoreOptions);
|
|
431
433
|
protected logMsgIfDebug(fn: () => void): void;
|
|
432
434
|
protected wrap(fn: () => void): void;
|
|
433
|
-
protected getCommonEventProperties():
|
|
435
|
+
protected getCommonEventProperties(): PostHogEventProperties;
|
|
434
436
|
get optedOut(): boolean;
|
|
435
437
|
optIn(): Promise<void>;
|
|
436
438
|
optOut(): Promise<void>;
|
|
@@ -445,18 +447,10 @@ declare abstract class PostHogCoreStateless {
|
|
|
445
447
|
***/
|
|
446
448
|
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
447
449
|
protected identifyStatelessImmediate(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): Promise<void>;
|
|
448
|
-
protected captureStateless(distinctId: string, event: string, properties?:
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
protected
|
|
452
|
-
[key: string]: any;
|
|
453
|
-
}, options?: PostHogCaptureOptions): Promise<void>;
|
|
454
|
-
protected aliasStateless(alias: string, distinctId: string, properties?: {
|
|
455
|
-
[key: string]: any;
|
|
456
|
-
}, options?: PostHogCaptureOptions): void;
|
|
457
|
-
protected aliasStatelessImmediate(alias: string, distinctId: string, properties?: {
|
|
458
|
-
[key: string]: any;
|
|
459
|
-
}, options?: PostHogCaptureOptions): Promise<void>;
|
|
450
|
+
protected captureStateless(distinctId: string, event: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
451
|
+
protected captureStatelessImmediate(distinctId: string, event: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): Promise<void>;
|
|
452
|
+
protected aliasStateless(alias: string, distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
453
|
+
protected aliasStatelessImmediate(alias: string, distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): Promise<void>;
|
|
460
454
|
/***
|
|
461
455
|
*** GROUPS
|
|
462
456
|
***/
|
|
@@ -497,9 +491,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
497
491
|
private _props;
|
|
498
492
|
protected get props(): PostHogEventProperties;
|
|
499
493
|
protected set props(val: PostHogEventProperties | undefined);
|
|
500
|
-
register(properties:
|
|
501
|
-
[key: string]: any;
|
|
502
|
-
}): Promise<void>;
|
|
494
|
+
register(properties: PostHogEventProperties): Promise<void>;
|
|
503
495
|
unregister(property: string): Promise<void>;
|
|
504
496
|
/***
|
|
505
497
|
*** QUEUEING AND FLUSHING
|
|
@@ -513,7 +505,29 @@ declare abstract class PostHogCoreStateless {
|
|
|
513
505
|
* Avoids unnecessary promise errors
|
|
514
506
|
*/
|
|
515
507
|
private flushBackground;
|
|
516
|
-
|
|
508
|
+
/**
|
|
509
|
+
* Flushes the queue
|
|
510
|
+
*
|
|
511
|
+
* This function will return a promise that will resolve when the flush is complete,
|
|
512
|
+
* or reject if there was an error (for example if the server or network is down).
|
|
513
|
+
*
|
|
514
|
+
* If there is already a flush in progress, this function will wait for that flush to complete.
|
|
515
|
+
*
|
|
516
|
+
* It's recommended to do error handling in the callback of the promise.
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* posthog.flush().then(() => {
|
|
520
|
+
* console.log('Flush complete')
|
|
521
|
+
* }).catch((err) => {
|
|
522
|
+
* console.error('Flush failed', err)
|
|
523
|
+
* })
|
|
524
|
+
*
|
|
525
|
+
*
|
|
526
|
+
* @throws PostHogFetchHttpError
|
|
527
|
+
* @throws PostHogFetchNetworkError
|
|
528
|
+
* @throws Error
|
|
529
|
+
*/
|
|
530
|
+
flush(): Promise<void>;
|
|
517
531
|
protected getCustomHeaders(): {
|
|
518
532
|
[key: string]: string;
|
|
519
533
|
};
|
|
@@ -532,13 +546,14 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
532
546
|
private flagCallReported;
|
|
533
547
|
protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined>;
|
|
534
548
|
protected _sessionExpirationTimeSeconds: number;
|
|
549
|
+
private _sessionMaxLengthSeconds;
|
|
535
550
|
protected sessionProps: PostHogEventProperties;
|
|
536
551
|
constructor(apiKey: string, options?: PostHogCoreOptions);
|
|
537
552
|
protected setupBootstrap(options?: Partial<PostHogCoreOptions>): void;
|
|
538
553
|
private clearProps;
|
|
539
554
|
on(event: string, cb: (...args: any[]) => void): () => void;
|
|
540
555
|
reset(propertiesToKeep?: PostHogPersistedProperty[]): void;
|
|
541
|
-
protected getCommonEventProperties():
|
|
556
|
+
protected getCommonEventProperties(): PostHogEventProperties;
|
|
542
557
|
private enrichProperties;
|
|
543
558
|
/**
|
|
544
559
|
* * @returns {string} The stored session ID for the current session. This may be an empty string if the client is not yet fully initialized.
|
|
@@ -553,25 +568,19 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
553
568
|
* * @returns {string} The stored distinct ID. This may be an empty string if the client is not yet fully initialized.
|
|
554
569
|
*/
|
|
555
570
|
getDistinctId(): string;
|
|
556
|
-
registerForSession(properties:
|
|
557
|
-
[key: string]: any;
|
|
558
|
-
}): void;
|
|
571
|
+
registerForSession(properties: PostHogEventProperties): void;
|
|
559
572
|
unregisterForSession(property: string): void;
|
|
560
573
|
/***
|
|
561
574
|
*** TRACKING
|
|
562
575
|
***/
|
|
563
576
|
identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
564
|
-
capture(event: string, properties?:
|
|
565
|
-
[key: string]: any;
|
|
566
|
-
}, options?: PostHogCaptureOptions): void;
|
|
577
|
+
capture(event: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
567
578
|
alias(alias: string): void;
|
|
568
579
|
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
569
580
|
/***
|
|
570
581
|
*** GROUPS
|
|
571
582
|
***/
|
|
572
|
-
groups(groups:
|
|
573
|
-
[type: string]: string | number;
|
|
574
|
-
}): void;
|
|
583
|
+
groups(groups: PostHogGroupProperties): void;
|
|
575
584
|
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
576
585
|
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
577
586
|
/***
|
|
@@ -603,7 +612,7 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
603
612
|
private _decideAsync;
|
|
604
613
|
private setKnownFeatureFlagDetails;
|
|
605
614
|
private getKnownFeatureFlagDetails;
|
|
606
|
-
|
|
615
|
+
protected getKnownFeatureFlags(): PostHogDecideResponse['featureFlags'] | undefined;
|
|
607
616
|
private getKnownFeatureFlagPayloads;
|
|
608
617
|
private getBootstrappedFeatureFlagDetails;
|
|
609
618
|
private setBootstrappedFeatureFlagDetails;
|
|
@@ -628,9 +637,7 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
628
637
|
/***
|
|
629
638
|
*** ERROR TRACKING
|
|
630
639
|
***/
|
|
631
|
-
captureException(error: unknown, additionalProperties?:
|
|
632
|
-
[key: string]: any;
|
|
633
|
-
}): void;
|
|
640
|
+
captureException(error: unknown, additionalProperties?: PostHogEventProperties): void;
|
|
634
641
|
/**
|
|
635
642
|
* Capture written user feedback for a LLM trace. Numeric values are converted to strings.
|
|
636
643
|
* @param traceId The trace ID to capture feedback for.
|
|
@@ -666,7 +673,7 @@ declare class PostHog extends PostHogCore {
|
|
|
666
673
|
getLibraryId(): string;
|
|
667
674
|
getLibraryVersion(): string;
|
|
668
675
|
getCustomUserAgent(): void;
|
|
669
|
-
getCommonEventProperties():
|
|
676
|
+
getCommonEventProperties(): PostHogEventProperties;
|
|
670
677
|
private setupHistoryEventTracking;
|
|
671
678
|
private captureNavigationEvent;
|
|
672
679
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var version = "3.
|
|
1
|
+
var version = "3.6.0";
|
|
2
2
|
|
|
3
3
|
var PostHogPersistedProperty;
|
|
4
4
|
(function (PostHogPersistedProperty) {
|
|
@@ -15,6 +15,7 @@ var PostHogPersistedProperty;
|
|
|
15
15
|
PostHogPersistedProperty["Queue"] = "queue";
|
|
16
16
|
PostHogPersistedProperty["OptedOut"] = "opted_out";
|
|
17
17
|
PostHogPersistedProperty["SessionId"] = "session_id";
|
|
18
|
+
PostHogPersistedProperty["SessionStartTimestamp"] = "session_start_timestamp";
|
|
18
19
|
PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
|
|
19
20
|
PostHogPersistedProperty["PersonProperties"] = "person_properties";
|
|
20
21
|
PostHogPersistedProperty["GroupProperties"] = "group_properties";
|
|
@@ -367,6 +368,9 @@ function isTokenInRollout(token, percentage = 0, excludedHashes) {
|
|
|
367
368
|
const hashInt = parseInt(tokenHash, 16);
|
|
368
369
|
const hashFloat = hashInt / 0xffffffff;
|
|
369
370
|
return hashFloat < percentage;
|
|
371
|
+
}
|
|
372
|
+
function allSettled(promises) {
|
|
373
|
+
return Promise.all(promises.map((p) => (p ?? Promise.resolve()).then((value) => ({ status: 'fulfilled', value }), (reason) => ({ status: 'rejected', reason }))));
|
|
370
374
|
}
|
|
371
375
|
|
|
372
376
|
// Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
|
|
@@ -1257,6 +1261,7 @@ class PostHogFetchNetworkError extends Error {
|
|
|
1257
1261
|
this.name = 'PostHogFetchNetworkError';
|
|
1258
1262
|
}
|
|
1259
1263
|
}
|
|
1264
|
+
const maybeAdd = (key, value) => value !== undefined ? { [key]: value } : {};
|
|
1260
1265
|
async function logFlushError(err) {
|
|
1261
1266
|
if (err instanceof PostHogFetchHttpError) {
|
|
1262
1267
|
let text = '';
|
|
@@ -1517,6 +1522,7 @@ class PostHogCoreStateless {
|
|
|
1517
1522
|
...extraPayload,
|
|
1518
1523
|
}),
|
|
1519
1524
|
};
|
|
1525
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', 'Decide URL', url));
|
|
1520
1526
|
// Don't retry /decide API calls
|
|
1521
1527
|
return this.fetchWithRetry(url, fetchOptions, { retryCount: 0 }, this.featureFlagsRequestTimeoutMs)
|
|
1522
1528
|
.then((response) => response.json())
|
|
@@ -1634,7 +1640,7 @@ class PostHogCoreStateless {
|
|
|
1634
1640
|
async getSurveysStateless() {
|
|
1635
1641
|
await this._initPromise;
|
|
1636
1642
|
if (this.disableSurveys === true) {
|
|
1637
|
-
this.logMsgIfDebug(() => console.log('Loading surveys is disabled.'));
|
|
1643
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', 'Loading surveys is disabled.'));
|
|
1638
1644
|
return [];
|
|
1639
1645
|
}
|
|
1640
1646
|
const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
|
|
@@ -1757,7 +1763,6 @@ class PostHogCoreStateless {
|
|
|
1757
1763
|
}
|
|
1758
1764
|
catch (err) {
|
|
1759
1765
|
this._events.emit('error', err);
|
|
1760
|
-
throw err;
|
|
1761
1766
|
}
|
|
1762
1767
|
}
|
|
1763
1768
|
prepareMessage(type, _message, options) {
|
|
@@ -1797,15 +1802,38 @@ class PostHogCoreStateless {
|
|
|
1797
1802
|
await logFlushError(err);
|
|
1798
1803
|
});
|
|
1799
1804
|
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Flushes the queue
|
|
1807
|
+
*
|
|
1808
|
+
* This function will return a promise that will resolve when the flush is complete,
|
|
1809
|
+
* or reject if there was an error (for example if the server or network is down).
|
|
1810
|
+
*
|
|
1811
|
+
* If there is already a flush in progress, this function will wait for that flush to complete.
|
|
1812
|
+
*
|
|
1813
|
+
* It's recommended to do error handling in the callback of the promise.
|
|
1814
|
+
*
|
|
1815
|
+
* @example
|
|
1816
|
+
* posthog.flush().then(() => {
|
|
1817
|
+
* console.log('Flush complete')
|
|
1818
|
+
* }).catch((err) => {
|
|
1819
|
+
* console.error('Flush failed', err)
|
|
1820
|
+
* })
|
|
1821
|
+
*
|
|
1822
|
+
*
|
|
1823
|
+
* @throws PostHogFetchHttpError
|
|
1824
|
+
* @throws PostHogFetchNetworkError
|
|
1825
|
+
* @throws Error
|
|
1826
|
+
*/
|
|
1800
1827
|
async flush() {
|
|
1801
1828
|
// Wait for the current flush operation to finish (regardless of success or failure), then try to flush again.
|
|
1802
1829
|
// Use allSettled instead of finally to be defensive around flush throwing errors immediately rather than rejecting.
|
|
1803
|
-
|
|
1830
|
+
// Use a custom allSettled implementation to avoid issues with patching Promise on RN
|
|
1831
|
+
const nextFlushPromise = allSettled([this.flushPromise]).then(() => {
|
|
1804
1832
|
return this._flush();
|
|
1805
1833
|
});
|
|
1806
1834
|
this.flushPromise = nextFlushPromise;
|
|
1807
1835
|
void this.addPendingPromise(nextFlushPromise);
|
|
1808
|
-
|
|
1836
|
+
allSettled([nextFlushPromise]).then(() => {
|
|
1809
1837
|
// If there are no others waiting to flush, clear the promise.
|
|
1810
1838
|
// We don't strictly need to do this, but it could make debugging easier
|
|
1811
1839
|
if (this.flushPromise === nextFlushPromise) {
|
|
@@ -1831,7 +1859,7 @@ class PostHogCoreStateless {
|
|
|
1831
1859
|
await this._initPromise;
|
|
1832
1860
|
let queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
1833
1861
|
if (!queue.length) {
|
|
1834
|
-
return
|
|
1862
|
+
return;
|
|
1835
1863
|
}
|
|
1836
1864
|
const sentMessages = [];
|
|
1837
1865
|
const originalQueueLength = queue.length;
|
|
@@ -1902,7 +1930,6 @@ class PostHogCoreStateless {
|
|
|
1902
1930
|
sentMessages.push(...batchMessages);
|
|
1903
1931
|
}
|
|
1904
1932
|
this._events.emit('flush', sentMessages);
|
|
1905
|
-
return sentMessages;
|
|
1906
1933
|
}
|
|
1907
1934
|
async fetchWithRetry(url, options, retryOptions, requestTimeout) {
|
|
1908
1935
|
var _a;
|
|
@@ -1912,7 +1939,14 @@ class PostHogCoreStateless {
|
|
|
1912
1939
|
return ctrl.signal;
|
|
1913
1940
|
});
|
|
1914
1941
|
const body = options.body ? options.body : '';
|
|
1915
|
-
|
|
1942
|
+
let reqByteLength = -1;
|
|
1943
|
+
try {
|
|
1944
|
+
reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
|
|
1945
|
+
}
|
|
1946
|
+
catch {
|
|
1947
|
+
const encoded = new TextEncoder().encode(body);
|
|
1948
|
+
reqByteLength = encoded.length;
|
|
1949
|
+
}
|
|
1916
1950
|
return await retriable(async () => {
|
|
1917
1951
|
let res = null;
|
|
1918
1952
|
try {
|
|
@@ -2001,6 +2035,7 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2001
2035
|
const featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 10000; // 10 seconds
|
|
2002
2036
|
super(apiKey, { ...options, disableGeoip: disableGeoipOption, featureFlagsRequestTimeoutMs });
|
|
2003
2037
|
this.flagCallReported = {};
|
|
2038
|
+
this._sessionMaxLengthSeconds = 24 * 60 * 60; // 24 hours
|
|
2004
2039
|
this.sessionProps = {};
|
|
2005
2040
|
this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
|
|
2006
2041
|
this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800; // 30 minutes
|
|
@@ -2074,7 +2109,7 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2074
2109
|
}
|
|
2075
2110
|
}
|
|
2076
2111
|
return {
|
|
2077
|
-
$active_feature_flags
|
|
2112
|
+
...maybeAdd('$active_feature_flags', featureFlags ? Object.keys(featureFlags) : undefined),
|
|
2078
2113
|
...featureVariantProperties,
|
|
2079
2114
|
...super.getCommonEventProperties(),
|
|
2080
2115
|
};
|
|
@@ -2096,18 +2131,26 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2096
2131
|
return '';
|
|
2097
2132
|
}
|
|
2098
2133
|
let sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
|
|
2099
|
-
const
|
|
2100
|
-
|
|
2134
|
+
const sessionLastTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp) || 0;
|
|
2135
|
+
const sessionStartTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp) || 0;
|
|
2136
|
+
const now = Date.now();
|
|
2137
|
+
const sessionLastDif = now - sessionLastTimestamp;
|
|
2138
|
+
const sessionStartDif = now - sessionStartTimestamp;
|
|
2139
|
+
if (!sessionId ||
|
|
2140
|
+
sessionLastDif > this._sessionExpirationTimeSeconds * 1000 ||
|
|
2141
|
+
sessionStartDif > this._sessionMaxLengthSeconds * 1000) {
|
|
2101
2142
|
sessionId = uuidv7();
|
|
2102
2143
|
this.setPersistedProperty(PostHogPersistedProperty.SessionId, sessionId);
|
|
2144
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, now);
|
|
2103
2145
|
}
|
|
2104
|
-
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp,
|
|
2146
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, now);
|
|
2105
2147
|
return sessionId;
|
|
2106
2148
|
}
|
|
2107
2149
|
resetSessionId() {
|
|
2108
2150
|
this.wrap(() => {
|
|
2109
2151
|
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
|
|
2110
2152
|
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, null);
|
|
2153
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, null);
|
|
2111
2154
|
});
|
|
2112
2155
|
}
|
|
2113
2156
|
/**
|
|
@@ -2159,8 +2202,8 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2159
2202
|
const userProps = properties?.$set || properties;
|
|
2160
2203
|
const allProperties = this.enrichProperties({
|
|
2161
2204
|
$anon_distinct_id: this.getAnonymousId(),
|
|
2162
|
-
$set
|
|
2163
|
-
$set_once
|
|
2205
|
+
...maybeAdd('$set', userProps),
|
|
2206
|
+
...maybeAdd('$set_once', userPropsOnce),
|
|
2164
2207
|
});
|
|
2165
2208
|
if (distinctId !== previousDistinctId) {
|
|
2166
2209
|
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
@@ -2308,14 +2351,16 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2308
2351
|
}
|
|
2309
2352
|
return this._decideAsync(sendAnonDistinctId);
|
|
2310
2353
|
}
|
|
2311
|
-
cacheSessionReplay(response) {
|
|
2354
|
+
cacheSessionReplay(source, response) {
|
|
2312
2355
|
const sessionReplay = response?.sessionRecording;
|
|
2313
2356
|
if (sessionReplay) {
|
|
2314
2357
|
this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
|
|
2315
|
-
this.logMsgIfDebug(() => console.log('PostHog Debug',
|
|
2358
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', `Session replay config from ${source}: `, JSON.stringify(sessionReplay)));
|
|
2316
2359
|
}
|
|
2317
|
-
else {
|
|
2318
|
-
|
|
2360
|
+
else if (typeof sessionReplay === 'boolean' && sessionReplay === false) {
|
|
2361
|
+
// if session replay is disabled, we don't need to cache it
|
|
2362
|
+
// we need to check for this because the response might be undefined (/flags does not return sessionRecording yet)
|
|
2363
|
+
this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay config from ${source} disabled.`));
|
|
2319
2364
|
this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
|
|
2320
2365
|
}
|
|
2321
2366
|
}
|
|
@@ -2329,25 +2374,30 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2329
2374
|
const remoteConfigWithoutSurveys = { ...response };
|
|
2330
2375
|
delete remoteConfigWithoutSurveys.surveys;
|
|
2331
2376
|
this.logMsgIfDebug(() => console.log('PostHog Debug', 'Fetched remote config: ', JSON.stringify(remoteConfigWithoutSurveys)));
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2377
|
+
if (this.disableSurveys === false) {
|
|
2378
|
+
const surveys = response.surveys;
|
|
2379
|
+
let hasSurveys = true;
|
|
2380
|
+
if (!Array.isArray(surveys)) {
|
|
2381
|
+
// If surveys is not an array, it means there are no surveys (its a boolean instead)
|
|
2382
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', 'There are no surveys.'));
|
|
2383
|
+
hasSurveys = false;
|
|
2384
|
+
}
|
|
2385
|
+
else {
|
|
2386
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', 'Surveys fetched from remote config: ', JSON.stringify(surveys)));
|
|
2387
|
+
}
|
|
2388
|
+
if (hasSurveys) {
|
|
2389
|
+
this.setPersistedProperty(PostHogPersistedProperty.Surveys, surveys);
|
|
2390
|
+
}
|
|
2391
|
+
else {
|
|
2392
|
+
this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
|
|
2393
|
+
}
|
|
2344
2394
|
}
|
|
2345
2395
|
else {
|
|
2346
2396
|
this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
|
|
2347
2397
|
}
|
|
2348
2398
|
// we cache the surveys in its own storage key
|
|
2349
2399
|
this.setPersistedProperty(PostHogPersistedProperty.RemoteConfig, remoteConfigWithoutSurveys);
|
|
2350
|
-
this.cacheSessionReplay(response);
|
|
2400
|
+
this.cacheSessionReplay('remote config', response);
|
|
2351
2401
|
// we only dont load flags if the remote config has no feature flags
|
|
2352
2402
|
if (response.hasFeatureFlags === false) {
|
|
2353
2403
|
// resetting flags to empty object
|
|
@@ -2404,7 +2454,7 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2404
2454
|
this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
|
|
2405
2455
|
// Mark that we hit the /decide endpoint so we can capture this in the $feature_flag_called event
|
|
2406
2456
|
this.setPersistedProperty(PostHogPersistedProperty.DecideEndpointWasHit, true);
|
|
2407
|
-
this.cacheSessionReplay(res);
|
|
2457
|
+
this.cacheSessionReplay('decide/flags', res);
|
|
2408
2458
|
}
|
|
2409
2459
|
return res;
|
|
2410
2460
|
})
|
|
@@ -2490,14 +2540,14 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2490
2540
|
this.capture('$feature_flag_called', {
|
|
2491
2541
|
$feature_flag: key,
|
|
2492
2542
|
$feature_flag_response: response,
|
|
2493
|
-
$feature_flag_id
|
|
2494
|
-
$feature_flag_version
|
|
2495
|
-
$feature_flag_reason
|
|
2496
|
-
$feature_flag_bootstrapped_response
|
|
2497
|
-
$feature_flag_bootstrapped_payload
|
|
2543
|
+
...maybeAdd('$feature_flag_id', featureFlag?.metadata?.id),
|
|
2544
|
+
...maybeAdd('$feature_flag_version', featureFlag?.metadata?.version),
|
|
2545
|
+
...maybeAdd('$feature_flag_reason', featureFlag?.reason?.description ?? featureFlag?.reason?.code),
|
|
2546
|
+
...maybeAdd('$feature_flag_bootstrapped_response', bootstrappedResponse),
|
|
2547
|
+
...maybeAdd('$feature_flag_bootstrapped_payload', bootstrappedPayload),
|
|
2498
2548
|
// If we haven't yet received a response from the /decide endpoint, we must have used the bootstrapped value
|
|
2499
2549
|
$used_bootstrap_value: !this.getPersistedProperty(PostHogPersistedProperty.DecideEndpointWasHit),
|
|
2500
|
-
$feature_flag_request_id
|
|
2550
|
+
...maybeAdd('$feature_flag_request_id', details.requestId),
|
|
2501
2551
|
});
|
|
2502
2552
|
}
|
|
2503
2553
|
// If we have flags we either return the value (true or string) or false
|
|
@@ -2571,7 +2621,7 @@ class PostHogCore extends PostHogCoreStateless {
|
|
|
2571
2621
|
.catch((e) => {
|
|
2572
2622
|
cb?.(e, undefined);
|
|
2573
2623
|
if (!cb) {
|
|
2574
|
-
this.logMsgIfDebug(() => console.log('
|
|
2624
|
+
this.logMsgIfDebug(() => console.log('PostHog Debug', 'Error reloading feature flags', e));
|
|
2575
2625
|
}
|
|
2576
2626
|
});
|
|
2577
2627
|
}
|