PostHog

Add PostHog analytics tracking and feature flags to your Convex backend functions with server-side event capture and A/B testing.

Installation

npm install @posthog/convex

About PostHog

The official PostHog component for Convex that integrates analytics event tracking and feature flags into your backend functions. It provides both local feature flag evaluation (cached definitions with reactive queries) and remote evaluation options, plus comprehensive event capture methods for user analytics. Events are captured asynchronously via ctx.scheduler.runAfter to avoid blocking mutations and actions.

Benefits

Use cases

how to track events in Convex mutations

Use @posthog/convex to capture analytics events directly from your Convex mutations and actions. The component provides server-side PostHog integration that lets you track user actions, conversions, and custom events without relying on client-side tracking.

feature flags in Convex backend functions

Implement feature flags in your Convex functions using @posthog/convex to control backend behavior. You can gate database operations, API calls, and business logic based on PostHog feature flag configurations.

PostHog analytics with Convex actions

Track third-party API calls and external service interactions by integrating PostHog analytics into your Convex actions. This enables comprehensive product analytics across your entire backend workflow.

server-side A/B testing with Convex

Run server-side A/B tests in your Convex functions using PostHog feature flags. Test different database query strategies, business logic variations, or API integrations based on user segments.

Frequently asked questions

Does @posthog/convex work with both mutations and actions?

@posthog/convex can be used in both Convex mutations and actions to track events and check feature flags. Since it integrates with PostHog's server-side APIs, it works in any Convex function context.

How does PostHog integration affect Convex function performance?

The @posthog/convex component makes HTTP requests to PostHog's APIs, which adds network latency to your functions. Consider using it in actions rather than mutations for time-sensitive operations, or implement async event tracking patterns.

Can I use PostHog feature flags to control database queries in Convex?

Yes, @posthog/convex allows you to check feature flags within your Convex functions to conditionally execute different database queries or business logic. This enables server-side experimentation with your data layer.

Do I need PostHog client-side tracking if I use this component?

@posthog/convex provides server-side tracking only and complements client-side PostHog integration. You typically use both together for comprehensive analytics covering frontend interactions and backend operations.

Links