Posthog: Product Analytics That Don't Require a Data Team
Mixpanel and Amplitude cost thousands per month at scale. PostHog is open-source, self-hostable, and free up to 1M events/month. It gives you funnels, session replay, feature flags, and A/B testing in one tool.
Install
npm install posthog-js posthog-node
Client Setup (Next.js)
// providers/PosthogProvider.tsx
'use client';
import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { useEffect } from 'react';
export function PHProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST ?? 'https://us.i.posthog.com',
capture_pageview: false, // We'll do this manually
});
}, []);
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
}
Tracking Events
import { usePostHog } from 'posthog-js/react';
function CheckoutButton({ plan }) {
const posthog = usePostHog();
return (
<button onClick={() => {
posthog.capture('checkout_started', {
plan,
price: PRICES[plan],
source: 'pricing_page',
});
router.push('/checkout');
}}>
Get Started
</button>
);
}
Identifying Users
// After login
posthog.identify(user.id, {
email: user.email,
name: user.name,
plan: user.plan,
createdAt: user.createdAt,
});
// After logout
posthog.reset();
Server-Side Events
// posthog.server.ts
import { PostHog } from 'posthog-node';
const posthog = new PostHog(process.env.POSTHOG_KEY!, {
host: 'https://us.i.posthog.com',
flushAt: 20,
flushInterval: 10000,
});
// In Stripe webhook handler
posthog.capture({
distinctId: user.id,
event: 'subscription_started',
properties: { plan: 'pro', amount: 2900 },
});
await posthog.shutdown(); // Flush before serverless function exits
Feature Flags
// Server-side flag check
const isEnabled = await posthog.isFeatureEnabled('new-dashboard', userId);
// Client-side
const { isFeatureEnabled } = usePostHog();
const showNewUI = isFeatureEnabled('new-dashboard');
Key Events to Track for SaaS
| Event | When |
|---|---|
signup_completed |
User finishes onboarding |
feature_used |
Core feature activated |
checkout_started |
Clicked pricing CTA |
subscription_started |
Payment confirmed |
subscription_cancelled |
Churn event |
invite_sent |
Viral loop action |
PostHog ships pre-configured in the AI SaaS Starter Kit — client + server setup, key SaaS events pre-wired. $99 at whoffagents.com.
Top comments (0)