- 1
Installing PostHog Nuxt SDK
RequiredYour goal in this step: Install the PostHog Nuxt.js SDK.- Install
posthog-js
using your package manager:
npm install --save posthog-js- Add your PostHog API key and host to your
nuxt.config.js
file. You can find these in your project settings.
nuxt.config.jsexport 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-05-24',},}})- Create a new plugin by creating a new file
posthog.client.js
in your plugins directory.
plugins/posthog.client.jsimport { 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,},}}) - Install
Verify PostHog is initialized
CheckpointConfirm you can capture events with PostHogBefore proceeding, confirm that you can capture events using
posthog.capture('test_event')
.- 2
Manually capturing exceptions
RequiredYour goal in this step: Manually capture exceptions in your Nuxt application.To send errors directly using the PostHog client, import it and use the
captureException
method like this:Vue<script>const { $posthog } = useNuxtApp()if ($posthog) {const posthog = $posthog()posthog.captureException(new Error("Important error message"))}</script>On the server side, you can use the
posthog
object directly.server/api/example.jsconst runtimeConfig = useRuntimeConfig()const posthog = new PostHog(runtimeConfig.public.posthogPublicKey,{host: runtimeConfig.public.posthogHost,});try {const results = await DB.query.users.findMany()return results} catch (error) {posthog.captureException(error)}}) - 3
Configuring exception autocapture
RequiredYour goal in this step: Enable automatic exception tracking for your Nuxt application.Update your
posthog.client.js
to add an error hookJavaScriptexport default defineNuxtPlugin((nuxtApp) => {...nuxtApp.hook('vue:error', (error) => {posthogClient.captureException(error)})...}) - 5
Nuxt error tracking installation
Questions? Ask Max AI.
It's easier than reading through 698 pages of documentation
Community questions
Was this page useful?
Next article
SvelteKit error tracking installation
SvelteKit Hooks can be used to capture exceptions in the client and server-side. Capture exceptions in the handleError callback in your client-side hooks file: To capture exceptions on the server-side, you will also need to implement the handleError callback: