Product Analytics
Captures product events and metrics.
@repo/analytics wraps PostHog for product event capture.
PostHog is an optional integration. If NEXT_PUBLIC_POSTHOG_KEY and NEXT_PUBLIC_POSTHOG_HOST are not set, the analytics export will be undefined and client-side initialization will be skipped.
Usage
To capture product events, you can use the analytics object exported from the @repo/analytics package. Since analytics is optional, use optional chaining when calling methods.
Start by importing the analytics object for the relevant environment:
// For server-side code
import { analytics } from '@repo/analytics/server';
// For client-side code
import { analytics } from '@repo/analytics';Then, you can use the capture method to send events:
analytics?.capture({
event: 'Product Purchased',
distinctId: 'user_123',
});Webhooks
Authentication and payment events reach PostHog through the Clerk and Stripe webhook routes in the api app, which call PostHog's Node.js server-side library:
Reverse Proxy
Next.js rewrites reverse-proxy PostHog requests, so client-side analytics events are not blocked by ad blockers.