Loading...
Loading...
Implement PostHog analytics, feature flags, and session replay for Next.js apps. Use this skill for event tracking, user identification, A/B testing, experiments, and session recording setup. Also handles analytics reporting (funnel analysis, retention, SEO) with Google Search Console integration.
npx skill4agent add andrehfp/tinyplate posthog| File | Load When |
|---|---|
| Setting up PostHog from scratch, detailed code patterns |
| Designing event naming conventions, property patterns |
| Implementing feature flags, A/B tests, experiments |
# .env.local
NEXT_PUBLIC_POSTHOG_KEY=phc_your_project_key
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.comnext.config.tsconst nextConfig: NextConfig = {
async rewrites() {
return [
{
source: "/ingest/static/:path*",
destination: "https://us-assets.i.posthog.com/static/:path*",
},
{
source: "/ingest/:path*",
destination: "https://us.i.posthog.com/:path*",
},
{
source: "/ingest/decide",
destination: "https://us.i.posthog.com/decide",
},
];
},
// ... rest of config
};"connect-src 'self' ... https://*.posthog.com https://us.i.posthog.com https://us-assets.i.posthog.com",app/providers.tsx'use client'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import { useEffect } from 'react'
export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
// Use reverse proxy to bypass ad blockers
api_host: '/ingest',
ui_host: 'https://us.i.posthog.com',
defaults: '2025-05-24',
capture_pageview: false, // We handle manually for accurate funnels
person_profiles: 'identified_only',
})
}, [])
return <PHProvider client={posthog}>{children}</PHProvider>
}lib/posthog-server.tsimport { PostHog } from 'posthog-node'
let posthogClient: PostHog | null = null
export function getPostHogServer(): PostHog {
if (!posthogClient) {
posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
flushAt: 1,
flushInterval: 0,
})
}
return posthogClient
}| Pattern | Example | Use Case |
|---|---|---|
| | User actions |
| | Feature usage |
| | User journey |
| Pattern | Example | Type |
|---|---|---|
| | Any |
| | Boolean |
| | Boolean |
| | Number |
| | Timestamp |
Where to track?
├── User action in browser → Client (posthog-js)
├── API route / webhook → Server (posthog-node)
├── Server Component render → Server (posthog-node)
├── Need 100% accuracy → Server (no ad blockers)
└── Real-time UI feedback → Client (posthog-js)| Pitfall | Solution |
|---|---|
| Ad blockers blocking PostHog | Use reverse proxy ( |
| Events not appearing | Check ad blockers, verify API key, use reverse proxy |
| Duplicate pageviews | Use |
| Feature flag flicker | Bootstrap flags via middleware |
| Missing user data | Call |
| Inconsistent naming | Use |
| Browser extension blocking - use reverse proxy |
503 from | Ad blocker injecting fake response - use reverse proxy |