Set up HumanBehavior SDK with Nuxt.js applications
Show How to get your API Key
Example screenshot – your actual API key will be different.
npm install humanbehavior-js # or yarn add humanbehavior-js
.env
NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY=your-api-key-here
nuxt.config.ts
export default defineNuxtConfig({ runtimeConfig: { public: { humanBehaviorApiKey: process.env.NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY } } })
plugins/humanbehavior.client.ts
import { HumanBehaviorTracker } from 'humanbehavior-js'; export default defineNuxtPlugin(() => { const config = useRuntimeConfig(); const apiKey = config.public.humanBehaviorApiKey; const tracker = apiKey ? HumanBehaviorTracker.init(apiKey as string) : null; return { provide: { tracker: tracker as HumanBehaviorTracker | null } }; });
<template> <div> <h1>My Component</h1> <button @click="handleButtonClick">Click Me</button> </div> </template> <script setup> import { onMounted } from 'vue'; const { $tracker } = useNuxtApp(); onMounted(() => { // Track component view $tracker.customEvent('component_viewed', { componentName: 'MyComponent' }); }); async function handleButtonClick() { await $tracker.customEvent('button_clicked', { buttonLocation: 'header' }); } </script>
<template> <div> <form @submit.prevent="handleLogin"> <input v-model="email" type="email" placeholder="Email" /> <input v-model="password" type="password" placeholder="Password" /> <button type="submit">Login</button> </form> </div> </template> <script setup> import { ref } from 'vue'; const email = ref(''); const password = ref(''); const { $tracker } = useNuxtApp(); async function handleLogin() { try { // Your login logic here const user = await login(email.value, password.value); if (user) { // Identify user with current endUserId await $tracker.identifyUser({ userProperties: { email: user.email, name: user.name, userId: user.id, plan: user.plan } }); } } catch (error) { console.error('Login failed:', error); } } </script>
# .env NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY=your-api-key-here
export default defineNuxtConfig({ runtimeConfig: { public: { humanBehaviorApiKey: process.env.NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY } } });
// In your plugin const apiKey = config.public.humanBehaviorApiKey;