Universal Analytics Engines Integration
npm install unplugin-analytics


Universal Analytics Engines Integration.
Support analytics engines:
- Umami
- Plausible
- Cloudflare Web Analytics
- Microsoft Clarity
``bash`
npm i -D unplugin-analytics
Vite
`ts
// vite.config.ts
import Analytics from 'unplugin-analytics/vite';
export default defineConfig({
plugins: [
Analytics({
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
})
]
});
`
Full example is located at examples/vite.
VitePress
`ts
// .vitepress/config.ts
import { defineConfig } from 'vitepress';
import { injectScriptTags } from 'unplugin-analytics/vitepress';
export default defineConfig({
async transformHead(context) {
// Add the following code
injectScriptTags({
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
})(context);
},
});
`
Astro
`ts
// astro.config.mjs
import Analytics from 'unplugin-analytics/astro';
export default defineConfig({
integrations: [
Analytics({
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
})
],
});
`
Then add the astro component made of injected scripts to your layouts.
`astro
---
// src/layouts/Layout.astro
import Analytics from '~analytics/scripts.astro'
// ...
---
To make the TypeScript work, you can add
unplugin-analytics/client to your corresponding tsconfig.json.`json5
{
"compilerOptions": {
// ...
"types": [
"unplugin-analytics/client"
],
},
// ...
}
`Or you can add TypeScript triple-slash directives to your
.d.ts (i.e. for projects initialized by Vite, it may be src/env.d.ts).`ts
// Your .d.ts file///
`Full example is located at examples/astro.
Nuxt
`ts
// nuxt.config.tsexport default defineNuxtConfig({
modules: ['unplugin-analytics/nuxt'],
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
});
`Full example is located at examples/nuxt.
Usage
Taking Vite as an example, you can config the analytics engines.
`ts
// vite.config.tsimport Analytics from 'unplugin-analytics/vite';
export default defineConfig({
plugins: [
Analytics({
analytics: {
umami: {
src:
...,
id: ...
},
plausible: {
domain: ...
},
cloudflare: {
beacon: ...
},
clarity: {
id: ...
}
}
})
]
});
`$3
Provider key:
umamiParameters:
-
src: Your umami script url or the host
- id: Your umami website idGenerated script:
`html
`$3
Provider key:
plausibleParameters:
-
src: Your plausible script
- id: Your website domainGenerated script:
`html
`$3
Provider key:
cloudflareParameters:
-
beacon: Your cloudflare web analytics beaconGenerated script:
`html
`$3
Provider key:
clarityParameters:
-
id: Your clarity project idGenerated script:
`html
``MIT License © 2024 XLor