Skip to content
trakoo
Esc
navigateopen⌘Jpreview
On this page

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:

PropType
tokenstring

Your PostHog project API key.

Typestring
api_host?string

PostHog instance URL. Change this for EU or self-hosted.

Typestring
Defaulthttps://us.i.posthog.com
debug?boolean

Log PostHog SDK activity to the console.

Typeboolean
Defaultfalse
enabled?boolean

Set to false to disable the provider without removing it.

Typeboolean
Defaulttrue

PostHogServerProvider forwards all PostHog Node options. The common ones:

PropType
apiKeystring

Your PostHog project API key.

Typestring
host?string

PostHog instance URL. Change this for EU or self-hosted.

Typestring
Defaulthttps://us.i.posthog.com
flushAt?number

Number of queued events that triggers a flush.

Typenumber
Default20
flushInterval?number

Milliseconds to wait before flushing queued events.

Typenumber
Default10000

Resources

Was this page helpful?