Skip to content

Commit d3fa3f0

Browse files
authored
feat: add PostHog Vercel Marketplace integration support (vercel#1375)
## Summary Updates the PostHog Flags SDK example to support the Vercel Marketplace integration for PostHog. **Changes:** - Added `products` parameter to deploy button URL to include PostHog marketplace integration in the deployment flow - Created custom PostHog adapter with fallback logic to support both: - Marketplace env vars: `POSTHOG_PROJECT_API_KEY`, `POSTHOG_HOST` - Manual setup env vars: `NEXT_PUBLIC_POSTHOG_KEY`, `NEXT_PUBLIC_POSTHOG_HOST` - Updated README with clearer deployment instructions explaining what the deploy button does - Updated `.env.example` to document both configuration options ## Test plan - [ ] Test deploy button URL triggers PostHog marketplace integration flow - [ ] Verify app works with marketplace-provided env vars - [ ] Verify app still works with manually configured env vars (backwards compatible) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent f0f7f8a commit d3fa3f0

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

flags-sdk/posthog/.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# node -e 'console.log(`${crypto.randomBytes(32).toString("base64url")}`)'
33
FLAGS_SECRET=""
44

5+
# Option 1: Vercel Marketplace Integration (Recommended)
6+
# These are automatically set when you install PostHog from the Vercel Marketplace
7+
# POSTHOG_PROJECT_API_KEY is set automatically
8+
# POSTHOG_HOST is set automatically
9+
10+
# Option 2: Manual Configuration
11+
# If not using the Vercel Marketplace integration, set these manually:
512
NEXT_PUBLIC_POSTHOG_KEY=""
613
NEXT_PUBLIC_POSTHOG_HOST="https://us.i.posthog.com"
714

flags-sdk/posthog/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ If you deploy your own and configure the feature flags in PostHog, you can also
1515

1616
## Deploy this template
1717

18-
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fflags-sdk/posthog&env=FLAGS_SECRET&envDescription=The+FLAGS_SECRET+will+be+used+by+the+Flags+Explorer+to+securely+overwrite+feature+flags.+Must+be+32+random+bytes%2C+base64-encoded.+Use+the+generated+value+or+set+your+own.&envLink=https%3A%2F%2Fvercel.com%2Fdocs%2Fworkflow-collaboration%2Ffeature-flags%2Fsupporting-feature-flags%23flags_secret-environment-variable&project-name=posthog-flags-sdk-example&repository-name=posthog-flags-sdk-example)
18+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fflags-sdk%2Fposthog&env=FLAGS_SECRET&envDescription=The+FLAGS_SECRET+will+be+used+by+the+Flags+Explorer+to+securely+overwrite+feature+flags.+Must+be+32+random+bytes%2C+base64-encoded.+Use+the+generated+value+or+set+your+own.&envLink=https%3A%2F%2Fvercel.com%2Fdocs%2Fworkflow-collaboration%2Ffeature-flags%2Fsupporting-feature-flags%23flags_secret-environment-variable&project-name=posthog-flags-sdk-example&repository-name=posthog-flags-sdk-example&products=%5B%7B%22type%22%3A%22integration%22%2C%22integrationSlug%22%3A%22posthog%22%2C%22productSlug%22%3A%22posthog%22%2C%22protocol%22%3A%22experimentation%22%7D%5D)
19+
20+
Clicking the button above will:
21+
22+
1. Clone this repository to your GitHub account
23+
2. Create a new Vercel project
24+
3. Install the PostHog integration from the Vercel Marketplace (automatically configures `POSTHOG_PROJECT_API_KEY` and `POSTHOG_HOST`)
25+
4. Prompt you to generate a `FLAGS_SECRET` for the Flags Explorer
1926

2027
### Step 1: Link the project
2128

@@ -37,7 +44,7 @@ vercel env pull
3744

3845
### Step 3: Create Feature Flags
3946

40-
Head over to [PostHog](posthog.com) and create the feature flags required by this template.
47+
Head over to [PostHog](https://posthog.com) and create the feature flags required by this template.
4148

4249
Feature Flags:
4350

@@ -47,3 +54,16 @@ Feature Flags:
4754
You can also find the feature flag keys in the `flags.ts` file.
4855

4956
Set both feature flags to rollout to 50% of users.
57+
58+
## Manual Setup (without Marketplace integration)
59+
60+
If you prefer not to use the Vercel Marketplace integration, you can manually configure the environment variables:
61+
62+
1. Set `NEXT_PUBLIC_POSTHOG_KEY` to your PostHog project API key
63+
2. Set `NEXT_PUBLIC_POSTHOG_HOST` to your PostHog host (e.g., `https://us.i.posthog.com`)
64+
3. Set `FLAGS_SECRET` to a base64-encoded 32-byte random value
65+
66+
For the Flags Explorer feature, also set:
67+
68+
- `POSTHOG_PERSONAL_API_KEY` - Your PostHog personal API key
69+
- `POSTHOG_PROJECT_ID` - Your PostHog project ID

flags-sdk/posthog/flags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { postHogAdapter, type PostHogEntities } from '@flags-sdk/posthog'
1+
import { type PostHogEntities } from '@flags-sdk/posthog'
22
import { flag } from 'flags/next'
33
import { identify } from './lib/identify'
4+
import { postHogAdapter } from './lib/posthog-adapter'
45
import type { Adapter } from 'flags'
56

67
export const showSummerBannerFlag = flag<boolean, PostHogEntities>({
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createPostHogAdapter } from '@flags-sdk/posthog'
2+
3+
// Support both Vercel Marketplace integration env vars and manual setup env vars
4+
// Marketplace provides: POSTHOG_PROJECT_API_KEY, POSTHOG_HOST
5+
// Manual setup uses: NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_HOST
6+
const postHogKey =
7+
process.env.NEXT_PUBLIC_POSTHOG_KEY || process.env.POSTHOG_PROJECT_API_KEY
8+
const postHogHost =
9+
process.env.NEXT_PUBLIC_POSTHOG_HOST ||
10+
process.env.POSTHOG_HOST ||
11+
'https://us.i.posthog.com'
12+
13+
if (!postHogKey) {
14+
throw new Error(
15+
'Missing PostHog API key. Set NEXT_PUBLIC_POSTHOG_KEY or install the PostHog Vercel Marketplace integration.'
16+
)
17+
}
18+
19+
export const postHogAdapter = createPostHogAdapter({
20+
postHogKey,
21+
postHogOptions: {
22+
host: postHogHost,
23+
},
24+
})

0 commit comments

Comments
 (0)