Installation
Install the plugin and get GA4 analytics, Search Console, and Lighthouse running in your Sanity Studio.
npm
Package Managers
npm
npm install sanity-plugin-analyticsyarn
yarn add sanity-plugin-analyticspnpm
pnpm add sanity-plugin-analyticsRequirements
Sanity Studiov3
Reactv18 or v19
Next.jsv13+ (App Router)
Peer Dependencies
The plugin requires the following peer dependencies. These are already included in a standard Sanity + Next.js project:
Install peer dependencies
npm install @sanity/icons sanity reactRegister the Plugin
Add the plugin to your sanity.config.ts (or .js) file:
sanity.config.ts
import { defineConfig } from 'sanity'
import { sanityAnalyticsPlugin } from 'sanity-plugin-analytics'
export default defineConfig({
name: 'your-project',
title: 'Your Project',
projectId: 'your-project-id',
dataset: 'production',
plugins: [
sanityAnalyticsPlugin({
analytics: {}, // GA4 dashboard
searchConsole: {}, // Search Console dashboard
lighthouse: { // Lighthouse dashboard
siteUrl: 'https://yoursite.com',
documentTypes: [{ type: 'post', slugField: 'slug', pathPrefix: '/blog' }],
},
}),
// ... other plugins
],
schema: {
types: [
// ... your schema types
],
},
})Install in Both Packages
If your project uses a monorepo structure, install the plugin in both your studio and web packages:
Monorepo installation
# In your studio package (for the Sanity Studio dashboard)
cd studio
npm install sanity-plugin-analytics
# In your frontend package (for the API route)
cd frontend
npm install sanity-plugin-analyticsNext steps: After installation, you need to set up Google Cloud and create the API route to complete the setup.