| name | posthog-multi-env-setup |
| description | Configure PostHog across development, staging, and production environments.
Separate PostHog projects per environment, environment-specific SDK config,
feature flag rollout per env, and session recording controls.
Trigger: "posthog environments", "posthog staging", "posthog dev prod",
"posthog environment setup", "posthog project per env".
|
| allowed-tools | Read, Write, Edit, Bash(aws:*), Bash(gcloud:*), Bash(vault:*) |
| version | 1.12.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","posthog","posthog-multi"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
PostHog Multi-Environment Setup
Overview
Use separate PostHog projects for each environment (dev, staging, production). This prevents dev/test events from polluting production analytics, allows different feature flag rollout percentages per environment, and lets you disable session recordings in non-production.
Prerequisites
- PostHog Cloud account or self-hosted instance
- Admin access to create multiple projects
- Deployment platform with environment variable support
Environment Strategy
| Environment | PostHog Project | Session Recording | Autocapture | Feature Flags |
|---|
| Development | myapp-dev | Disabled | Enabled | 100% rollout (test all) |
| Staging | myapp-staging | Disabled | Enabled | 100% rollout (QA all) |
| Production | myapp-prod | 10% sampled | Tuned | Gradual rollout |
Instructions
Step 1: Create Separate PostHog Projects
In PostHog Cloud (app.posthog.com), create three projects:
myapp-development — copy the phc_... project API key
myapp-staging — copy the phc_... project API key
myapp-production — copy the phc_... project API key
Step 2: Environment Variables
NEXT_PUBLIC_POSTHOG_KEY=phc_dev_key_here
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
POSTHOG_PERSONAL_API_KEY=phx_your_key
POSTHOG_PROJECT_ID=11111
NEXT_PUBLIC_POSTHOG_KEY=phc_staging_key_here
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
POSTHOG_PERSONAL_API_KEY=phx_your_key
POSTHOG_PROJECT_ID=22222
Step 3: Environment-Aware SDK Configuration
= | | ;
{
: ;
: ;
: ;
: ;
: | ;
: ;
}
(): {
env = (process.. || ) ;
key = process..;
host = process.. || ;
(!key) {
.();
}
: <, <, | >> = {
: {
: ,
: ,
: ,
: ,
},
: {
: ,
: ,
: ,
: ,
},
: {
: ,
: ,
: {
: [, ],
: [, , ],
: [],
},
: ,
},
};
{ : key || , host, ...configs[env] };
}
posthogConfig = ();