Skip to main content
Scripts

PostHog is an open-source product analytics platform with session replay, feature flags, and experiments. Nuxt Scripts loads PostHog's official posthog-js browser SDK from npm.

Use useScriptPostHog() to load the SDK and access the PostHog client.

PostHog

View source

Nuxt Config Setup

Add this to your nuxt.config.ts to load PostHog globally. Alternatively you can use the useScriptPostHog composable for more control.

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'phc_xxxxxxxxxxxxxxxxx',
        trigger: 'onNuxtReady',
      }
    }
  }
})

This config automatically enables first-party mode (proxy). See below to customise.

useScriptPostHog()

The useScriptPostHog composable lets you have fine-grain control over when and how PostHog is loaded on your site.

const { proxy } = useScriptPostHog()

proxy.posthog.capture('conversion', { value: 1 })

Please follow the Registry Scripts guide to learn more about advanced usage.

First-Party Mode: Privacy Focused Proxy

No extra config needed. Runtime requests are reverse-proxied through your server instead of going directly to PostHog. User IPs are anonymised and requests work with ad blockers. Learn more.

Mode
Bundle Proxy
Privacy
User IP addresses are anonymised. Other request data passes through.
export default defineNuxtConfig({
  scripts: {
    // ✅ First-party mode: proxied
    registry: {
      posthog: {
        apiKey: 'phc_xxxxxxxxxxxxxxxxx',
        trigger: 'onNuxtReady',
      },
    },
  },
})

Example

Using PostHog in a component with the proxy to send events .

<script setup lang="ts">
const { proxy } = useScriptPostHog()

// noop in development, ssr
// just works in production, client
function handleAction() {
  proxy.posthog.capture('conversion', { value: 1 })
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>

Installation

You must install the posthog-js dependency:

pnpm add posthog-js

EU Hosting

To use PostHog's EU cloud:

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'YOUR_API_KEY',
        region: 'eu'
      }
    }
  }
})

First-Party Proxy

When first-party mode is active (auto-enabled for scripts that support it), your server automatically proxies PostHog requests. PostHog recommends a reverse proxy for more reliable event capture because ad blockers can reject requests to known analytics domains. Nuxt anonymizes the client IP address to subnet level; other data passes through so features such as session replay and feature flags continue to work.

The module sets apiHost to your server's proxy endpoint:

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'YOUR_API_KEY',
        // apiHost is auto-set to '/_scripts/p/ph' (or '/_scripts/p/ph-eu' for EU region)
      }
    }
  }
})

The proxy handles API requests and static assets, including the session recording SDK.

Custom API Host

To use a custom reverse proxy or self-hosted PostHog instance, set apiHost directly:

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'YOUR_API_KEY',
        apiHost: '/my-proxy'
      }
    }
  }
})

The apiHost option accepts any URL or relative path, overriding both the region default and the first-party proxy auto-configuration. For additional PostHog SDK options like ui_host, use the config passthrough.

Feature Flags

Feature flag methods return values, so you need to wait for PostHog to load first:

const { onLoaded } = useScriptPostHog()

onLoaded(({ posthog }) => {
  // Check a feature flag
  if (posthog.isFeatureEnabled('new-dashboard')) {
    // Show new dashboard
  }

  // Get flag payload
  const payload = posthog.getFeatureFlagPayload('experiment-config')
})

PostHog exposes opt_in_capturing / opt_out_capturing. Set the boot-time default with defaultConsent and call consent.optIn() / consent.optOut() at runtime.

defaultConsent

ValueBehavior
'opt-in'Calls posthog.opt_in_capturing() immediately after init.
'opt-out'Calls posthog.init(..., { opt_out_capturing_by_default: true }) so the SDK boots opted out.

Use defaultConsent: 'opt-out' when you need the SDK to boot opted out. The runtime consent.optOut() calls opt_out_capturing() after init, which is weaker than the boot-time flag; any events captured between init and the opt-out call are still sent.

Example

<script setup lang="ts">
const { consent } = useScriptPostHog({
  apiKey: 'YOUR_API_KEY',
  defaultConsent: 'opt-out',
})

function onAccept() {
  consent.optIn()
}
function onRevoke() {
  consent.optOut()
}
</script>

Configuring PostHog globally in nuxt.config:

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'YOUR_API_KEY',
        defaultConsent: 'opt-out',
      }
    }
  }
})

Disabling Session Recording

export default defineNuxtConfig({
  scripts: {
    registry: {
      posthog: {
        apiKey: 'YOUR_API_KEY',
        disableSessionRecording: true
      }
    }
  }
})
apiKeystring required

Your PostHog project API key.

region'us' | 'eu' = 'us'

Your PostHog data region.

apiHoststring

Custom API host URL. Overrides the default derived from `region`. Useful for self-hosted instances or reverse proxies.

autocaptureboolean = true

Enable autocapture of clicks, form submissions, and page views.

capturePageviewboolean | 'history_change' = true

Capture page views automatically. Set to `'history_change'` to only capture on history changes.

capturePageleaveboolean = true

Capture page leave events automatically.

disableSessionRecordingboolean

Disable session recording.

configRecord<string, any>

Additional PostHog configuration options passed directly to `posthog.init()`.

defaultConsent'opt-in' | 'opt-out'

Default capture-consent state for PostHog. - `'opt-out'`: passed as `opt_out_capturing_by_default: true` to `posthog.init`, so capturing is suppressed from the first event. - `'opt-in'`: applied after `posthog.init` via `posthog.opt_in_capturing()` on the returned instance.