Nuxt
@databuddy/nuxt is a first-class Nuxt module. Add it to nuxt.config.ts once and you're done. Pageviews and SPA navigation work automatically with no app.vue changes. Set autocapture: { errors: true } to also capture JavaScript and Vue component errors.
Install
Run this in your project root. It installs the package and adds it to your nuxt.config.ts automatically:
npx nuxi module add @databuddy/nuxtOr install manually:
bun add @databuddy/nuxt
# npm install @databuddy/nuxt
# yarn add @databuddy/nuxt
# pnpm add @databuddy/nuxtSetup
Add your Client ID to nuxt.config.ts:
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
},
})NUXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-idThe module tracks pageviews and route changes without any app.vue edits or plugins.
Tracking events
useDatabuddy is auto-imported in every component:
<script setup>
const { track } = useDatabuddy()
function handleSignup() {
track("signup_clicked", {
source: "hero",
plan: "pro",
})
}
</script>
<template>
<button @click="handleSignup">Get started</button>
</template>Full composable API
All functions are available from useDatabuddy():
Options API and templates
$databuddy is available directly in templates and Options API components with no import needed:
<template>
<button @click="$databuddy.track('cta_clicked')">Sign up</button>
</template>export default defineComponent({
methods: {
handleClick() {
this.$databuddy.track("cta_clicked")
},
},
})Feature flags
Add the flags config to enable the flag composables:
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
flags: {}, // clientId is inherited automatically
},
})Then use useFlag and useFlags, which are auto-imported everywhere:
<script setup>
const { on: isNewCheckout, loading } = useFlag("new-checkout")
</script>
<template>
<template v-if="loading">
<CheckoutSkeleton />
</template>
<template v-else-if="isNewCheckout">
<NewCheckout />
</template>
<template v-else>
<LegacyCheckout />
</template>
</template>Module options
All options are passed under the databuddy key in nuxt.config.ts. The grouped names (autocapture, batching, retries, sampleRate, skipPaths, maskPaths) need @databuddy/nuxt 3.1.0 or later; the flat names they replace still work.
The module also accepts the other browser options (batching, retries, replay, scriptUrl, ...) except filter, because runtime config can't hold functions.
Privacy
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
// Never track admin or internal routes
skipPaths: ["/admin/**", "/internal/*"],
// Anonymise user IDs in paths
maskPaths: ["/users/*", "/orders/**"],
},
})Disable in development
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
disabled: process.env.NODE_ENV !== "production",
},
})Related
How is this guide?