Nuxt
Add the module once, and pageviews and route changes are tracked with no app.vue changes.
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: {
autocapture: { webVitals: true, errors: true },
skipPaths: ["/admin/**"],
disabled: process.env.NODE_ENV !== "production",
},
});NUXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-idThe module reads the client ID from databuddy.clientId or the NUXT_PUBLIC_DATABUDDY_CLIENT_ID variable. autocapture.errors also captures Vue component errors.
Track events
<script setup lang="ts">
const { track } = useDatabuddy();
</script>
<template>
<button @click="track('signup_clicked', { source: 'hero' })">Get started</button>
</template>useDatabuddy() is auto-imported, and $databuddy works in templates and the Options API. Both return the tracker functions, such as identify, group, reset and optOut. They add screenView() to record a pageview yourself.
Identify users and their accounts
<script setup lang="ts">
const { identify, group, reset } = useDatabuddy();
const { user, workspace, isLoading } = useSession();
watchEffect(() => {
if (isLoading.value) return;
if (!user.value) return reset();
identify(user.value.id, { email: user.value.email });
group("workspace", workspace.value?.id ?? null);
});
</script>To target flags at the same user, call useFlags().updateUser({ userId, email, groups }).
Read feature flags
export default defineNuxtConfig({
modules: ["@databuddy/nuxt"],
databuddy: { flags: {} },
});<script setup lang="ts">
const { on: newCheckout, loading } = useFlag("new-checkout");
</script>
<template>
<CheckoutSkeleton v-if="loading" />
<NewCheckout v-else-if="newCheckout" />
<LegacyCheckout v-else />
</template>flags takes the Vue plugin's options and inherits the module's client ID. useFlag and useFlags are auto-imported.
Consent and event hooks
nuxt.config.ts takes every browser option except those that take a function: consent, beforeSend, filter and a cleanUrl function. Runtime config can't hold functions, so the build stops if one is set there. Set them in a client plugin that runs first.
export default defineNuxtPlugin({
enforce: "pre",
setup() {
window.databuddyConfig = {
...window.databuddyConfig,
consent: () => document.cookie.includes("analytics_consent=granted"),
beforeSend: (event) => event.type !== "outgoing_link",
};
},
});The grouped option names, such as autocapture, batching and skipPaths, need @databuddy/nuxt 3.1.0 or later. The flat names they replace still work.
Related
How is this guide?