Claude Skills Guide

Claude Code PostHog Feature Flags Analytics Workflow

PostHog has become an essential platform for product analytics, feature flags, and experimentation. When combined with Claude Code’s skills and MCP (Model Context Protocol) capabilities, you can create powerful automation workflows that streamline analytics tasks, manage feature releases, and derive insights from your data. This guide walks you through building an integrated Claude Code + PostHog workflow for feature flag management and analytics automation.

Setting Up the PostHog Integration

Before diving into workflows, you’ll need to configure Claude Code to communicate with your PostHog instance. The recommended approach uses a custom skill with the MCP protocol to handle API interactions securely.

Create a new skill file at ~/.claude/skills posthog skill.md:

---
name: posthog
description: "Interact with PostHog for feature flags, events, and analytics"
---

# PostHog Skill

This skill provides commands for working with PostHog projects.

## Available Commands

### List Feature Flags
Retrieve all feature flags from your PostHog project:

POST /api/projects/:project_uuid/feature_flags/


### Evaluate Feature Flag
Check if a feature flag is enabled for a specific user:

POST /api/projects/:project_uuid/feature-flags/evaluate/


### Track Custom Event
Send custom events to PostHog:

POST /api/projects/:project_uuid/events/

Creating Feature Flag Management Workflows

One of the most valuable use cases is automating feature flag operations. Instead of manually toggling flags in the PostHog dashboard, you can use Claude Code to manage flags programmatically.

Example: Bulk Feature Flag Creation

When launching a new product feature across multiple environments, you might need to create several related flags:

import os
from posthog import PostHog

# Initialize client with your API key
client = PostHog(
    api_key=os.environ['POSTHOG_API_KEY'],
    host=os.environ['POSTHOG_HOST']
)

def create_feature_flags(project_id, feature_name, variants):
    """Create multivariate feature flag with percentage rollout"""
    response = client.feature_flags.create(
        project_id=project_id,
        name=feature_name,
        key=f"{feature_name}_ rollout",
        filters={
            "groups": [
                {
                    "properties": [],
                    "rollout_percentage": 100 if variants == ["control"] else 50
                }
            ],
            "multivariate": {
                "variants": [
                    {"name": variant, "key": variant, "rollout_percentage": 100 // len(variants)}
                    for variant in variants
                ]
            }
        }
    )
    return response

# Usage: Create flags for new checkout flow
flags = create_feature_flags(
    project_id="prod_123",
    feature_name="new_checkout",
    variants=["control", "variant_a", "variant_b"]
)
print(f"Created flag: {flags['key']}")

Integrating with Claude Code Skills

You can wrap this functionality in a Claude Code skill that accepts natural language commands:

## Manage Feature Flags

When asked to create, update, or delete feature flags:

1. First, read the current flag configuration from PostHog
2. Validate the proposed changes won't break existing experiments
3. Execute the API call to update the flag
4. Confirm the changes were applied correctly

Example workflow:
- "Create a feature flag for dark mode" → Calls create_feature_flag()
- "Roll out variant B to 50% of users" → Updates rollout_percentage
- "Check if the onboarding experiment is still running" → Queries flag status

Building Analytics Automation Pipelines

Beyond flag management, Claude Code excels at building analytics workflows that pull data from PostHog, process it, and generate actionable insights.

Automated Experiment Analysis

Here’s a practical workflow for analyzing experiment results:

from posthog import PostHog
import pandas as pd
from datetime import datetime, timedelta

def analyze_experiment(project_id, experiment_flag_key):
    """Pull experiment data and calculate conversion metrics"""
    client = PostHog(
        api_key=os.environ['POSTHOG_API_KEY'],
        host=os.environ['POSTHOG_HOST']
    )
    
    # Fetch events for the past 7 days
    start_date = datetime.now() - timedelta(days=7)
    
    # Query experiment exposure events
    query = {
        "kind": "EventsQuery",
        "select": [
            "properties.$feature/#{experiment_flag_key}",
            "properties.$set.account_tier",
            "count()"
        ],
        "where": [
            "event == 'experiment_exposure'"
        ],
        "after": start_date.isoformat()
    }
    
    results = client.cohorts.calculate(project_id, query)
    
    # Convert to DataFrame for analysis
    df = pd.DataFrame(results)
    
    # Calculate conversion rates by variant
    analysis = df.groupby('variant').agg({
        'conversions': 'sum',
        'users': 'nunique',
        'revenue': 'sum'
    }).assign(
        conversion_rate=lambda x: x['conversions'] / x['users'] * 100
    )
    
    return analysis.to_dict()

# Example: Analyze checkout experiment
results = analyze_experiment("prod_123", "checkout_v2_test")
for variant, metrics in results.items():
    print(f"{variant}: {metrics['conversion_rate']:.2f}% conversion")

Real-Time Monitoring with Claude Code

You can set up continuous monitoring workflows that alert you to significant changes in your metrics:

import os
import asyncio
from posthog import PostHog

class MetricsMonitor:
    def __init__(self, threshold=0.1):
        self.client = PostHog(
            api_key=os.environ['POSTHOG_API_KEY'],
            host=os.environ['POSTHOG_HOST']
        )
        self.threshold = threshold
        self.previous_values = {}
    
    async def check_metrics(self, metrics_to_watch):
        """Check if any metric has shifted significantly"""
        alerts = []
        
        for metric_name, query in metrics_to_watch.items():
            current_value = self.client.query(query)
            previous = self.previous_values.get(metric_name)
            
            if previous:
                change_pct = abs(current_value - previous) / previous
                if change_pct > self.threshold:
                    alerts.append({
                        'metric': metric_name,
                        'previous': previous,
                        'current': current_value,
                        'change': f"{change_pct*100:.1f}%"
                    })
            
            self.previous_values[metric_name] = current_value
        
        return alerts

# Usage in Claude Code skill
monitor = MetricsMonitor(threshold=0.15)
alerts = await monitor.check_metrics({
    'checkout_conversion': {"event": "checkout_completed"},
    'signup_rate': {"event": "sign_up_completed"}
})

if alerts:
    print("⚠️ Significant metric changes detected:")
    for alert in alerts:
        print(f"  {alert['metric']}: {alert['change']}")

Best Practices for Production Workflows

When building PostHog workflows with Claude Code, keep these recommendations in mind:

  1. Environment Separation: Always use separate PostHog projects for development and production. Store API keys in environment variables, never in skill files.

  2. Rate Limiting: PostHog’s API has rate limits. Implement exponential backoff in your automation scripts and cache frequently accessed data.

  3. Audit Trails: Log all flag changes and automated decisions. This helps troubleshoot issues and maintains compliance.

  4. Idempotent Operations: Design your workflows so they can be run multiple times safely. Use upsert patterns for flag creation.

  5. Error Handling: Always validate API responses and handle authentication errors gracefully.

Conclusion

Claude Code combined with PostHog creates a powerful automation layer for feature management and product analytics. By wrapping PostHog’s API in custom skills, you can manage feature flags through natural language commands, build automated experiment analysis pipelines, and set up real-time monitoring that keeps your team informed of important metric changes. The key is starting simple—perhaps just flag toggling—and gradually expanding to more sophisticated analytics workflows as your needs grow.

Built by theluckystrike — More at zovo.one