Disable Unwanted Default Analytics in Astro Starlight
Remove Starlight/Vercel analytics injection, run Plausible instead, and find the script that kept loading after I thought I removed it.
Tested on: Astro 5.7, Starlight 0.32 Problem: Two analytics scripts loading — one I didn't add, one I did.
Find what's loading
View source on production. I found:
@vercel/analytics(I never installed it — came from a template)- Starlight's optional analytics hook
- My Plausible script (the only one I wanted)
Remove Vercel analytics
Search codebase:
grep -r "vercel/analytics" .
Found in package.json and astro.config.mjs:
import vercel from '@astrojs/vercel/analytics';
// integrations: [vercel()]
Remove package and integration:
npm uninstall @vercel/analytics @astrojs/vercel
Starlight analytics config
astro.config.mjs before:
starlight({
title: 'My Docs',
// analytics default may inject if plugin present
})
Explicitly disable — no built-in analytics in Starlight core, but check head config:
starlight({
title: 'My Docs',
head: [
// remove any third-party script entries you didn't add
]
})
Add Plausible (privacy-friendly)
src/components/Analytics.astro:
---
const domain = import.meta.env.PUBLIC_PLAUSIBLE_DOMAIN;
const isProd = import.meta.env.PROD;
---
{isProd && domain && (
<script defer data-domain={domain} src="https://plausible.io/js/script.js"></script>
)}
Add to src/layouts/Layout.astro before </head>.
.env:
PUBLIC_PLAUSIBLE_DOMAIN=myblog.dev
Ghost script — @astrojs/sitemap confusion
Not analytics, but worth checking:
grep -r "script" astro.config.mjs
Sitemap integration doesn't inject JS. Good.
Verify clean build
npm run build
grep -r "googletagmanager\|vercel\|segment" dist/
# Should return nothing
CSP to block future accidents
public/_headers (Netlify) or adapter headers:
/*
Content-Security-Policy: script-src 'self' https://plausible.io; connect-src 'self' https://plausible.io
New scripts fail loudly in console instead of silently tracking.
Result
Scripts before: 3 (vercel + starlight remnant + plausible)
Scripts after: 1 (plausible)
Page weight: -18 KB
GDPR banner: not needed with Plausible