Free toolGenerate .mcp.json configs for Claude Code, Cursor & Codex →

Based on PostHog and Anthropic official docs, checked

How to add PostHog analytics with Claude Code

PostHog scores 5/5 on our Claude Code fit scale: it ships an official plugin with a hosted MCP server. PostHog tells you what people actually do in your app: which pages they visit, which buttons they click, where they drop off, and which feature flag they saw. It also records sessions, runs experiments, and tracks errors, all from the same events. This guide adds PostHog to a React app with Claude Code, using the official PostHog plugin, the hosted PostHog MCP server, and the posthog-js SDK. By the end you will see your own pageviews and clicks arriving live in your PostHog project.

  1. 1. Create a PostHog account and project

    Sign up at posthog.com and create a project. Then find your project token and host in your project settings: you will need both in step 5.

    Note which cloud region your project uses. US Cloud projects use https://us.i.posthog.com as the ingestion host, while EU Cloud projects use https://eu.i.posthog.com. Using the wrong host means events go nowhere, so copy the host from your own project settings rather than from this guide.

    Source: https://posthog.com/docs/libraries/react

  2. 2. Install the official PostHog plugin in Claude Code

    The PostHog plugin lives in Anthropic's official marketplace, claude-plugins-official, which Claude Code adds automatically the first time you start an interactive session. Install it with one command inside Claude Code.

    The plugin bundles the PostHog MCP server and a library of PostHog skills, so Claude can query your analytics, read errors, manage feature flags, and follow PostHog's own setup guides without you pasting docs into the chat.

    /plugin install posthog@claude-plugins-official

    Source: https://code.claude.com/docs/en/discover-plugins

  3. 3. Connect the PostHog MCP server

    The fastest way to connect is the PostHog wizard, which installs the MCP server directly into Claude Code, Cursor, Claude Desktop, Codex, VS Code, and Zed. Run it in your terminal and pick Claude Code when it asks.

    The wizard uses OAuth, so no API key is needed, and the MCP server routes you to the correct region (US or EU) based on the account you sign in with. The server URL is https://mcp.posthog.com/mcp. As with any MCP server, review tool calls before Claude executes them, since language models can be tricked by untrusted content.

    npx @posthog/wizard mcp add

    Source: https://posthog.com/docs/model-context-protocol

  4. 4. Install the SDK in your app

    If you are starting from a React app, the wizard can do this step too: run npx @posthog/wizard in your project directory and it detects your framework, installs the SDK, and sets up your first events.

    To install by hand instead, add posthog-js and @posthog/react with your package manager. This guide uses React; if you use Next.js, Remix, or React Router, follow PostHog's framework-specific guide for that setup instead of this one.

    npm install --save posthog-js@1 @posthog/react@1

    Source: https://posthog.com/docs/libraries/react

  5. 5. Add your keys and wrap your app

    Add your project token and host to a .env.local file, and to your hosting provider as well. If you use Vite, the VITE_ prefix exposes the values to the frontend.

    Then initialize PostHog at the root of your app, such as main.jsx for Vite apps, and wrap your app in the PostHogProvider so every component can use the usePostHog hook. Events captured before posthog.init() runs are dropped, so initialize first, before anything renders.

    # .env.local
    VITE_POSTHOG_PROJECT_TOKEN=YOUR_PROJECT_TOKEN
    VITE_POSTHOG_HOST=https://us.i.posthog.com
    
    // src/main.jsx
    import posthog from 'posthog-js';
    import { PostHogProvider } from '@posthog/react'
    
    posthog.init(import.meta.env.VITE_POSTHOG_PROJECT_TOKEN, {
      api_host: import.meta.env.VITE_POSTHOG_HOST,
      defaults: '2026-08-30',
    });
    
    <PostHogProvider client={posthog}>
      <App />
    </PostHogProvider>

    Source: https://posthog.com/docs/libraries/react

  6. 6. Capture and verify your first events

    PostHog autocaptures pageviews, clicks, and inputs by default, so your first events should arrive as soon as you load a page. For anything specific, capture a custom event with posthog.capture(), and identify logged-in users with posthog.identify() so their events link to one person.

    Verify in your PostHog project: open the Activity tab, where recent events update live, or use the toolbar event debugger, which shows events as they are captured on your site. If nothing arrives after a minute or two, check the troubleshooting section below.

    import { usePostHog } from '@posthog/react'
    
    const posthog = usePostHog()
    
    // After login, link events to a known user
    posthog?.identify(user.id, { email: user.email })
    
    // Fire a custom event on a button click
    posthog?.capture('button_clicked', { button: 'signup' })
    
    // On logout, clear the identity so the next user does not inherit it
    posthog?.reset()

    Source: https://posthog.com/docs/data/events

Feature flags in React

PostHog feature flags let you roll a feature out to some users and roll it back without a deploy. In React, read a flag with the useFeatureFlagEnabled hook from @posthog/react, and pass a default value as the second argument so you do not have to handle undefined while flags are still loading.

Use the hook's value to conditionally render the new UI. The same flags can be targeted at specific users or groups from the PostHog dashboard, so start with your own team, then widen the rollout once the flag behaves.

If events are not showing up

A silent PostHog install is usually one of three things. Work through them in order before reinstalling the SDK.

What else ships with the same install

The same SDK you just installed also powers session replay, feature flags, surveys, and error tracking, so you do not need a separate install for each one. PostHog's React docs describe the integration as enabling analytics about user behavior, custom event capture, session recordings, feature flags, and more, all from the one setup.

Turn features on one at a time and verify each in the dashboard before adding the next. Session replay shows you what the user saw before an error, feature flags let you gate a rollout, and surveys collect feedback in the product. If a feature needs extra configuration, such as a Content-Security-Policy allowance for replay bundles, PostHog's docs for that feature say so explicitly.

Ask Claude about your analytics

With the MCP server connected, you can ask Claude questions in plain text instead of building dashboards by hand. Try: "What are the top 5 errors in my project this week?" or "Show me the full stack trace for the most recent crash, then propose a fix." Claude reads the live data and answers from it.

The MCP server also exposes docs search and framework-specific setup resources, so you can ask Claude to check the current PostHog docs before it writes integration code. Keep the habit of reviewing what Claude changes, especially when it edits tracking code.

Tips

Next steps

Frequently asked questions

Use the host that matches your project region: https://us.i.posthog.com for US Cloud or https://eu.i.posthog.com for EU Cloud. Copy it from your own project settings, since the MCP server also routes you based on the account you sign in with.

Sources