VitePress
Integrate Swetrix with your VitePress documentation site to track page views, monitor errors, and capture custom events — all while staying privacy-friendly and GDPR-compliant.
Installation
There are two ways to add Swetrix to a VitePress site: via the config file (quickest) or via the npm package (more flexible, enables custom events from Vue components).
Option A: Script tag via config (quickest)
Add the Swetrix tracking script to your .vitepress/config.ts (or .js / .mts):
import { defineConfig } from 'vitepress'
export default defineConfig({
// ...your existing config
head: [
[
'script',
{ src: 'https://swetrix.org/swetrix.js', defer: '' },
],
[
'script',
{},
`document.addEventListener('DOMContentLoaded', function () {
swetrix.init('YOUR_PROJECT_ID')
swetrix.trackViews()
})`,
],
],
})
The empty string for defer is intentional — VitePress renders it as a boolean attribute (<script defer>).
Replace YOUR_PROJECT_ID with your actual Project ID from the Swetrix dashboard, otherwise tracking won't work.
Option B: npm package (recommended for custom events)
If you want to track custom events from Vue components or need finer control, install the npm package:
npm install swetrix
Then extend your VitePress theme to initialise Swetrix. Create or edit .vitepress/theme/index.ts:
import DefaultTheme from 'vitepress/theme'
import type { Theme } from 'vitepress'
import * as Swetrix from 'swetrix'
export default {
extends: DefaultTheme,
enhanceApp() {
if (typeof window === 'undefined') return
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
},
} satisfies Theme
trackViews() automatically detects client-side route changes, so you only need to call it once.