references/EXAMPLE.md · 24000
ools: { enabled: true }, css: [resolve(__dirname, 'assets/css/main.css')], modules: ['@posthog/nuxt'], runtimeConfig: { public: { posthog: { publicKey: process.env.NUXT_PUBLIC_POSTHOG_PROJECT_TOKEN || '', host: process.env.NUXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', }, }, }, posthogConfig: { publicKey: process.env.NUXT_PUBLIC_POSTHOG_PROJECT_TOKEN || '', // Find it in project settings https://app.posthog.com/settings/project host: process.env.NUXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', // Optional: defaults to https://us.i.posthog.com. Use https://eu.i.posthog.com for EU region clientConfig: { capture_exceptions: true, // Enables automatic exception capture on the client side (Vue) __add_tracing_headers: [ 'localhost', 'yourdomain.com' ], // Add your domain here }, serverConfig: { enableExceptionAutocapture: true, // Enables automatic exception capture on the server side (Nitro) }, sourcemaps: { enabled: true, envId: process.env.PROJECT_ID || '', // Your project ID from PostHog settings https://app.posthog.com/settings/environment#variables personalApiKey: process.env.PERSONAL_API_KEY || '', // Your personal API key from PostHog settings https://app.posthog.com/settings/user-api-keys (requires organization:read and error_tracking:write scopes) project: 'my-application', // Optional: defaults to git repository name version: '1.0.0', // Optional: defaults to current git commit }, }, })
---
## public/robots.txt
```txt
User-Agent: *
Disallow:
server/api/auth/login.post.ts
import { useServerPostHog } from '../../utils/posthog'
import { getOrCreateUser, users } from '../../utils/users'
export default defineEventHandler(async (event) => {
const body = await readBody<{ username: string; password: string }>(event)
const { username, password } = body || {}
if (!username || !password) {
throw createError({
statusCode: 400,
message: 'Username and password required',
})
}
const user = getOrCreateUser(username)
const isNewUser = !users.has(username)
const sessionId = getHeader(event, 'x-posthog-session-id')
const distinctId = getHeader(event, 'x-posthog-distinct-id')
// Capture server-side login event
const posthog = useServerPostHog()
posthog.capture({
distinctId: distinctId,
event: 'server_login',
properties: {
$session_id: sessionId,
username: username,
isNewUser: isNewUser,
source: 'api',
},
})
return {
success: true,
user,
}
})
server/api/burrito/consider.post.ts
import { useServerPostHog } from '../../utils/posthog'
import { users, incrementBurritoConsiderations } from '../../utils/users'
import { defineEventHandler, readBody, createError, getHeader } from 'h3'
export default defineEventHandler(async (event) => {
const body = await readBody<{ username: string }>(event)
const username = body?.username
if (!username) {
throw createError({
statusCode: 400,
message: 'Username required',
})
}
if (!users.has(username)) {
throw createError({
statusCode: 404,
message: 'User not found',
})
}
// Increment burrito considerations (fake, in-memory)
const user = incrementBurritoConsiderations(username)
const sessionId = getHeader(event, 'x-posthog-session-id')
const distinctId = getHeader(event, 'x-posthog-distinct-id')
// Capture server-side burrito consideration event
const posthog = useServerPostHog()
posthog.capture({
distinctId: distinctId,
event: 'burrito_considered',
properties: {
$session_id: sessionId,
username: username,
total_considerations: user.burritoConsiderations,
source: 'api',
},
})
return {
success: true,
user: { ...user },
}
})
server/utils/posthog.ts
import { PostHog } from 'posthog-node'
let client: PostHog | null = null
export function useServerPostHog(): PostHog {
if (!client) {
const config = useRuntimeConfig()
// The @posthog/nuxt module exposes config at runtimeConfig.public.posthog
const posthogConfig = config.public.posthog
client = new PostHog(posthogConfig.publicKey, {
host: posthogConfig.host,
})
}
return client
}
server/utils/users.ts
// Shared in-memory storage for users (fake, no database)
export const users = new Map<string, { username: string; burritoConsiderations: number }>()
export function getOrCreateUser(username: string): { username: string; burritoConsiderations: number } {
let user = users.get(username)
if (!user) {
user = { username, burritoConsiderations: 0 }
users.set(username, user)
}
return user
}
export function incrementBurritoConsiderations(username: string): { username: string; burritoConsiderations: number } {
const user = users.get(username)
if (!user) {
throw new Error('User not found')
}
user.burritoConsiderations++
users.set(username, user)
return { ...user }
}