Overview
Vercel provides two analytics solutions for monitoring your Astro application:
- Web Analytics: Track page views, visitor data, and user behavior
- Speed Insights: Monitor Core Web Vitals and performance metrics
Both analytics solutions are privacy-friendly, GDPR-compliant, and don’t use cookies.
Installation
The boilerplate includes both analytics packages:
{
"dependencies": {
"@vercel/analytics": "^1.6.1",
"@vercel/speed-insights": "^1.3.1",
"web-vitals": "^5.1.0"
}
}
Install the packages if not already present:
npm install @vercel/analytics @vercel/speed-insights web-vitals
Configuration
Enable Web Analytics in your Astro config:
import vercel from '@astrojs/vercel'
import { defineConfig } from 'astro/config'
export default defineConfig({
adapter: vercel({
webAnalytics: {
enabled: true,
},
}),
})
Web Analytics is automatically enabled for all production deployments on Vercel.
Web Vitals Implementation
The boilerplate includes a custom Web Vitals script:
src/assets/scripts/web-vitals.js
import { webVitals } from '../../lib/vitals'
const analyticsId = import.meta.env.PUBLIC_VERCEL_ANALYTICS_ID
if (analyticsId) {
webVitals({
path: location.pathname,
params: location.search,
analyticsId,
})
}
Web Vitals Library
The vitals library tracks Core Web Vitals:
import { onCLS, onFCP, onFID, onLCP, onTTFB } from "web-vitals";
const vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals";
function getConnectionSpeed() {
return "connection" in navigator &&
navigator["connection"] &&
"effectiveType" in navigator["connection"]
? navigator["connection"]["effectiveType"]
: "";
}
export function sendToAnalytics(metric, options) {
const page = Object.entries(options.params).reduce(
(acc, [key, value]) => acc.replace(value, `[${key}]`),
options.path
);
const body = {
dsn: options.analyticsId,
id: metric.id,
page,
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(vitalsUrl, blob);
} else {
fetch(vitalsUrl, {
body: blob,
method: "POST",
credentials: "omit",
keepalive: true,
});
}
}
export function webVitals(options) {
try {
onFID((metric) => sendToAnalytics(metric, options));
onTTFB((metric) => sendToAnalytics(metric, options));
onLCP((metric) => sendToAnalytics(metric, options));
onCLS((metric) => sendToAnalytics(metric, options));
onFCP((metric) => sendToAnalytics(metric, options));
} catch (err) {
console.error("[Web Vitals]", err);
}
}
Core Web Vitals
The implementation tracks these key metrics:
Largest Contentful PaintMeasures loading performance. Occurs when the largest content element becomes visible.
- Good: < 2.5s
- Needs Improvement: 2.5s - 4s
- Poor: > 4s
First Input DelayMeasures interactivity. Time from when a user first interacts with your page to when the browser responds.
- Good: < 100ms
- Needs Improvement: 100ms - 300ms
- Poor: > 300ms
Cumulative Layout ShiftMeasures visual stability. Sum of all unexpected layout shifts during the page’s lifecycle.
- Good: < 0.1
- Needs Improvement: 0.1 - 0.25
- Poor: > 0.25
Time to First ByteMeasures server response time. Time from navigation start to when the browser receives the first byte.
- Good: < 800ms
- Needs Improvement: 800ms - 1800ms
- Poor: > 1800ms
First Contentful PaintMeasures perceived load speed. Time when the first content appears on screen.
- Good: < 1.8s
- Needs Improvement: 1.8s - 3s
- Poor: > 3s
Viewing Analytics
Access analytics data in your Vercel dashboard:
- Navigate to your project on Vercel
- Click the Analytics tab
- View metrics, page views, and visitor data
- Click Speed Insights for Core Web Vitals
Analytics data is available for all Vercel deployments, including preview deployments.
Custom Events
Track custom events in your application:
import { track } from '@vercel/analytics'
// Track a custom event
track('button_click', {
location: 'header',
button_name: 'signup',
})
// Track with user properties
track('purchase', {
product_id: '123',
price: 99.99,
currency: 'USD',
})
Environment Variables
The analytics implementation uses environment variables:
# Automatically provided by Vercel
PUBLIC_VERCEL_ANALYTICS_ID=your-analytics-id
Do not manually set PUBLIC_VERCEL_ANALYTICS_ID. Vercel automatically provides this in production.
Monitor performance across different pages and user segments:
// Track vitals for specific pages
webVitals({
path: '/checkout',
params: '?step=payment',
analyticsId,
})
Privacy & Compliance
Vercel Analytics is privacy-focused:
- No cookies used
- GDPR compliant
- No personal data collected
- Visitor data is anonymized
- No cross-site tracking
You don’t need a cookie consent banner for Vercel Analytics.
Best Practices
- Monitor Regularly: Check analytics weekly to identify trends
- Set Performance Budgets: Define acceptable thresholds for Core Web Vitals
- Test on Real Devices: Speed Insights shows real user metrics
- Optimize Based on Data: Focus on pages with poor vitals
- Track Key User Flows: Monitor critical paths like checkout or signup
Debugging
Enable debug mode to see vitals in the console:
webVitals({
path: location.pathname,
params: location.search,
analyticsId,
debug: true, // Log metrics to console
})
Next Steps
Image Optimization
Improve LCP with optimized images
Edge Functions
Reduce TTFB with edge execution