Replit integration

The PostHog MCP lets Replit Agent set up analytics, error tracking, and feature flags in your project automatically. Once connected, you can also query your data, investigate errors, and manage experiments - all through natural language.

Connect PostHog to Replit

  1. Add PostHog to Replit
  2. When prompted, log in to PostHog to authorize access
  3. The PostHog MCP server is now available in your Replit Agent
EU region users

If your PostHog project is on EU Cloud, use https://mcp-eu.posthog.com/mcp instead.

Set up PostHog in your project

Once connected, give Replit Agent this prompt to add PostHog with error tracking and source maps:

text
Add PostHog to this project for error tracking and analytics with source map uploads.
Use the PostHog MCP to:
- Get the project API token (from `projects-get`) → set as `VITE_POSTHOG_KEY`
- Get the host URL → set as `VITE_POSTHOG_HOST`
- Set `VITE_POSTHOG_KEY` and `VITE_POSTHOG_HOST` as shared environment variables so they work in both development and production
Install packages: posthog-js and @posthog/cli
Create a PostHog client at client/src/lib/posthog.ts that:
- Initializes with defaults: '2026-01-30', capture_exceptions: true, autocapture: true, capture_pageview: true
- Uses env vars VITE_POSTHOG_KEY and VITE_POSTHOG_HOST
- Exports a wrapper around posthog.captureException(error) - do NOT manually capture $exception events
Initialize PostHog in App.tsx with useEffect.
Add an error boundary component that uses posthog.captureException() to capture React errors.
Update `script/build.ts` to:
- Build with `sourcemap: true`
- After build, run `posthog-cli sourcemap inject` then `posthog-cli sourcemap upload`
- Delete `.map` files from dist after upload (keep them private)
- Skip upload gracefully if `POSTHOG_CLI_TOKEN` or `POSTHOG_CLI_ENV_ID` aren't set
For the source map upload secrets:
- Prompt me to provide POSTHOG_CLI_TOKEN as a secret
- Prompt me to provide POSTHOG_CLI_ENV_ID as a secret

The agent will get your project credentials from the MCP, set up shared environment variables, install the SDKs, add an error boundary, and configure source map uploads. It will prompt you to provide the CLI secrets needed for source map uploads.

For source maps to work in production, add these as Production app secrets (go to PublishAdjust settings):

What you can do with the MCP

Once set up, you can ask Replit Agent to interact with your PostHog data directly:

Query your analytics

Prompt: "How many unique users signed up in the last 7 days, broken down by day?"

The agent will run a trends query and return daily signup counts with a breakdown.

Prompt: "What's the conversion rate from signup to first purchase?"

The agent will build a funnel query and calculate the conversion percentage between those two events.

Investigate errors

Prompt: "What are the top 5 errors in my project this week?"

The agent will fetch error groups sorted by occurrence count, including how many users were affected.

Prompt: "Show me details about the most recent TypeError"

The agent will pull the stack trace, affected users, and browser/OS context for that error.

Manage feature flags

Prompt: "Create a feature flag called 'new-checkout' enabled for 10% of users"

The agent will create the flag with a 10% rollout and return the configuration with a link to the flag in PostHog.

Prompt: "Update the 'beta-features' flag to 50% rollout"

The agent will modify the existing flag and confirm the new rollout percentage.

Run experiments

Prompt: "Create an A/B test for the pricing page that measures conversion to checkout"

The agent will set up an experiment with control/test variants and configure a funnel metric measuring pricing page to checkout conversion.

See all available MCP tools for the complete list.

Troubleshooting

Source maps not uploading

Check that POSTHOG_CLI_TOKEN and POSTHOG_CLI_ENV_ID are set as secrets in Replit. If they're not set, the build will succeed but source maps won't be uploaded.

Events not appearing

Verify VITE_POSTHOG_KEY and VITE_POSTHOG_HOST are set correctly and that PostHog is initialized before your app renders.

MCP not connecting

Try disconnecting and reconnecting the PostHog MCP server. Make sure you're logged into the correct PostHog account.

Next steps

Have feedback or questions? Let us know on GitHub or ask a question.

Community questions

Was this page useful?

Questions about this page? or post a community question.