|
| 1 | +--- |
| 2 | +id: measuring-performance |
| 3 | +title: Measuring Performance |
| 4 | +--- |
| 5 | + |
| 6 | +By default, Create React App includes a performance relayer that allows you to measure and analyze |
| 7 | +the performance of your application using different metrics. |
| 8 | + |
| 9 | +To measure any of the supported metrics, you only need to pass a function into the `reportWebVitals` |
| 10 | +function in `index.js`: |
| 11 | + |
| 12 | +```js |
| 13 | +reportWebVitals(console.log); |
| 14 | +``` |
| 15 | + |
| 16 | +This function is fired when the final values for any of the metrics have finished calculating on the |
| 17 | +page. You can use it to log any of the results to the console or send to a particular endpoint. |
| 18 | + |
| 19 | +## Web Vitals |
| 20 | + |
| 21 | +[Web Vitals](https://web.dev/vitals/) are a set of useful metrics that aim to capture the user |
| 22 | +experience of a web page. In Create React App, a third-party library is used to measure these |
| 23 | +metrics ([web-vitals](https://github.com/GoogleChrome/web-vitals)). |
| 24 | + |
| 25 | +To understand more about the object returned to the function when a metric value is calculated, |
| 26 | +refer to the [documentation](https://github.com/GoogleChrome/web-vitals/#types). The [Browser |
| 27 | +Support](https://github.com/GoogleChrome/web-vitals/#browser-support) section also explains which browsers are supported. |
| 28 | + |
| 29 | +## Sending results to analytics |
| 30 | + |
| 31 | +With the `reportWebVitals` function, you can send any of results to an analytics endpoint to measure and track real user performance on your site. For example: |
| 32 | + |
| 33 | +```js |
| 34 | +function sendToAnalytics(metric) { |
| 35 | + const body = JSON.stringify(metric); |
| 36 | + const url = 'https://example.com/analytics'; |
| 37 | + |
| 38 | + // Use `navigator.sendBeacon()` if available, falling back to `fetch()` |
| 39 | + if (navigator.sendBeacon) { |
| 40 | + navigator.sendBeacon(url, body); |
| 41 | + } else { |
| 42 | + fetch(url, { body, method: 'POST', keepalive: true }); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +reportWebVitals(sendToAnalytics); |
| 47 | +``` |
| 48 | + |
| 49 | +> **Note:** If you use Google Analytics, use the `id` value to make it easier to construct metric distributions manually (to calculate percentiles, etc…). |
| 50 | +> |
| 51 | +> ```js |
| 52 | +> function sendToAnalytics({id, name, value}) { |
| 53 | +> ga('send', 'event', { |
| 54 | +> eventCategory: 'Web Vitals', |
| 55 | +> eventAction: name, |
| 56 | +> eventValue: Math.round(name === 'CLS' ? value * 1000 : value), // values must be integers |
| 57 | +> eventLabel: id, // id unique to current page load |
| 58 | +> nonInteraction: true, // avoids affecting bounce rate |
| 59 | +> }); |
| 60 | +> } |
| 61 | +> |
| 62 | +> reportWebVitals(sendToAnalytics); |
| 63 | +> ``` |
| 64 | +> |
| 65 | +> Read more about sending results to Google Analytics [here](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics). |
0 commit comments