PostHog
Product analytics with feature flags and session replay, on both client and server.
PostHog is an open-source product analytics platform that also offers feature flags and session replay. It supports both browser and backend tracking, so the same trakoo event definitions work on the client and the server.
When to use it
- You want product analytics that span the browser and your backend.
- You need a single tool for events, funnels, feature flags, and session replay.
- You want to self-host or keep data in a specific region.
If you only need lightweight page views, a privacy-friendly option like Pirsch or Visitors may be a better fit.
Installation
Install trakoo alongside the PostHog SDKs you need — posthog-js for the client, posthog-node for the server.
pnpm install trakoo posthog-js posthog-node
Client-side usage
Use PostHogClientProvider from trakoo/providers/client. It accepts every option from the PostHog JS SDK, so anything you can pass to posthog.init() works here.
import { createClientAnalytics } from 'trakoo/client';
import { PostHogClientProvider } from 'trakoo/providers/client';
export const analytics = createClientAnalytics({
providers: [
new PostHogClientProvider({
token: import.meta.env.VITE_POSTHOG_KEY,
api_host: import.meta.env.VITE_POSTHOG_HOST
})
]
});
Server-side usage
Use PostHogServerProvider from trakoo/providers/server. Server analytics is stateless, so pass user context with each call.
import { createServerAnalytics } from 'trakoo/server';
import { PostHogServerProvider } from 'trakoo/providers/server';
export const serverAnalytics = createServerAnalytics({
providers: [
new PostHogServerProvider({
apiKey: process.env.POSTHOG_API_KEY!,
host: 'https://us.i.posthog.com'
})
]
});
await serverAnalytics.track('user_signed_up', {
plan: 'pro'
}, {
userId: 'user_123'
});
Configuration
PostHogClientProvider forwards all PostHog JS options. The keys you’ll set most often:
tokenstring
Your PostHog project API key.
stringapi_host?string
PostHog instance URL. Change this for EU or self-hosted.
stringhttps://us.i.posthog.comdebug?boolean
Log PostHog SDK activity to the console.
booleanfalseenabled?boolean
Set to false to disable the provider without removing it.
booleantruePostHogServerProvider forwards all PostHog Node options. The common ones:
apiKeystring
Your PostHog project API key.
stringhost?string
PostHog instance URL. Change this for EU or self-hosted.
stringhttps://us.i.posthog.comflushAt?number
Number of queued events that triggers a flush.
number20flushInterval?number
Milliseconds to wait before flushing queued events.
number10000