SDK reference

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:

bash
npx nuxi module add @databuddy/nuxt

Or install manually:

bash
bun add @databuddy/nuxt
# npm install @databuddy/nuxt
# yarn add @databuddy/nuxt
# pnpm add @databuddy/nuxt

Setup

Add your Client ID to nuxt.config.ts:

nuxt.config.tsts
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
  clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
},
})
.envbash
NUXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-id

The module tracks pageviews and route changes without any app.vue edits or plugins.

Nuxt automatically maps the NUXT_PUBLIC_DATABUDDY_CLIENT_ID environment variable to runtimeConfig.public.databuddy.clientId, so no extra configuration is needed.

Tracking events

useDatabuddy is auto-imported in every component:

pages/index.vuehtml
<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():

FunctionDescription
track(name, properties?)Track a custom event
trackError(error, properties?)Track an error, from an Error or a message
screenView(properties?)Manually trigger a pageview
setGlobalProperties(properties)Attach properties to all future events
identify(userId, traits?)Link this browser to a user ID from your system
reset()Forget the user and reset anonymous and session IDs (call after logout)
flush()Force-send all queued events immediately
getIds()Get { anonymousId, sessionId, profileId }
getTrackingParams()Get IDs as a URL query string for cross-domain tracking
optOut(), optIn()Stop or resume tracking this visitor; visitors with Global Privacy Control or Do Not Track stay untracked

Options API and templates

$databuddy is available directly in templates and Options API components with no import needed:

html
<template>
<button @click="$databuddy.track('cta_clicked')">Sign up</button>
</template>
ts
export default defineComponent({
methods: {
  handleClick() {
    this.$databuddy.track("cta_clicked")
  },
},
})

Feature flags

Add the flags config to enable the flag composables:

nuxt.config.tsts
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:

html
<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.

OptionTypeDefaultDescription
clientIdstring-Your Databuddy Client ID
disabledbooleanfalseDisable all tracking
debugbooleanfalseEnable verbose logging
autocaptureboolean | object{ interactions: true }What to record besides pageviews: webVitals, errors (JS errors + Vue component errors), outgoingLinks, interactions, attributes, hashChanges
maskPathsstring[][]Glob patterns to anonymise paths
skipPathsstring[][]Glob patterns to skip entirely
sampleRatenumber1Fraction of automatic pageview and page-exit events sent, from 0 to 1, decided per event; track() events are not sampled
flagsFlagsConfig-Feature flag configuration

The module also accepts the other browser options (batching, retries, replay, scriptUrl, ...) except filter, because runtime config can't hold functions.

Privacy

nuxt.config.tsts
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

nuxt.config.tsts
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
  clientId: process.env.NUXT_PUBLIC_DATABUDDY_CLIENT_ID,
  disabled: process.env.NODE_ENV !== "production",
},
})

How is this guide?