Nuxt web analytics installation

  1. Install posthog-js using your package manager:

npm install --save posthog-js

  1. Add your PostHog API key and host to your nuxt.config.js file. You can find these in your project settings.
nuxt.config.js
export default defineNuxtConfig({
runtimeConfig: {
public: {
posthogPublicKey: process.env.NUXT_PUBLIC_POSTHOG_KEY || '<ph_project_api_key>',
posthogHost: process.env.NUXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
posthogDefaults: '2025-11-30',
},
}
})
  1. Create a new plugin by creating a new file posthog.client.js in your plugins directory.
plugins/posthog.client.js
import { defineNuxtPlugin, useRuntimeConfig } from '#imports'
import posthog from 'posthog-js'
export default defineNuxtPlugin(() => {
const runtimeConfig = useRuntimeConfig()
const posthogClient = posthog.init(runtimeConfig.public.posthogPublicKey, {
api_host: runtimeConfig.public.posthogHost,
defaults: runtimeConfig.public.posthogDefaults,
loaded: (posthog) => {
if (import.meta.env.MODE === 'development') posthog.debug()
},
})
return {
provide: {
posthog: () => posthogClient,
},
}
})

Next steps

After installing PostHog and ensuring autocapture is enabled, head to your web analytics dashboard to see your data. And then check out our getting started guide.

PostHog tip: Web analytics works with anonymous events. This means if you are primarily using PostHog for web analytics, it can be significantly cheaper for you.

Community questions

Was this page useful?

Questions about this page? or post a community question.