Setup
PostHog is pre-configured and ready to use. Simply add your PostHog project credentials to enable analytics.# Optional: PostHog Analytics (https://zerostarter.dev/docs/features/analytics)
NEXT_PUBLIC_POSTHOG_HOST=https://eu.i.posthog.com
NEXT_PUBLIC_POSTHOG_KEY=phc_your_project_api_key
PostHog offers multiple hosting regions. Use
https://us.i.posthog.com for US hosting or https://eu.i.posthog.com for EU hosting.Architecture
PostHog is initialized at the application root and provided via React Context.app/providers.tsx
PostHog is wrapped in the
OuterProvider to ensure it’s available throughout your application, including in Server Components via Client Components.Event Tracking
Track custom events to understand how users interact with your application.Client Components
Use theusePostHog hook in Client Components:
app/components/signup-button.tsx
Track user properties
Identify users and set properties:Common events to track
Feature Flags
Feature flags allow you to control feature rollouts, run A/B tests, and toggle features without deploying code.Create a feature flag
new-dashboard)"use client"
import { useFeatureFlagEnabled } from "@posthog/react"
import { NewDashboard } from "@/components/new-dashboard"
import { OldDashboard } from "@/components/old-dashboard"
export default function DashboardPage() {
const isNewDashboard = useFeatureFlagEnabled("new-dashboard")
if (isNewDashboard) {
return <NewDashboard />
}
return <OldDashboard />
}
import { useFeatureFlagVariantKey } from "@posthog/react"
export function PricingPage() {
const pricingVariant = useFeatureFlagVariantKey("pricing-test")
return (
<div>
{pricingVariant === "control" && <PricingControl />}
{pricingVariant === "test-a" && <PricingTestA />}
{pricingVariant === "test-b" && <PricingTestB />}
</div>
)
}
Session Replay
PostHog automatically records user sessions, allowing you to replay user interactions and debug issues.Session replay is enabled by default in PostHog. You can configure privacy settings like masking sensitive inputs in your PostHog project settings.
Configure privacy
Mask sensitive data:View replays
- Go to Session Replay in PostHog
- Filter by user, date, or custom events
- Watch session recordings with console logs and network activity
- Debug issues by seeing exactly what users experienced
Analytics in Production
Environment-specific tracking
Track different environments separately:.env.production
.env.staging
Opt-out support
Respect user privacy preferences:Performance Monitoring
Track application performance metrics:DevTools
PostHog is integrated with the development tools panel (only shown in development):app/providers.tsx
Best Practices
Use descriptive event names
Use descriptive event names
Use clear, consistent naming conventions:
- ✅
user_signed_up,project_created,plan_upgraded - ❌
click,event1,action
Include context in properties
Include context in properties
Add relevant properties to understand the event:
Track both success and failure
Track both success and failure
Track errors to understand where users struggle:
Respect user privacy
Respect user privacy
- Never track PII (passwords, credit cards, SSNs) in events
- Use PostHog’s masking features for sensitive data
- Implement opt-out functionality
- Be transparent about what you track
Next Steps
PostHog Docs
Learn about PostHog’s features and integrations
UI Components
Build beautiful interfaces to track user interactions