Vercel Analytics & Speed Insights Setup
Wire up Vercel Analytics, Speed Insights, and SPA routing rewrites into a React/Vite project in one pass — including the routing fix most people miss.
You add Vercel Analytics, check the dashboard a week later, and it's empty. The package installed fine — you just imported from the wrong entry point, and nothing told you.
Who it's for: React and Vite developers deploying to Vercel, indie hackers wanting traffic data without a heavier analytics stack, solo founders setting up a new SPA, developers debugging a 404-on-refresh bug in production
Example
"Set up Vercel Analytics for this Vite app" → @vercel/analytics and @vercel/speed-insights installed, both components mounted with the correct /react imports, a vercel.json rewrite rule added so direct route navigation stops 404ing, and a verification pass confirming both actually render
New here? 3-minute setup guide → | Already set up? Copy the template below.
# Vercel Analytics & Speed Insights Setup
## Your Role
Wire up Vercel Analytics and Speed Insights into a React/Vite project, and fix the SPA routing gap that causes direct-navigation 404s on Vercel. One-shot setup, not an ongoing workflow.
---
## Tasks
### 1. Install the packages
```bash
npm install @vercel/analytics @vercel/speed-insights
```
### 2. Mount the components
Import from the `/react` entry points — **not** `/next**, even if the project has some Next.js-adjacent tooling. Using the wrong entry point is the most common way this silently does nothing.
```tsx
import { Analytics } from '@vercel/analytics/react';
import { SpeedInsights } from '@vercel/speed-insights/react';
```
Render both inside the root component — typically `src/App.tsx` or `src/main.tsx`, wherever the app's top-level JSX lives:
```tsx
function App() {
return (
<>
{/* existing app content */}
<Analytics />
<SpeedInsights />
</>
);
}
```
### 3. Fix SPA routing for direct navigation
Vite/React SPAs use client-side routing. Without a rewrite rule, Vercel's server has no idea `/dashboard/settings` should serve `index.html` — a hard refresh or a shared link on any non-root route 404s.
Create `vercel.json` in the project root:
```json
{
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}
```
### 4. Verify
- Confirm `<Analytics />` and `<SpeedInsights />` actually render — check the mounted DOM or network tab for the Vercel script requests
- Deploy (or preview-deploy) and hit a nested route directly by URL, not by clicking through the app — this is the only way to catch a routing regression the dev server won't show you, since Vite's dev server already handles client-side routing correctly regardless of `vercel.json`
---
## Common Mistakes
**Wrong import path.** `/next` imports look almost identical to `/react` imports and won't error — they just don't collect anything. If analytics data never shows up in the Vercel dashboard, check this first.
**Skipping `vercel.json`.** Works perfectly in local dev (Vite handles routing itself), then 404s in production the moment someone refreshes on a non-root route or shares a deep link. This gap only shows up after deploying.
**Testing routing only via in-app navigation.** Clicking links inside the app never exercises the server-side routing path. Always test with a direct URL hit or a hard refresh.
---
## Scope
This is a five-minute setup task, not a monitoring workflow — it configures the two packages and the one routing fix, then stops. It doesn't cover custom event tracking, alerting, or dashboard configuration; those are separate, ongoing concerns outside what this task sets up.
What This Does
A one-shot setup task for React/Vite projects deploying to Vercel: installs Analytics and Speed Insights, mounts them correctly, and fixes the SPA routing gap that causes 404s when someone refreshes on a non-root route — a bug that only shows up in production, never in local dev.
Quick Start
Step 1: Navigate to Your Project
cd ~/your-vite-project
Step 2: Download the Template
Click Download above, then:
mv ~/Downloads/CLAUDE.md ./
Step 3: Run the Setup
claude
Then ask: "Set up Vercel Analytics and Speed Insights"
What Gets Configured
| Component | Purpose |
|---|---|
@vercel/analytics |
Page view and visitor tracking |
@vercel/speed-insights |
Core Web Vitals monitoring |
vercel.json rewrite rule |
Serves index.html for all routes, fixing direct-navigation 404s |
Tips & Best Practices
- Import from
/react, not/next. The two entry points look nearly identical and neither errors if you pick wrong — the wrong one just silently collects nothing. If the Vercel dashboard stays empty after a deploy, check this first. - Test routing with a direct URL hit, not in-app navigation. Vite's dev server already handles client-side routing correctly, so the missing-rewrite bug never shows up locally. Only a hard refresh or a shared link on a nested route — after deploying — will surface it.
- This is a five-minute task, not an ongoing workflow. It doesn't set up custom event tracking, alerting, or dashboard configuration — just the two packages and the routing fix.
Limitations
- Scoped to React/Vite projects specifically — Next.js apps use different Vercel Analytics import paths and don't need the
vercel.jsonrewrite (Next.js handles SPA-style routing server-side already) - Doesn't cover custom event tracking or alerting — only the base pageview/vitals setup