2
2
3
3
function sendWebVitals ( ) {
4
4
5
- function getLoafAttribution ( attribution ) {
6
- const loafEntriesLength = attribution ?. longAnimationFrameEntries ?. length || 0 ;
7
- if ( loafEntriesLength === 0 ) {
8
- return { } ;
9
- }
10
-
11
- let loafAttribution = {
12
- debug_loaf_script_total_duration : 0
13
- } ;
14
-
15
- // The last LoAF entry is usually the most relevant.
16
- const loaf = attribution . longAnimationFrameEntries . at ( - 1 ) ;
17
- const loafEndTime = loaf . startTime + loaf . duration ;
18
- loaf . scripts . forEach ( script => {
19
- if ( script . duration <= loafAttribution . debug_loaf_script_total_duration ) {
20
- return ;
21
- }
22
- 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 : loaf . scripts . reduce ( ( sum , script ) => sum + script . forcedStyleAndLayoutDuration , 0 ) ,
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
- // Stats for the longest script in the LoAF entry.
33
- debug_loaf_script_total_duration : script . duration ,
34
- debug_loaf_script_compile_duration : script . executionStart - script . startTime ,
35
- debug_loaf_script_exec_duration : script . startTime + script . duration - script . executionStart ,
36
- debug_loaf_script_forced_style_and_layout_duration : script . forcedStyleAndLayoutDuration ,
37
- debug_loaf_script_type : script . invokerType ,
38
- debug_loaf_script_invoker : script . invoker ,
39
- debug_loaf_script_source_url : script . sourceURL ,
40
- debug_loaf_script_source_function_name : script . sourceFunctionName ,
41
- debug_loaf_script_source_char_position : script . sourceCharPosition ,
42
-
43
- // LoAF metadata.
44
- debug_loaf_meta_length : loafEntriesLength ,
45
- }
46
- } ) ;
47
-
48
- if ( ! loafAttribution . debug_loaf_script_total_duration ) {
49
- return { } ;
50
- }
51
-
52
- // The LoAF script with the single longest total duration.
53
- return Object . fromEntries ( Object . entries ( loafAttribution ) . map ( ( [ k , v ] ) => {
54
- // Convert all floats to ints.
55
- return [ k , typeof v == 'number' ? Math . floor ( v ) : v ] ;
56
- } ) ) ;
57
- }
58
-
59
5
function sendWebVitalsGAEvents ( { name, delta, value, id, attribution, navigationType} ) {
60
6
61
7
let overrides = { } ;
62
8
9
+ function roundIfNotNull ( x ) {
10
+ return x != null ? Math . round ( x ) : null ;
11
+ }
12
+
63
13
switch ( name ) {
64
14
case 'CLS' :
65
15
overrides = {
@@ -76,18 +26,24 @@ function sendWebVitals() {
76
26
debug_target : attribution . loadState || '(not set)' ,
77
27
} ;
78
28
break ;
79
- case 'FID' :
80
29
case 'INP' : {
81
- const loafAttribution = getLoafAttribution ( attribution ) ;
82
30
overrides = {
83
31
debug_event : attribution . interactionType ,
84
- debug_time : Math . round ( attribution . interactionTime ) ,
32
+ debug_time : roundIfNotNull ( attribution . interactionTime ) ,
85
33
debug_load_state : attribution . loadState ,
86
34
debug_target : attribution . interactionTarget || '(not set)' ,
87
- debug_interaction_delay : Math . round ( attribution . inputDelay ) ,
88
- debug_processing_duration : Math . round ( attribution . processingDuration ) ,
89
- debug_presentation_delay : Math . round ( attribution . presentationDelay ) ,
90
- ...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 ,
91
47
} ;
92
48
break ;
93
49
}
@@ -98,7 +54,7 @@ function sendWebVitals() {
98
54
debug_resource_load_delay : attribution . resourceLoadDelay ,
99
55
debug_resource_load_time : attribution . resourceLoadTime ,
100
56
debug_element_render_delay : attribution . elementRenderDelay ,
101
- debug_target : attribution . element || '(not set)' ,
57
+ debug_target : attribution . target || '(not set)' ,
102
58
} ;
103
59
break ;
104
60
case 'TTFB' :
@@ -163,7 +119,6 @@ function sendWebVitals() {
163
119
webVitals . onLCP ( sendWebVitalsGAEvents ) ;
164
120
webVitals . onCLS ( sendWebVitalsGAEvents ) ;
165
121
webVitals . onTTFB ( sendWebVitalsGAEvents ) ;
166
- webVitals . onFID ( sendWebVitalsGAEvents ) ;
167
122
webVitals . onINP ( sendWebVitalsGAEvents ) ;
168
123
} else {
169
124
console . error ( 'Web Vitals is not loaded!!' ) ;
0 commit comments