|
1 | 1 | # Interactions |
2 | 2 |
|
3 | | -This script it's part of the [Web Vitals Chrome Extension](https://chrome.google.com/webstore/detail/web-vitals/ahfhijdlegdabablpippeagghigmibma) and allows you to track all interactions as you click around the page to help improve INP. |
| 3 | +This script is part of the [Web Vitals Chrome Extension](https://chrome.google.com/webstore/detail/web-vitals/ahfhijdlegdabablpippeagghigmibma) and allows you to track all interactions as you click around the page to help improve INP. |
4 | 4 |
|
5 | 5 |
|
6 | 6 | #### Snippet |
7 | 7 |
|
8 | 8 | ```js copy |
9 | | -const valueToRating = (score) => score <= 200 ? 'good' : score <= 500 ? 'needs-improvement' : 'poor'; |
| 9 | +const valueToRating = (score) => |
| 10 | + score <= 200 ? "good" : score <= 500 ? "needs-improvement" : "poor"; |
10 | 11 |
|
11 | | -const COLOR_GOOD = '#0CCE6A'; |
12 | | -const COLOR_NEEDS_IMPROVEMENT = '#FFA400'; |
13 | | -const COLOR_POOR = '#FF4E42'; |
| 12 | +const COLOR_GOOD = "#0CCE6A"; |
| 13 | +const COLOR_NEEDS_IMPROVEMENT = "#FFA400"; |
| 14 | +const COLOR_POOR = "#FF4E42"; |
14 | 15 | const RATING_COLORS = { |
15 | | - 'good': COLOR_GOOD, |
16 | | - 'needs-improvement': COLOR_NEEDS_IMPROVEMENT, |
17 | | - 'poor': COLOR_POOR |
| 16 | + good: COLOR_GOOD, |
| 17 | + "needs-improvement": COLOR_NEEDS_IMPROVEMENT, |
| 18 | + poor: COLOR_POOR, |
18 | 19 | }; |
19 | 20 |
|
20 | 21 | const observer = new PerformanceObserver((list) => { |
21 | 22 | const interactions = {}; |
22 | 23 |
|
23 | | - for (const entry of list.getEntries().filter((entry) => !entry.interactionId)) { |
| 24 | + for (const entry of list |
| 25 | + .getEntries() |
| 26 | + .filter((entry) => !entry.interactionId)) { |
24 | 27 | interactions[entry.interactionId] = interactions[entry.interactionId] || []; |
25 | 28 | interactions[entry.interactionId].push(entry); |
26 | 29 | } |
27 | 30 |
|
28 | 31 | // Will report as a single interaction even if parts are in separate frames. |
29 | 32 | // Consider splitting by animation frame. |
30 | 33 | for (const interaction of Object.values(interactions)) { |
31 | | - const entry = interaction.reduce((prev, curr) => prev.duration >= curr.duration ? prev : curr); |
| 34 | + const entry = interaction.reduce((prev, curr) => |
| 35 | + prev.duration >= curr.duration ? prev : curr, |
| 36 | + ); |
32 | 37 | const value = entry.duration; |
33 | 38 | const rating = valueToRating(value); |
34 | 39 |
|
35 | 40 | const formattedValue = `${value.toFixed(0)} ms`; |
36 | 41 | console.groupCollapsed( |
37 | 42 | `Interaction tracking snippet %c${formattedValue} (${rating})`, |
38 | | - `color: ${RATING_COLORS[rating] || 'inherit'}` |
| 43 | + `color: ${RATING_COLORS[rating] || "inherit"}`, |
39 | 44 | ); |
40 | | - console.log('Interaction target:', entry.target); |
| 45 | + console.log("Interaction target:", entry.target); |
41 | 46 |
|
42 | 47 | for (let entry of interaction) { |
43 | | - console.log(`Interaction event type: %c${entry.name}`, 'font-family: monospace'); |
| 48 | + console.log( |
| 49 | + `Interaction event type: %c${entry.name}`, |
| 50 | + "font-family: monospace", |
| 51 | + ); |
44 | 52 |
|
45 | 53 | // RenderTime is an estimate, because duration is rounded, and may get rounded down. |
46 | 54 | // In rare cases it can be less than processingEnd and that breaks performance.measure(). |
47 | 55 | // Lets make sure its at least 4ms in those cases so you can just barely see it. |
48 | | - const adjustedPresentationTime = Math.max(entry.processingEnd + 4, entry.startTime + entry.duration); |
49 | | - |
50 | | - console.table([{ |
51 | | - subPartString: 'Input delay', |
52 | | - 'Time (ms)': Math.round(entry.processingStart - entry.startTime, 0), |
53 | | - }, |
54 | | - { |
55 | | - subPartString: 'Processing time', |
56 | | - 'Time (ms)': Math.round(entry.processingEnd - entry.processingStart, 0), |
57 | | - }, |
58 | | - { |
59 | | - subPartString: 'Presentation delay', |
60 | | - 'Time (ms)': Math.round(adjustedPresentationTime - entry.processingEnd, 0), |
61 | | - }]); |
| 56 | + const adjustedPresentationTime = Math.max( |
| 57 | + entry.processingEnd + 4, |
| 58 | + entry.startTime + entry.duration, |
| 59 | + ); |
| 60 | + |
| 61 | + console.table([ |
| 62 | + { |
| 63 | + subPartString: "Input delay", |
| 64 | + "Time (ms)": Math.round(entry.processingStart - entry.startTime, 0), |
| 65 | + }, |
| 66 | + { |
| 67 | + subPartString: "Processing time", |
| 68 | + "Time (ms)": Math.round( |
| 69 | + entry.processingEnd - entry.processingStart, |
| 70 | + 0, |
| 71 | + ), |
| 72 | + }, |
| 73 | + { |
| 74 | + subPartString: "Presentation delay", |
| 75 | + "Time (ms)": Math.round( |
| 76 | + adjustedPresentationTime - entry.processingEnd, |
| 77 | + 0, |
| 78 | + ), |
| 79 | + }, |
| 80 | + ]); |
62 | 81 | } |
63 | 82 |
|
64 | 83 | console.groupEnd(); |
65 | | - |
66 | 84 | } |
67 | 85 | }); |
68 | 86 |
|
69 | 87 | observer.observe({ |
70 | | - type: 'event', |
| 88 | + type: "event", |
71 | 89 | durationThreshold: 0, // 16 minimum by spec |
72 | | - buffered: true |
| 90 | + buffered: true, |
73 | 91 | }); |
74 | 92 | ``` |
75 | | - |
|
0 commit comments