Skill 76 · Instrument Error Tracking
Subchapter 76.19
references/nuxt-3-6.mdMarkdown5 KBView on GitHub
AI agents: this is one page from PostHog’s docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt (opens in a new tab)
1
Required
Install the PostHog JavaScript library using your package manager:
npm install posthog-jsyarn add posthog-jspnpm add posthog-jsbun add posthog-jsNuxt version
This guide is for Nuxt v3.0 and above. For Nuxt v2.16 and below, see our Nuxt docs (opens in a new tab).
2
Required
Add your PostHog project token and host to your nuxt.config.js file:
nuxt.config.js
export default defineNuxtConfig({
runtimeConfig: {
public: {
posthogPublicKey: '<ph_project_token>',
posthogHost: 'https://us.i.posthog.com',
posthogDefaults: '2026-05-30'
}
}
})3
Required
Create a new plugin by creating a new file posthog.client.js in your plugins directory:
plugins/posthog.client.js
import { defineNuxtPlugin } from '#app'
import posthog from 'posthog-js'
export default defineNuxtPlugin(nuxtApp => {
const
4
Optional
To capture events from server routes, install posthog-node and instantiate it directly. You can also use it to evaluate feature flags on the server:
npm install posthog-node5
Click around and view a couple pages to generate some events. PostHog automatically captures pageviews, clicks, and other interactions for you.
If you’d like, you can also manually capture custom events:
JavaScript
posthog.capture('my_custom_event', { property: 'value' })6
Optional
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(
7
Recommended
Update your posthog.client.js to add an error hook.
JavaScript
export default defineNuxtPlugin((nuxtApp) => {
...
nuxtApp.hook('vue:error', (error) => {
posthogClient.captureException(error)
})
Recommended
Confirm events are being sent to PostHog
Before proceeding, let’s make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.

8
Required
Great, you’re capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.
Let’s continue to the next section.
Ask PostHog AI
HelpfulCould be better
yarn add posthog-nodepnpm add posthog-nodebun add posthog-nodeserver/api/example.js
import { PostHog } from 'posthog-node'
export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig()
const posthog = new PostHog(
runtimeConfig.public.posthogPublicKey,
{ host: runtimeConfig.public.posthogHost }
)
posthog.capture({
distinctId: 'distinct_id_of_the_user',
event: 'event_name'
})
await posthog.shutdown()
})On the server side, you can use the posthog object directly.
server/api/example.js
const 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)
}