summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/speed-insights.ts
blob: cd2ae7fe8d70526d0196e9685c670bc2b9983e54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { Metric } from 'web-vitals';
import { onCLS, onFCP, onFID, onLCP, onTTFB } from 'web-vitals';

const SPEED_INSIGHTS_INTAKE = 'https://vitals.vercel-analytics.com/v1/vitals';

type Options = { path: string; analyticsId: string };

const getConnectionSpeed = () => {
	return 'connection' in navigator &&
		navigator['connection'] &&
		'effectiveType' in (navigator['connection'] as unknown as { effectiveType: string })
		? (navigator['connection'] as unknown as { effectiveType: string })['effectiveType']
		: '';
};

const sendToSpeedInsights = (metric: Metric, options: Options) => {
	const body = {
		dsn: options.analyticsId,
		id: metric.id,
		page: options.path,
		href: location.href,
		event_name: metric.name,
		value: metric.value.toString(),
		speed: getConnectionSpeed(),
	};
	const blob = new Blob([new URLSearchParams(body).toString()], {
		type: 'application/x-www-form-urlencoded',
	});
	if (navigator.sendBeacon) {
		navigator.sendBeacon(SPEED_INSIGHTS_INTAKE, blob);
	} else
		fetch(SPEED_INSIGHTS_INTAKE, {
			body: blob,
			method: 'POST',
			credentials: 'omit',
			keepalive: true,
		});
};

function collectWebVitals() {
	const analyticsId = (import.meta as any).env.PUBLIC_VERCEL_ANALYTICS_ID;

	if (!analyticsId) {
		console.error('[Speed Insights] VERCEL_ANALYTICS_ID not found');
		return;
	}

	const options: Options = { path: window.location.pathname, analyticsId };

	try {
		onFID((metric) => sendToSpeedInsights(metric, options));
		onTTFB((metric) => sendToSpeedInsights(metric, options));
		onLCP((metric) => sendToSpeedInsights(metric, options));
		onCLS((metric) => sendToSpeedInsights(metric, options));
		onFCP((metric) => sendToSpeedInsights(metric, options));
	} catch (err) {
		console.error('[Speed Insights]', err);
	}
}

const mode = (import.meta as any).env.MODE as 'development' | 'production';

if (mode === 'production') {
	collectWebVitals();
}