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,266 stars 315 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
7 months ago
Last Updated
today
Language
Python
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

AI & Automation Featured

posthog-hello-world

Create a minimal working PostHog example with event capture, identify, and feature flags. Use when starting a new PostHog integration, testing your setup, or learning basic posthog-js and posthog-node patterns. Trigger: "posthog hello world", "posthog example", "posthog quick start", "simple posthog code", "first posthog event".

2,266 Updated today
jeremylongshore
AI & Automation Featured

posthog-core-workflow-a

Implement PostHog product analytics: event capture, user identification, group analytics, and property management using posthog-js and posthog-node. Trigger: "posthog analytics", "capture events", "track users posthog", "posthog identify", "posthog group analytics", "product analytics".

2,266 Updated today
jeremylongshore
AI & Automation Featured

posthog-reference-architecture

Production PostHog architecture: event taxonomy, SDK layering, feature flag strategy, analytics module layout, and data pipeline integration patterns. Trigger: "posthog architecture", "posthog best practices", "posthog project structure", "how to organize posthog", "posthog design".

2,266 Updated today
jeremylongshore
AI & Automation Featured

posthog-common-errors

Diagnose and fix common PostHog errors: events not appearing, flags returning undefined, 401/429 errors, SDK initialization failures, and identity issues. Trigger: "posthog error", "fix posthog", "posthog not working", "debug posthog", "posthog events missing", "posthog broken".

2,266 Updated today
jeremylongshore
AI & Automation Featured

posthog-migration-deep-dive

Migrate to PostHog from Google Analytics, Mixpanel, Amplitude, or Segment. Covers dual-write strategy, historical data import, event name mapping, identity resolution, and feature flag based traffic shifting. Trigger: "migrate posthog", "posthog migration", "switch to posthog", "posthog from mixpanel", "posthog from GA", "posthog replatform".

2,266 Updated today
jeremylongshore