|
1 | | -import { CLSMetric } from "web-vitals"; |
2 | | - |
3 | | -const vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals"; |
4 | | - |
5 | | -function getConnectionSpeed() { |
6 | | - // This line makes all of my tools angry, and I can't find any type definitions for this, so I just got them to ignore it. |
7 | | - // prettier-ignore |
8 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
9 | | - // @ts-ignore |
10 | | - return "connection" in navigator && "effectiveType" in navigator["connection"] ? navigator["connection"]["effectiveType"] : ""; |
11 | | -} |
12 | | - |
13 | | -export function sendToVercelAnalytics(metric: CLSMetric) { |
14 | | - const analyticsId = import.meta.env.VITE_VERCEL_ANALYTICS_ID; |
15 | | - if (!analyticsId) { |
16 | | - return; |
17 | | - } |
18 | | - |
19 | | - const body = { |
20 | | - dsn: analyticsId, |
21 | | - id: metric.id, |
22 | | - page: window.location.pathname, |
23 | | - href: window.location.href, |
24 | | - event_name: metric.name, |
25 | | - value: metric.value.toString(), |
26 | | - speed: getConnectionSpeed(), |
27 | | - }; |
28 | | - |
29 | | - const blob = new Blob([new URLSearchParams(body).toString()], { |
30 | | - // This content type is necessary for `sendBeacon` |
31 | | - type: "application/x-www-form-urlencoded", |
32 | | - }); |
33 | | - if (navigator.sendBeacon) { |
34 | | - navigator.sendBeacon(vitalsUrl, blob); |
35 | | - } else |
36 | | - fetch(vitalsUrl, { |
37 | | - body: blob, |
38 | | - method: "POST", |
39 | | - credentials: "omit", |
40 | | - keepalive: true, |
41 | | - }); |
42 | | -} |
| 1 | +import { CLSMetric } from "web-vitals"; |
| 2 | + |
| 3 | +const vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals"; |
| 4 | + |
| 5 | +function getConnectionSpeed() { |
| 6 | + // This line makes all of my tools angry, and I can't find any type definitions for this, so I just got them to ignore it. |
| 7 | + // prettier-ignore |
| 8 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 9 | + // @ts-ignore |
| 10 | + return "connection" in navigator && "effectiveType" in navigator["connection"] ? navigator["connection"]["effectiveType"] : ""; |
| 11 | +} |
| 12 | + |
| 13 | +export function sendToVercelAnalytics(metric: CLSMetric) { |
| 14 | + const analyticsId = import.meta.env.VITE_VERCEL_ANALYTICS_ID; |
| 15 | + if (!analyticsId) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + const body = { |
| 20 | + dsn: analyticsId, |
| 21 | + id: metric.id, |
| 22 | + page: window.location.pathname, |
| 23 | + href: window.location.href, |
| 24 | + event_name: metric.name, |
| 25 | + value: metric.value.toString(), |
| 26 | + speed: getConnectionSpeed(), |
| 27 | + }; |
| 28 | + |
| 29 | + const blob = new Blob([new URLSearchParams(body).toString()], { |
| 30 | + // This content type is necessary for `sendBeacon` |
| 31 | + type: "application/x-www-form-urlencoded", |
| 32 | + }); |
| 33 | + if (navigator.sendBeacon) { |
| 34 | + navigator.sendBeacon(vitalsUrl, blob); |
| 35 | + } else |
| 36 | + fetch(vitalsUrl, { |
| 37 | + body: blob, |
| 38 | + method: "POST", |
| 39 | + credentials: "omit", |
| 40 | + keepalive: true, |
| 41 | + }); |
| 42 | +} |
0 commit comments