22
33function sendWebVitals ( ) {
44
5- function getLoafAttribution ( attribution ) {
6- const loafEntriesLength = attribution ?. longAnimationFrameEntries ?. length || 0 ;
7- if ( loafEntriesLength === 0 ) {
8- return { } ;
9- }
5+ function sendWebVitalsGAEvents ( { name, delta, value, id, attribution, navigationType} ) {
106
11- // The last LoAF entry is usually the most relevant.
12- const loaf = attribution . longAnimationFrameEntries . at ( - 1 ) ;
13- const loafEndTime = loaf . startTime + loaf . duration ;
7+ let overrides = { } ;
148
15- let totalReflow = null ;
16- if ( loaf . scripts . length ) {
17- totalReflow = loaf . scripts . reduce ( ( sum , script ) => {
18- return sum + script . forcedStyleAndLayoutDuration ;
19- } , 0 ) ;
9+ function roundIfNotNull ( x ) {
10+ return x != null ? Math . round ( x ) : null ;
2011 }
2112
22- const loafAttribution = {
23- // Stats for the LoAF entry itself.
24- debug_loaf_entry_start_time : loaf . startTime ,
25- debug_loaf_entry_end_time : loafEndTime ,
26- debug_loaf_entry_work_duration : loaf . renderStart ? loaf . renderStart - loaf . startTime : loaf . duration ,
27- debug_loaf_entry_render_duration : loaf . renderStart ? loafEndTime - loaf . renderStart : 0 ,
28- debug_loaf_entry_total_forced_style_and_layout_duration : totalReflow ,
29- debug_loaf_entry_pre_layout_duration : loaf . styleAndLayoutStart ? loaf . styleAndLayoutStart - loaf . renderStart : 0 ,
30- debug_loaf_entry_style_and_layout_duration : loaf . styleAndLayoutStart ? loafEndTime - loaf . styleAndLayoutStart : 0 ,
31-
32- // LoAF metadata.
33- debug_loaf_meta_length : loafEntriesLength ,
34- debug_loaf_meta_script_length : loaf . scripts . length ,
35- } ;
36-
37- // Stats for the slowest script in the LoAF entry.
38- let scriptAttribution = { } ;
39- let slowestScriptDuration = 0 ;
40- loaf . scripts . forEach ( script => {
41- if ( script . duration <= slowestScriptDuration ) {
42- return { } ;
43- }
44-
45- slowestScriptDuration = script . duration ;
46- scriptAttribution = {
47- debug_loaf_script_total_duration : script . duration ,
48- debug_loaf_script_compile_duration : script . executionStart - script . startTime ,
49- debug_loaf_script_exec_duration : script . startTime + script . duration - script . executionStart ,
50- debug_loaf_script_forced_style_and_layout_duration : script . forcedStyleAndLayoutDuration ,
51- debug_loaf_script_type : script . invokerType ,
52- debug_loaf_script_invoker : script . invoker ,
53- debug_loaf_script_source_url : script . sourceURL ,
54- debug_loaf_script_source_function_name : script . sourceFunctionName ,
55- debug_loaf_script_source_char_position : script . sourceCharPosition ,
56- }
57- } ) ;
58-
59- // The LoAF script with the single longest total duration.
60- return Object . fromEntries ( Object . entries ( { ...loafAttribution , ...scriptAttribution } ) . map ( ( [ k , v ] ) => {
61- // Convert all floats to ints.
62- return [ k , typeof v == 'number' ? Math . floor ( v ) : v ] ;
63- } ) ) ;
64- }
65-
66- function sendWebVitalsGAEvents ( { name, value, delta, id, attribution, navigationType} ) {
67- let overrides = { } ;
68-
6913 switch ( name ) {
7014 case 'CLS' :
7115 overrides = {
@@ -83,16 +27,23 @@ function sendWebVitals() {
8327 } ;
8428 break ;
8529 case 'INP' : {
86- const loafAttribution = getLoafAttribution ( attribution ) ;
8730 overrides = {
8831 debug_event : attribution . interactionType ,
89- debug_time : Math . round ( attribution . interactionTime ) ,
32+ debug_time : roundIfNotNull ( attribution . interactionTime ) ,
9033 debug_load_state : attribution . loadState ,
9134 debug_target : attribution . interactionTarget || '(not set)' ,
92- debug_interaction_delay : Math . round ( attribution . inputDelay ) ,
93- debug_processing_duration : Math . round ( attribution . processingDuration ) ,
94- debug_presentation_delay : Math . round ( attribution . presentationDelay ) ,
95- ...loafAttribution
35+ debug_interaction_delay : roundIfNotNull ( attribution . inputDelay ) ,
36+ debug_processing_duration : roundIfNotNull ( attribution . processingDuration ) ,
37+ debug_presentation_delay : roundIfNotNull ( attribution . presentationDelay ) ,
38+ debug_totalPaintDuration : roundIfNotNull ( attribution . totalPaintDuration ) ,
39+ debug_totalScriptDuration : roundIfNotNull ( attribution . totalScriptDuration ) ,
40+ debug_totalStyleAndLayoutDuration : roundIfNotNull ( attribution . totalStyleAndLayoutDuration ) ,
41+ debug_totalUnattributedDuration : roundIfNotNull ( attribution . totalUnattributedDuration ) ,
42+ debug_longestScriptIntersectingDuration : roundIfNotNull ( attribution . longestScript ?. intersectingDuration ) ,
43+ debug_longestScriptSubPart : attribution . longestScript ?. subpart || null ,
44+ debug_longestScriptInvoker : attribution . longestScript ?. entry . invoker || null ,
45+ debug_longestScriptInvokerType : attribution . longestScript ?. entry . invokerType || null ,
46+ debug_longestScriptName : attribution . longestScript ?. entry . name || null ,
9647 } ;
9748 break ;
9849 }
@@ -103,7 +54,7 @@ function sendWebVitals() {
10354 debug_resource_load_delay : attribution . resourceLoadDelay ,
10455 debug_resource_load_time : attribution . resourceLoadTime ,
10556 debug_element_render_delay : attribution . elementRenderDelay ,
106- debug_target : attribution . element || '(not set)' ,
57+ debug_target : attribution . target || '(not set)' ,
10758 debug_scroll_y : scrollY ,
10859 } ;
10960 break ;
@@ -146,12 +97,11 @@ function sendWebVitals() {
14697 event_value : Math . round ( name === 'CLS' ? delta * 1000 : delta ) ,
14798 event_label : id ,
14899 // Repeat with new fields to match web-vitals documentation
149- // TODO deprecate above UA-style names
100+ // TODO deprecate above names when no longer required
150101 metric_value : Math . round ( name === 'CLS' ? value * 1000 : value ) ,
151102 metric_delta : Math . round ( name === 'CLS' ? delta * 1000 : delta ) ,
152103 metric_id : id ,
153- nonInteraction : true ,
154-
104+ non_interaction : true ,
155105 effective_type : effectiveType ,
156106 data_saver : dataSaver ,
157107 device_memory : deviceMemory ,
0 commit comments