
A production-ready template demonstrating how to integrate PostHog with Directus for implementing A/B testing in Next.js applications. This template provides a complete setup for content management with Directus and A/B testing with PostHog.
- π Page-level A/B testing with PostHog feature flags
- π Experiment tracking and analytics
- ποΈ Headless CMS with Directus for content management
- π Modern Next.js 15 frontend with App Router
- π Type-safe development environment
- β‘ Automated experiment creation via Directus flows
Prerequisites
- Docker and Docker Compose
- Node.js 18+ and pnpm (recommended) or npm
- A PostHog account and project
git clone https://github.com/directus-labs/directus-posthog-ab-testing.git
cd posthog-ab-testing
Navigate to the Directus directory and set up environment variables:
cd directus
cp .env.example .env
Edit the .env
file to configure your Directus installation:
Start the Directus instance:
docker compose up -d
Directus will be available at http://localhost:8055
(or your configured port).
Apply the template to set up the necessary collections and schema:
npx directus-template-cli@latest apply -p --directusUrl="http://localhost:8055" --directusToken="your-admin-token" --templateLocation="./directus/template" --templateType="local"
After applying the template and logging into Directus:
- Navigate to Globals
- Make sure you add the following fields to the global settings
- PostHog Project ID: Your PostHog project ID
- PostHog Private API Key: A private API key created in PostHog
These settings enable the built-in Directus flow that automatically creates and manages A/B testing experiments in PostHog when content editors create them in Directus.
Navigate to the Next.js directory and set up environment variables:
cd ../nextjs
cp .env.example .env
Edit the .env
file to configure your Next.js application:
NEXT_PUBLIC_DIRECTUS_URL=http://localhost:8055
DIRECTUS_PUBLIC_TOKEN=<your-directus-public-token>
DIRECTUS_FORM_TOKEN=<your-directus-form-token>
NEXT_PUBLIC_SITE_URL=http://localhost:3000
DRAFT_MODE_SECRET=<your-draft-mode-secret>
NEXT_PUBLIC_POSTHOG_KEY=<your-posthog-api-key>
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
Install dependencies and run the development server:
pnpm i
pnpm dev
The Next.js application will be available at http://localhost:3000
.
If you don't already have a PostHog account:
- Sign up at PostHog
- Create a new project
- Copy your API key (Project Settings > Project API Key)
- Create a personal API key:
- Go to your user settings by clicking on your profile picture > "Settings"
- Navigate to "Personal API Keys"
- Create a new key with appropriate permissions (at minimum: "View and create feature flags")
- Copy this key for use in Directus global variables
The integration provides two ways to create feature flags:
Content editors can create experiments directly in Directus:
- Navigate to the "Experiments" collection in Directus
- Create a new experiment with:
- Name: The experiment name
- Feature Flag Key: Unique identifier for the underlying feature flag
- Variants: Every experiement has to have a control variant, but you can add as many other variants as you like
The Directus flow will automatically create the corresponding feature flag in PostHog with the correct payload structure.
For manual configuration:
- In PostHog, navigate to Experiments
- Create a new experiment
- Name:
your-experiment-name
- Key:
your-experiment-key
- Roll out to: Select a percentage of users
- Configure variants
- Name:
- For Page (Redirect) tests, add a payload for the feature flags:
{ "experiment_type": "page", "control_path": "/original-page", "path": "/variant-page" }
This will automatically redirect users in the test group from the control path to the variant path.
βββ directus/ # Directus headless CMS
β βββ template/ # Directus schema template
β βββ extensions/ # Custom Directus extensions
β β βββ flows/ # Contains PostHog integration flows
β βββ docker-compose.yaml # Docker Compose setup
β βββ .env.example # Environment variables template
β
βββ nextjs/ # Next.js frontend application
β βββ src/
β β βββ posthog/ # PostHog integration
β β βββ app/ # Next.js application routes
β β βββ components/ # UI components
β β βββ lib/ # Utility functions and API clients
β β βββ middleware.ts # Next.js middleware for A/B testing
β β βββ types/ # TypeScript type definitions
β βββ .env.example # Environment variables template
The A/B testing implementation uses PostHog feature flags with payloads to handle page redirects:
- Content editors create experiments in Directus
- Directus flows automatically create the corresponding feature flags in PostHog
- Next.js middleware checks if the user is in a test group
- If the user is visiting a control page and is in the test group, they are redirected to the variant page
- PostHog tracks events and pageviews for both control and variant pages
- Results can be analyzed in the PostHog insights dashboard
You can create different types of experiments by modifying the feature flag payload structure in PostHog. The current template supports page-level A/B testing, but you can extend it to support:
- Block
- Content variation testing
- Behavioral experiments
The Directus template includes a base schema for managing content. You can extend it by adding custom collections and fields through the Directus admin interface.
-
Update the
.env
file with production values:- Set secure passwords
- Configure CORS settings properly
- Set cookie security settings
-
Deploy using Docker Compose or a container orchestration platform.
-
Build the Next.js application:
pnpm run build
-
Deploy to your preferred hosting platform (Vercel, Netlify, etc.).
Here's a callout to the specific items to make this integration work.
- Experiment Flow:
directus/template/src/flows.json
- Automates experiment creation in PostHog - Transformation Scripts:
scripts/format-ph-experiment-payload.js
andscripts/format-ph-feature-flag-payload.js
- Convert Directus data to PostHog format. Used in the Directus Flow. - Global Settings: Configure PostHog project ID and API key in Directus settings
- Directus Data Fetching:
nextjs/src/lib/directus/fetchers.ts
- Add the experiment and experiment variants collections when fetching pages - Middleware:
nextjs/src/middleware.ts
- Handles redirects based on feature flags - PostHog Provider:
nextjs/src/posthog/PostHogProvider.tsx
- Initializes PostHog on the client - API Routes:
nextjs/src/app/api/flags/route.ts
- Server-side feature flag fetching - Redirect Logic:
nextjs/src/posthog/redirect.ts
- Determines when to redirect users
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.