11import { PostHog } from '../../posthog-core'
2- import { PostHogConfig , RemoteConfig , SupportedWebVitalsMetrics } from '../../types'
2+ import { RemoteConfig , SupportedWebVitalsMetrics } from '../../types'
33import { createLogger } from '../../utils/logger'
44import { isBoolean , isNullish , isNumber , isUndefined , isObject } from '@posthog/core'
55import { WEB_VITALS_ALLOWED_METRICS , WEB_VITALS_ENABLED_SERVER_SIDE } from '../../constants'
66import { assignableWindow , window , location } from '../../utils/globals'
77import { maskQueryParams } from '../../utils/request-utils'
88import { PERSONAL_DATA_CAMPAIGN_PARAMS , MASKED } from '../../utils/event-utils'
9+ import { extendArray } from '../../utils'
910
1011const logger = createLogger ( '[Web Vitals]' )
1112
@@ -30,38 +31,36 @@ export class WebVitalsAutocapture {
3031 this . startIfEnabled ( )
3132 }
3233
33- /** Shorthand for performance config to reduce bundle size */
34- private get _perfConfig ( ) : PostHogConfig [ 'capture_performance' ] {
35- return this . _instance . config . capture_performance
36- }
37-
3834 public get allowedMetrics ( ) : SupportedWebVitalsMetrics [ ] {
39- const clientConfigMetricAllowList : SupportedWebVitalsMetrics [ ] | undefined = isObject ( this . _perfConfig )
40- ? this . _perfConfig ?. web_vitals_allowed_metrics
35+ const clientConfigMetricAllowList : SupportedWebVitalsMetrics [ ] | undefined = isObject (
36+ this . _instance . config . capture_performance
37+ )
38+ ? this . _instance . config . capture_performance ?. web_vitals_allowed_metrics
4139 : undefined
4240 return ! isNullish ( clientConfigMetricAllowList )
4341 ? clientConfigMetricAllowList
4442 : this . _instance . persistence ?. props [ WEB_VITALS_ALLOWED_METRICS ] || [ 'CLS' , 'FCP' , 'INP' , 'LCP' ]
4543 }
4644
4745 public get flushToCaptureTimeoutMs ( ) : number {
48- const clientConfig : number | undefined = isObject ( this . _perfConfig )
49- ? this . _perfConfig . web_vitals_delayed_flush_ms
46+ const clientConfig : number | undefined = isObject ( this . _instance . config . capture_performance )
47+ ? this . _instance . config . capture_performance . web_vitals_delayed_flush_ms
5048 : undefined
5149 return clientConfig || DEFAULT_FLUSH_TO_CAPTURE_TIMEOUT_MILLISECONDS
5250 }
5351
5452 public get useAttribution ( ) : boolean {
55- const clientConfig : boolean | undefined = isObject ( this . _perfConfig )
56- ? this . _perfConfig . web_vitals_attribution
53+ const clientConfig : boolean | undefined = isObject ( this . _instance . config . capture_performance )
54+ ? this . _instance . config . capture_performance . web_vitals_attribution
5755 : undefined
5856 return clientConfig ?? false
5957 }
6058
6159 public get _maxAllowedValue ( ) : number {
6260 const configured =
63- isObject ( this . _perfConfig ) && isNumber ( this . _perfConfig . __web_vitals_max_value )
64- ? this . _perfConfig . __web_vitals_max_value
61+ isObject ( this . _instance . config . capture_performance ) &&
62+ isNumber ( this . _instance . config . capture_performance . __web_vitals_max_value )
63+ ? this . _instance . config . capture_performance . __web_vitals_max_value
6564 : FIFTEEN_MINUTES_IN_MILLIS
6665 // you can set to 0 to disable the check or any value over ten seconds
6766 // 1 milli to 1 minute will be set to 15 minutes, cos that would be a silly low maximum
@@ -77,10 +76,10 @@ export class WebVitalsAutocapture {
7776 }
7877
7978 // Otherwise, check config
80- const clientConfig = isObject ( this . _perfConfig )
81- ? this . _perfConfig . web_vitals
82- : isBoolean ( this . _perfConfig )
83- ? this . _perfConfig
79+ const clientConfig = isObject ( this . _instance . config . capture_performance )
80+ ? this . _instance . config . capture_performance . web_vitals
81+ : isBoolean ( this . _instance . config . capture_performance )
82+ ? this . _instance . config . capture_performance
8483 : undefined
8584 return isBoolean ( clientConfig ) ? clientConfig : this . _enabledServerSide
8685 }
@@ -149,7 +148,7 @@ export class WebVitalsAutocapture {
149148 const customPersonalDataProperties = this . _instance . config . custom_personal_data_properties
150149
151150 const paramsToMask = maskPersonalDataProperties
152- ? [ ... PERSONAL_DATA_CAMPAIGN_PARAMS , ... ( customPersonalDataProperties || [ ] ) ]
151+ ? extendArray ( [ ] , PERSONAL_DATA_CAMPAIGN_PARAMS , customPersonalDataProperties || [ ] )
153152 : [ ]
154153
155154 return maskQueryParams ( href , paramsToMask , MASKED )
@@ -177,12 +176,6 @@ export class WebVitalsAutocapture {
177176 }
178177
179178 private _addToBuffer = ( metric : any ) => {
180- const sessionIds = this . _instance . sessionManager ?. checkAndGetSessionAndWindowId ( true )
181- if ( isUndefined ( sessionIds ) ) {
182- logger . error ( 'Could not read session ID. Dropping metrics!' )
183- return
184- }
185-
186179 this . _buffer = this . _buffer || { url : undefined , metrics : [ ] , firstMetricTimestamp : undefined }
187180
188181 const $currentUrl = this . _currentURL ( )
@@ -230,13 +223,18 @@ export class WebVitalsAutocapture {
230223 metric . attribution . interactionTargetElement = undefined
231224 }
232225
233- this . _buffer . metrics . push ( {
226+ const sessionIds = this . _instance . sessionManager ?. checkAndGetSessionAndWindowId ( true )
227+ const bufferedMetric : Record < string , unknown > = {
234228 ...metric ,
235229 $current_url : $currentUrl ,
236- $session_id : sessionIds . sessionId ,
237- $window_id : sessionIds . windowId ,
238230 timestamp : Date . now ( ) ,
239- } )
231+ }
232+ if ( ! isUndefined ( sessionIds ) ) {
233+ bufferedMetric . $session_id = sessionIds . sessionId
234+ bufferedMetric . $window_id = sessionIds . windowId
235+ }
236+
237+ this . _buffer . metrics . push ( bufferedMetric )
240238
241239 if ( this . _buffer . metrics . length === this . allowedMetrics . length ) {
242240 // we have all allowed metrics
0 commit comments