Getting Started with Vercel Web Analytics
This guide will help you get started with using Vercel Web Analytics on your project, showing you how to enable it, add the package to your project, deploy your app to Vercel, and view your data in the dashboard.
Select your framework to view instructions on using the Vercel Web Analytics in your project.
Prerequisites
Before you begin, ensure you have the following:
- A Vercel account - If you don't have one, you can sign up for free.
- A Vercel project - If you don't have one, you can create a new project.
- The Vercel CLI installed - If you don't have it, you can install it using one of the following commands:
- pnpm
- yarn
- npm
- bun
pnpm i vercel
yarn add vercel
npm i vercel
bun add vercel
Setup Steps
Step 1: Enable Web Analytics in Vercel
On the Vercel dashboard, select your Project and then click the Analytics tab and click Enable from the dialog.
Enabling Web Analytics will add new routes (scoped at /_vercel/insights/*) after your next deployment.
Step 2: Add @vercel/analytics to Your Project
The installation process varies by framework. HTML projects do not require package installation.
For most frameworks (Next.js, React, Remix, SvelteKit, Vue, Nuxt, Astro), install the package using your preferred package manager:
- pnpm
- yarn
- npm
- bun
pnpm i @vercel/analytics
yarn add @vercel/analytics
npm i @vercel/analytics
bun add @vercel/analytics
When using the HTML implementation, there is no need to install the @vercel/analytics package. Skip to the HTML integration section below.
Step 3: Integrate Web Analytics into Your Application
The integration method depends on your framework. Choose the appropriate section below:
Framework-Specific Integration
Next.js (Pages Router)
The Analytics component is a wrapper around the tracking script, offering more seamless integration with Next.js, including route support.
Add the component to your pages/_app.tsx or pages/_app.jsx file:
- TypeScript
- JavaScript
import type { AppProps } from 'next/app';
import { Analytics } from '@vercel/analytics/next';
function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
<Analytics />
</>
);
}
export default MyApp;
import { Analytics } from '@vercel/analytics/next';
function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Analytics />
</>
);
}
export default MyApp;
Next.js (App Router)
The Analytics component is a wrapper around the tracking script, offering more seamless integration with Next.js, including route support.
Add the component to your root layout:
- TypeScript
- JavaScript
import { Analytics } from '@vercel/analytics/next';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<head>
<title>Next.js</title>
</head>
<body>
{children}
<Analytics />
</body>
</html>
);
}
import { Analytics } from '@vercel/analytics/next';
export default function RootLayout({ children }) {
return (
<html lang="en">
<head>
<title>Next.js</title>
</head>
<body>
{children}
<Analytics />
</body>
</html>
);
}
Remix
The Analytics component is a wrapper around the tracking script, offering a seamless integration with Remix, including route detection.
Add the component to your root file:
- TypeScript
- JavaScript
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from '@remix-run/react';
import { Analytics } from '@vercel/analytics/remix';
export default function App() {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<Analytics />
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
}
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from '@remix-run/react';
import { Analytics } from '@vercel/analytics/remix';
export default function App() {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<Analytics />
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
}
Create React App
The Analytics component is a wrapper around the tracking script, offering more seamless integration with React.
When using the plain React implementation, there is no route support.
Add the component to your main app file:
- TypeScript
- JavaScript
import { Analytics } from '@vercel/analytics/react';
export default function App() {
return (
<div>
{/* Your app content */}
<Analytics />
</div>
);
}
import { Analytics } from '@vercel/analytics/react';
export default function App() {
return (
<div>
{/* Your app content */}
<Analytics />
</div>
);
}
SvelteKit
The injectAnalytics function is a wrapper around the tracking script, offering more seamless integration with SvelteKit, including route support.
Call the function in your main layout:
- TypeScript
- JavaScript
import { dev } from '$app/environment';
import { injectAnalytics } from '@vercel/analytics/sveltekit';
injectAnalytics({ mode: dev ? 'development' : 'production' });
import { dev } from '$app/environment';
import { injectAnalytics } from '@vercel/analytics/sveltekit';
injectAnalytics({ mode: dev ? 'development' : 'production' });
Vue
The Analytics component is a wrapper around the tracking script, offering more seamless integration with Vue.
Route support is automatically enabled if you're using vue-router.
Add the component to your main component:
- TypeScript
- JavaScript
<script setup lang="ts">
import { Analytics } from '@vercel/analytics/vue';
</script>
<template>
<Analytics />
<!-- your content -->
</template>
<script setup>
import { Analytics } from '@vercel/analytics/vue';
</script>
<template>
<Analytics />
<!-- your content -->
</template>
Nuxt
The Analytics component is a wrapper around the tracking script, offering more seamless integration with Nuxt, including route support.
Add the component to your main component:
- TypeScript
- JavaScript
<script setup lang="ts">
import { Analytics } from '@vercel/analytics/nuxt';
</script>
<template>
<Analytics />
<NuxtPage />
</template>
<script setup>
import { Analytics } from '@vercel/analytics/nuxt';
</script>
<template>
<Analytics />
<NuxtPage />
</template>
Astro
The Analytics component is a wrapper around the tracking script, offering more seamless integration with Astro, including route support.
Add the component to your base layout:
- TypeScript
- JavaScript
---
import Analytics from '@vercel/analytics/astro';
// ...
---
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- ... -->
<Analytics />
</head>
<body>
<slot />
</body>
</html>
---
import Analytics from '@vercel/analytics/astro';
// ...
---
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- ... -->
<Analytics />
</head>
<body>
<slot />
</body>
</html>
The Analytics component is available in version @vercel/analytics@1.4.0 and later. If you are using an earlier version, you must configure the webAnalytics property of the Vercel adapter in your astro.config.mjs file as shown below. For further information, see the Astro adapter documentation.
- TypeScript
- JavaScript
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
output: 'server',
adapter: vercel({
webAnalytics: {
enabled: true, // set to false when using @vercel/analytics@1.4.0
},
}),
});
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
output: 'server',
adapter: vercel({
webAnalytics: {
enabled: true, // set to false when using @vercel/analytics@1.4.0
},
}),
});
HTML (Plain Sites)
For plain HTML sites, you can add the following script to your .html files:
- TypeScript
- JavaScript
<script>
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
</script>
<script defer src="/_vercel/insights/script.js"></script>
<script>
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
</script>
<script defer src="/_vercel/insights/script.js"></script>
When using the HTML implementation, there is no need to install the @vercel/analytics package. However, there is no route support.
Other Frameworks
Import the inject function from the package, which will add the tracking script to your app. This should only be called once in your app, and must run in the client.
There is no route support with the inject function.
Add the following code to your main app file:
- TypeScript
- JavaScript
import { inject } from '@vercel/analytics';
inject();
import { inject } from '@vercel/analytics';
inject();
Step 4: Deploy Your App to Vercel
Deploy your app using the following command:
vercel deploy
If you haven't already, we also recommend connecting your project's Git repository, which will enable Vercel to deploy your latest commits to main without terminal commands.
Once your app is deployed, it will start tracking visitors and page views.
If everything is set up properly, you should be able to see a Fetch/XHR request in your browser's Network tab from /_vercel/insights/view when you visit any page.
Step 5: View Your Data in the Dashboard
Once your app is deployed, and users have visited your site, you can view your data in the dashboard.
To do so, go to your dashboard, select your project, and click the Analytics tab.
After a few days of visitors, you'll be able to start exploring your data by viewing and filtering the panels.
Users on Pro and Enterprise plans can also add custom events to their data to track user interactions such as button clicks, form submissions, or purchases.
Privacy and Compliance
Learn more about how Vercel supports privacy and data compliance standards with Vercel Web Analytics.
Next Steps
Now that you have Vercel Web Analytics set up, you can explore the following topics to learn more: