Developer pages

Nuxt

Install HumanBehavior in Nuxt with a browser-only plugin and public runtime config.

Nuxt runs code on both server and client, so HumanBehavior belongs in a .client.ts plugin. The plugin reads Nuxt public runtime config and never imports the SDK into the server renderer.

Wizard detection

The wizard detects Nuxt when it sees:

SignalDetails
nuxt dependencyVersion read from package.json
nuxt.config.js / .ts / .mjs / .cjsWorks even when dependency parsing is incomplete

Nuxt is checked before standalone Vue, so Nuxt projects do not fall through to the Vue guide.

Package

pnpm add humanbehavior-js

Environment variables

NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY=your-api-key
NUXT_PUBLIC_HUMANBEHAVIOR_INGESTION_URL=https://ingest.humanbehavior.co

Nuxt exposes NUXT_PUBLIC_* values to runtimeConfig.public. Non-public env vars are server-only and cannot initialize the browser SDK.

Wire runtime config

In nuxt.config.ts:

export default defineNuxtConfig({
  runtimeConfig: {
    public: {
      humanbehaviorApiKey: process.env.NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY,
      humanbehaviorIngestionUrl:
        process.env.NUXT_PUBLIC_HUMANBEHAVIOR_INGESTION_URL,
    },
  },
});

Init in plugins/humanbehavior.client.ts

import { HumanBehaviorTracker } from "humanbehavior-js";

export default defineNuxtPlugin(() => {
  const config = useRuntimeConfig();
  const apiKey = config.public.humanbehaviorApiKey as string | undefined;
  if (!apiKey) return;

  HumanBehaviorTracker.init(apiKey, {
    ingestionUrl: config.public.humanbehaviorIngestionUrl as string | undefined,
  });
});

The .client.ts suffix is the important part. It tells Nuxt to run this plugin only in the browser.

Why not import.meta.server in a universal plugin?

An import.meta.server guard can work, but the wizard reference project uses .client.ts because it is harder to accidentally regress:

PatternStatus
plugins/humanbehavior.client.tsRecommended
Universal plugin with if (import.meta.server) returnAcceptable but easier to break
Init in nuxt.config.tsWrong: config runs outside the browser
Init in server middlewareWrong

Nuxt app placement

You do not need to edit layouts or wrap <NuxtPage />. Nuxt auto-registers plugins under plugins/.

Expected files:

nuxt.config.ts
plugins/
  humanbehavior.client.ts

Options to consider

HumanBehaviorTracker.init(apiKey, {
  ingestionUrl: config.public.humanbehaviorIngestionUrl as string | undefined,
  environment: config.public.environment as string | undefined,
  release: config.public.release as string | undefined,
  redactionStrategy: { mode: "visibility-first" },
});

Only put values in runtimeConfig.public when they are safe to expose to browser code. Project API keys are browser credentials; private server secrets are not.

Nuxt router navigations use browser history. The SDK's navigation capture handles ordinary route changes automatically.

If your app uses custom navigation that does not touch history, retain the tracker and call trackPageView() after route changes:

export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig();
  const apiKey = config.public.humanbehaviorApiKey as string | undefined;
  if (!apiKey) return;

  const tracker = HumanBehaviorTracker.init(apiKey);
  nuxtApp.hook("page:finish", () => {
    void tracker.trackPageView();
  });
});

Most Nuxt apps do not need that hook.

Common pitfalls

PitfallSymptomFix
Using VITE_ vars in NuxtConfig value missingUse NUXT_PUBLIC_*
Reading process.env directly in pluginValue missing in client bundleMap through runtimeConfig.public
File named humanbehavior.ts instead of .client.tsSSR import/runtime issuesRename plugin to .client.ts
Initializing in a componentDuplicate init on remountUse one plugin
Local ingestion points at dashboardNetwork failuresUse actual ingestion host or reverse proxy

Verify

  1. Restart Nuxt after env changes.
  2. Open the site in a browser.
  3. Confirm /api/ingestion/events and /heartbeat return 2xx (init is not required).
  4. Navigate between routes.
  5. Check Replays for a session and Dashboard for page activity.

Troubleshooting checklist: Verify data is flowing.

What the wizard writes

The wizard's Nuxt framework config provides:

FieldValue
Integrationnuxt
Reference project keynuxt
Env varsNUXT_PUBLIC_HUMANBEHAVIOR_API_KEY, NUXT_PUBLIC_HUMANBEHAVIOR_INGESTION_URL
Project detection promptLook for nuxt dependency and/or nuxt.config.*

It installs the runtime package and prompts the agent toward the client plugin pattern above.