Next.js web analytics installation

  1. Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    npm install posthog-js
  2. Add environment variables

    Required

    Add your PostHog API key and host to your .env.local file and to your hosting provider (e.g. Vercel, Netlify). These values need to start with NEXT_PUBLIC_ to be accessible on the client-side.

    .env.local
    NEXT_PUBLIC_POSTHOG_KEY=<ph_project_api_key>
    NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
  3. Initialize PostHog

    Required

    Choose the integration method based on your Next.js version and router type.

    If you're using Next.js 15.3+, you can use instrumentation-client.ts for a lightweight, fast integration:

    instrumentation-client.ts
    import posthog from 'posthog-js'
    posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
    api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
    defaults: '2026-01-30'
    })
    Defaults option

    The defaults option automatically configures PostHog with recommended settings for new projects. See SDK defaults for details.

  4. Accessing PostHog on the client

    Recommended

    Once initialized in instrumentation-client.ts, import posthog from posthog-js anywhere and call the methods you need:

    app/checkout/page.tsx
    'use client'
    import posthog from 'posthog-js'
    export default function CheckoutPage() {
    function handlePurchase() {
    posthog.capture('purchase_completed', { amount: 99 })
    }
    return <button onClick={handlePurchase}>Complete purchase</button>
    }
  5. Server-side setup

    Optional

    To capture events from API routes or server actions, install posthog-node:

    npm install posthog-node

    Then, initialize PostHog in your API route or server action. Choose the method based on your router type:

    For the App router, you can use PostHog in API routes or server actions. Create a new PostHog client instance for each request, or reuse a singleton instance across requests:

    app/api/example/route.ts
    import { PostHog } from 'posthog-node'
    export async function POST(request: Request) {
    const posthog = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
    host: process.env.NEXT_PUBLIC_POSTHOG_HOST
    })
    posthog.capture({
    distinctId: 'distinct_id_of_the_user',
    event: 'event_name'
    })
    await posthog.shutdown()
    }

    You can also use PostHog in server actions:

    app/actions.ts
    'use server'
    import { PostHog } from 'posthog-node'
    export async function myServerAction() {
    const posthog = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
    host: process.env.NEXT_PUBLIC_POSTHOG_HOST
    })
    posthog.capture({
    distinctId: 'distinct_id_of_the_user',
    event: 'server_action_completed'
    })
    await posthog.shutdown()
    }
    Important

    Always call await posthog.shutdown() when you're done with the client to ensure all events are flushed before the request completes. For better performance, consider creating a singleton PostHog instance that you reuse across requests.

  6. Send events

    Recommended

    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' })
  7. Next steps

    Recommended

    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.