Add analytics to Sanity Studio
How to inject analytics scripts like the Google tag (gtag.js) for Google Analytics into Sanity Studio, including Sanity-hosted deployments.
Sanity Studio is a React application, so you can't add <script> tags to its HTML <head> directly. This is true for both Sanity-hosted studios (*.sanity.studio) and self-hosted deployments. Instead, you can use a React component that injects the script at runtime when the studio loads.
This approach works with any analytics provider: Google Tag Manager, Google Analytics, Plausible, PostHog, Fathom, and others.
This guide shows you how to create an analytics component, mount it with the studio.components.layout override, and deploy your studio.
Create an analytics component
Create a component that uses useEffect to inject a script tag into the document <head> when it mounts. The following example uses the Google tag (gtag.js) for Google Analytics, but you can replace the script source and initialization logic with any provider.
import {useEffect} from 'react'
declare global {
interface Window {
dataLayer: unknown[]
}
}
export function Analytics() {
useEffect(() => {
const script = document.createElement('script')
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX'
script.async = true
document.head.appendChild(script)
script.onload = () => {
window.dataLayer = window.dataLayer || []
function gtag(..._args: unknown[]) {
// gtag.js expects the arguments object, not an array
window.dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', 'G-XXXXXXX')
}
return () => {
document.head.removeChild(script)
}
}, [])
return null
}Replace G-XXXXXXX with your actual measurement ID. The component renders nothing visible (return null) and cleans up the script tag when it unmounts.
Add the component to your studio layout
Use the studio.components.layout override in your studio configuration to mount the analytics component alongside the default layout.
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
import {schemaTypes} from './schemaTypes'
import {Analytics} from './components/Analytics'
export default defineConfig({
name: 'default',
title: 'My Studio',
projectId: '<your-project-id>',
dataset: 'production',
plugins: [structureTool()],
schema: {
types: schemaTypes,
},
studio: {
components: {
layout: (props) => (
<>
<Analytics />
{props.renderDefault(props)}
</>
),
},
},
})Because the layout component wraps the entire studio, <Analytics /> mounts once when the studio initializes, and the analytics script persists across all navigation within the studio.
JSX in config files
Because this configuration uses JSX, the file extension must be .tsx (or .jsx). If your config file currently uses .ts, rename it before adding the layout override.
Deploy
Deploy your studio as usual. If you use Sanity-hosted deployments, run npx sanity@latest deploy. The script injection happens in the browser at runtime, so no changes to the hosting configuration are needed.
Open the deployed studio and verify that your analytics provider is receiving events. For the Google tag (gtag.js), you can check Tag Assistant or the Google Analytics real-time reports to confirm the script is active.
Use environment variables
To avoid hardcoding your measurement ID, use an environment variable. Sanity Studio supports environment variables prefixed with SANITY_STUDIO_, which are embedded at build time.
import {useEffect} from 'react'
declare global {
interface Window {
dataLayer: unknown[]
}
}
const measurementId = process.env.SANITY_STUDIO_GTAG_ID
export function Analytics() {
useEffect(() => {
if (!measurementId) return
const script = document.createElement('script')
script.src = `https://www.googletagmanager.com/gtag/js?id=${measurementId}`
script.async = true
document.head.appendChild(script)
script.onload = () => {
window.dataLayer = window.dataLayer || []
function gtag(..._args: unknown[]) {
// gtag.js expects the arguments object, not an array
window.dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', measurementId)
}
return () => {
document.head.removeChild(script)
}
}, [])
return null
}Add the variable to your .env.production file:
SANITY_STUDIO_GTAG_ID=G-XXXXXXXWith this approach, the analytics script only loads when the environment variable is set. The sanity build and sanity deploy commands load .env.production, but sanity dev doesn't, so analytics stays off in local development and active in deployed environments.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?