@@ -22,10 +22,16 @@ import {
2222 OOB_TRACE_PAGE_LOAD_PREFIX ,
2323 FIRST_PAINT_COUNTER_NAME ,
2424 FIRST_CONTENTFUL_PAINT_COUNTER_NAME ,
25- FIRST_INPUT_DELAY_COUNTER_NAME
25+ FIRST_INPUT_DELAY_COUNTER_NAME ,
26+ LARGEST_CONTENTFUL_PAINT_METRIC_NAME ,
27+ LARGEST_CONTENTFUL_PAINT_ATTRIBUTE_NAME ,
28+ INTERACTION_TO_NEXT_PAINT_METRIC_NAME ,
29+ INTERACTION_TO_NEXT_PAINT_ATTRIBUTE_NAME ,
30+ CUMULATIVE_LAYOUT_SHIFT_METRIC_NAME ,
31+ CUMULATIVE_LAYOUT_SHIFT_ATTRIBUTE_NAME
2632} from '../constants' ;
2733import { Api } from '../services/api_service' ;
28- import { logTrace } from '../services/perf_logger' ;
34+ import { logTrace , flushLogs } from '../services/perf_logger' ;
2935import { ERROR_FACTORY , ErrorCode } from '../utils/errors' ;
3036import {
3137 isValidCustomAttributeName ,
@@ -37,6 +43,7 @@ import {
3743} from '../utils/metric_utils' ;
3844import { PerformanceTrace } from '../public_types' ;
3945import { PerformanceController } from '../controllers/perf' ;
46+ import { CoreVitalMetric , WebVitalMetrics } from './web_vitals' ;
4047
4148const enum TraceState {
4249 UNINITIALIZED = 1 ,
@@ -279,6 +286,7 @@ export class Trace implements PerformanceTrace {
279286 performanceController : PerformanceController ,
280287 navigationTimings : PerformanceNavigationTiming [ ] ,
281288 paintTimings : PerformanceEntry [ ] ,
289+ webVitalMetrics : WebVitalMetrics ,
282290 firstInputDelay ?: number
283291 ) : void {
284292 const route = Api . getInstance ( ) . getUrl ( ) ;
@@ -340,7 +348,43 @@ export class Trace implements PerformanceTrace {
340348 }
341349 }
342350
351+ this . addWebVitalMetric (
352+ trace ,
353+ LARGEST_CONTENTFUL_PAINT_METRIC_NAME ,
354+ LARGEST_CONTENTFUL_PAINT_ATTRIBUTE_NAME ,
355+ webVitalMetrics . lcp
356+ ) ;
357+ this . addWebVitalMetric (
358+ trace ,
359+ CUMULATIVE_LAYOUT_SHIFT_METRIC_NAME ,
360+ CUMULATIVE_LAYOUT_SHIFT_ATTRIBUTE_NAME ,
361+ webVitalMetrics . cls
362+ ) ;
363+ this . addWebVitalMetric (
364+ trace ,
365+ INTERACTION_TO_NEXT_PAINT_METRIC_NAME ,
366+ INTERACTION_TO_NEXT_PAINT_ATTRIBUTE_NAME ,
367+ webVitalMetrics . inp
368+ ) ;
369+
370+ // Page load logs are sent at unload time and so should be logged and
371+ // flushed immediately.
343372 logTrace ( trace ) ;
373+ flushLogs ( ) ;
374+ }
375+
376+ static addWebVitalMetric (
377+ trace : Trace ,
378+ metricKey : string ,
379+ attributeKey : string ,
380+ metric ?: CoreVitalMetric
381+ ) : void {
382+ if ( metric ) {
383+ trace . putMetric ( metricKey , Math . floor ( metric . value * 1000 ) ) ;
384+ if ( metric . elementAttribution ) {
385+ trace . putAttribute ( attributeKey , metric . elementAttribution ) ;
386+ }
387+ }
344388 }
345389
346390 static createUserTimingTrace (
0 commit comments