DEV Community

Alex Spinov
Alex Spinov

Posted on

PostHog Has a Free API: Product Analytics, Feature Flags, and A/B Testing in One Platform

What is PostHog?

PostHog is an all-in-one product analytics platform — event tracking, session replay, feature flags, A/B testing, and surveys. Open source, self-hostable, and with a generous free tier.

Free tier: 1M events/month, 5K session recordings, unlimited feature flags.

Quick Start

npm install posthog-js
Enter fullscreen mode Exit fullscreen mode
import posthog from "posthog-js";

posthog.init("YOUR_API_KEY", {
  api_host: "https://us.i.posthog.com",
});

// Track events
posthog.capture("button_clicked", {
  button_name: "signup",
  page: "/pricing",
});

// Identify users
posthog.identify("user_123", {
  email: "alice@example.com",
  plan: "pro",
  company: "Acme Inc",
});
Enter fullscreen mode Exit fullscreen mode

The REST API

export POSTHOG_URL="https://us.i.posthog.com"
export POSTHOG_KEY="your-personal-api-key"
export PROJECT_ID="your-project-id"
Enter fullscreen mode Exit fullscreen mode

Capture Events

curl -X POST "$POSTHOG_URL/capture/" \
  -H "Content-Type: application/json" \
  -d '{
    "api_key": "YOUR_PROJECT_KEY",
    "event": "purchase_completed",
    "distinct_id": "user_123",
    "properties": {
      "amount": 49.99,
      "currency": "USD",
      "product": "Pro Plan"
    }
  }'
Enter fullscreen mode Exit fullscreen mode

Query Events

curl "$POSTHOG_URL/api/projects/$PROJECT_ID/events/?event=purchase_completed&limit=10" \
  -H "Authorization: Bearer $POSTHOG_KEY"
Enter fullscreen mode Exit fullscreen mode

Insights (Analytics Queries)

curl -X POST "$POSTHOG_URL/api/projects/$PROJECT_ID/insights/trend/" \
  -H "Authorization: Bearer $POSTHOG_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "events": [{"id": "$pageview", "math": "dau"}],
    "date_from": "-30d",
    "interval": "day"
  }'
Enter fullscreen mode Exit fullscreen mode

Feature Flags

// Check feature flag
if (posthog.isFeatureEnabled("new-pricing-page")) {
  showNewPricingPage();
} else {
  showOldPricingPage();
}

// Get flag payload
const variant = posthog.getFeatureFlagPayload("onboarding-flow");
// Returns: { "version": "v2", "steps": 3 }

// Via API
curl -X POST "$POSTHOG_URL/decide/?v=3" \
  -H "Content-Type: application/json" \
  -d '{
    "api_key": "YOUR_PROJECT_KEY",
    "distinct_id": "user_123"
  }'
Enter fullscreen mode Exit fullscreen mode

Create Feature Flag via API

curl -X POST "$POSTHOG_URL/api/projects/$PROJECT_ID/feature_flags/" \
  -H "Authorization: Bearer $POSTHOG_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "new-dashboard",
    "name": "New Dashboard UI",
    "filters": {
      "groups": [{
        "properties": [{"key": "plan", "value": "pro", "type": "person"}],
        "rollout_percentage": 50
      }]
    },
    "active": true
  }'
Enter fullscreen mode Exit fullscreen mode

A/B Testing (Experiments)

curl -X POST "$POSTHOG_URL/api/projects/$PROJECT_ID/experiments/" \
  -H "Authorization: Bearer $POSTHOG_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Pricing Page Test",
    "feature_flag_key": "pricing-test",
    "parameters": {
      "feature_flag_variants": [
        {"key": "control", "rollout_percentage": 50},
        {"key": "test", "rollout_percentage": 50}
      ]
    },
    "filters": {
      "events": [{"id": "purchase_completed", "order": 0}]
    }
  }'
Enter fullscreen mode Exit fullscreen mode

Node.js Backend SDK

import { PostHog } from "posthog-node";

const posthog = new PostHog("YOUR_API_KEY", {
  host: "https://us.i.posthog.com",
});

// Server-side event
posthog.capture({
  distinctId: "user_123",
  event: "api_call",
  properties: { endpoint: "/users", method: "GET" },
});

// Server-side feature flag
const enabled = await posthog.isFeatureEnabled("new-api", "user_123");

await posthog.shutdown(); // Flush events
Enter fullscreen mode Exit fullscreen mode

PostHog vs Others

Feature PostHog Mixpanel Amplitude GA4
Free events/mo 1M 20M 10M Unlimited
Feature flags Yes No Yes No
Session replay Yes No Yes No
A/B testing Yes No Yes Yes
Self-host Yes No No No
Open source Yes No No No

Need product analytics or feature flag setup?

📧 spinov001@gmail.com
🔧 My tools on Apify Store

PostHog, Mixpanel, or custom analytics? Share your stack!

Top comments (0)