posthog-sdk-patterns

Featured

Production-ready PostHog SDK patterns: singleton client, typed events, React hooks, Next.js App Router integration, and Python patterns. Trigger: "posthog SDK patterns", "posthog best practices", "posthog React hook", "posthog Next.js", "posthog typescript".

AI & Automation 2,944 stars 440 forks Updated today MIT

Install

View on GitHub

Quality Score: 99/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# PostHog SDK Patterns ## Overview Production-ready patterns for PostHog integrations: singleton client, type-safe event capture, React hooks for feature flags, Next.js App Router provider, and Python integration patterns. ## Prerequisites - `posthog-js` and/or `posthog-node` installed - TypeScript project with strict mode - React/Next.js (for frontend patterns) ## Instructions ### Step 1: Singleton Server Client ```typescript // lib/posthog-server.ts import { PostHog } from 'posthog-node'; let client: PostHog | null = null; export function getPostHogServer(): PostHog { if (!client) { client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { host: process.env.POSTHOG_HOST || 'https://us.i.posthog.com', personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY, flushAt: 20, flushInterval: 10000, }); } return client; } // Graceful shutdown hook process.on('SIGTERM', async () => { if (client) await client.shutdown(); process.exit(0); }); ``` ### Step 2: Type-Safe Event Capture ```typescript // lib/analytics/events.ts type EventMap = { user_signed_up: { method: 'email' | 'google' | 'github' }; feature_used: { feature_name: string; duration_ms?: number }; subscription_started: { plan: string; interval: 'monthly' | 'annual'; mrr: number }; item_created: { item_type: string; source: 'web' | 'api' }; search_performed: { query: string; results_count: number }; }; export function capture<K extends keyof EventMap>( distinc...

Details

Author
jeremylongshore
Repository
jeremylongshore/claude-code-plugins-plus-skills
Created
1 years ago
Last Updated
today
Language
Python
License
MIT

Integrates with

Bundled in these plugins

Similar Skills

Semantically similar based on skill content — not just same category