Loading...
Loading...
> Product analytics and session recordings with PostHog integration.
Fabrk integrates PostHog for product analytics. Track user behavior, analyze conversion funnels, and record sessions when configured.
DESC: PostHog only activates when API key is present. Leave empty to disable.
1$# .env.local2$NEXT_PUBLIC_POSTHOG_KEY=phc_your_project_key # Optional3$NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com # Optional45$# If not set, PostHog will not initialize (graceful degradation)
DESC: PostHog initializes automatically if configured (no setup needed)
1// src/lib/analytics/posthog-provider.tsx2// Provider checks for NEXT_PUBLIC_POSTHOG_KEY3// If present: initializes PostHog4// If absent: no-op (app works without analytics)56// Already integrated in src/app/layout.tsx7import { PostHogProvider } from '@/lib/analytics/posthog-provider';89export default function RootLayout({ children }) {10 return (11 <PostHogProvider>12 {children}13 </PostHogProvider>14 );15}
Track custom events in client components
1"use client";23import { trackEvent } from "@/lib/analytics/posthog-provider";45export function CheckoutButton({ plan, price }: Props) {6 const handleCheckout = async () => {7 // Safe tracking - no-op if PostHog not configured8 trackEvent("checkout_started", {9 plan_name: plan,10 plan_price: price,11 currency: "USD",12 });1314 // Proceed with checkout...15 };1617 return (18 <button onClick={handleCheckout}>19 Subscribe to {plan}20 </button>21 );22}2324// Common events to track:25// - user_signed_up26// - checkout_started27// - payment_completed28// - feature_used29// - org_created30// - member_invited
Track events from API routes and Server Actions using safe helpers
1// src/lib/analytics/events.ts provides safe helper functions2import { trackUserSignup, trackOrgCreated, trackSubscriptionStarted } from "@/lib/analytics/events";34// Example: Track user signup in API route5export async function POST(request: Request) {6 const { email, userId } = await request.json();78 // Create user in database...910 // Track signup (no-op if PostHog not configured)11 await trackUserSignup(userId, email, {12 provider: "credentials",13 timestamp: new Date().toISOString(),14 });1516 return Response.json({ success: true });17}1819// Example: Track organization creation20await trackOrgCreated(userId, orgId, orgName, {21 memberCount: 1,22 plan: "free",23});2425// Example: Track subscription26await trackSubscriptionStarted(userId, "pro", 99, "month");
Page views are automatically tracked by PostHogProvider
1// Page views are handled automatically by PostHogProvider2// No additional code needed!34// The provider tracks:5// - Route changes (usePathname)6// - Query parameters (useSearchParams)7// - Full URL with origin89// Events appear in PostHog as "$pageview" with:10// - $current_url: Full page URL11// - Automatic user properties12// - Session data
object_action