Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.9 KB
Raw Blame
import { PostHog } from 'posthog-node'

import { Team } from '~/types'

import { defaultConfig } from '../config/config'

const fs = require('fs')

const posthog = defaultConfig.POSTHOG_API_KEY
    ? new PostHog(defaultConfig.POSTHOG_API_KEY, {
          host: defaultConfig.POSTHOG_HOST_URL,
          enableExceptionAutocapture: false, // TODO - disabled while data volume is a problem, PS seems /extremely/ chatty exceptions wise
      })
    : null

if (process.env.NODE_ENV === 'test' && posthog) {
    void posthog.disable()
}

export function initSuperProperties(eventsIngestionPipeline: string | null = null): void {
    if (posthog) {
        const superProperties: Record<string, any> = {
            plugin_server_mode: defaultConfig.PLUGIN_SERVER_MODE,
            deployment: defaultConfig.CLOUD_DEPLOYMENT,
            plugin_server_events_ingestion_pipeline: eventsIngestionPipeline,
            // Super properties matching Python posthoganalytics.super_properties (posthog/apps.py)
            region: defaultConfig.CLOUD_DEPLOYMENT,
            service: defaultConfig.OTEL_SERVICE_NAME,
            environment: defaultConfig.OTEL_SERVICE_ENVIRONMENT,
        }

        try {
            // Docker containers should have a commit.txt file in the base directory with the git
            // commit hash used to generate them. `nodejs` runs from a child directory, so we
            // need to look up one level.
            superProperties['release'] = fs.readFileSync('../commit.txt', 'utf8')
        } catch {
            // The release isn't required, it's just nice to have.
        }

        void posthog.register(superProperties)
    }
}

export function captureTeamEvent(
    team: Team,
    event: string,
    properties: Record<string, any> = {},
    distinctId: string | null = null
): void {
    if (posthog) {
        posthog.capture({
            distinctId: distinctId ?? team.uuid,
            event,
            properties: {
                team: team.uuid,
                ...properties,
            },
            groups: {
                project: team.uuid,
                organization: team.organization_id,
                instance: defaultConfig.SITE_URL,
            },
        })
    }
}

export async function isFeatureFlagEnabled(
    key: string,
    distinctId: string,
    options?: {
        groups?: Record<string, string>
        personProperties?: Record<string, string>
        groupProperties?: Record<string, Record<string, string>>
        onlyEvaluateLocally?: boolean
        sendFeatureFlagEvents?: boolean
    }
): Promise<boolean> {
    if (!posthog) {
        return false
    }

    try {
        const isEnabled = await posthog.isFeatureEnabled(key, distinctId, options)
        return isEnabled ?? false
    } catch (error) {
        // Log errors to aid debugging of feature flag evaluation issues (e.g. SES v1 vs v2 gating).
        console.error('Error evaluating PostHog feature flag', {
            key,
            distinctId,
            options,
            error,
        })
        return false
    }
}

export function shutdown(): Promise<void> | null {
    return posthog ? posthog.shutdown() : null
}

export function flush(): void {
    if (posthog) {
        void posthog.flush().catch(() => null)
    }
}

// We use sentry-style hints rather than our flat property list all over the place,
// so define a type for them that we can flatten internally
type Primitive = number | string | boolean | bigint | symbol | null | undefined
interface ExceptionHint {
    tags: Record<string, Primitive>
    extra: Record<string, any>
}

export function captureException(exception: any, hint?: Partial<ExceptionHint>): void {
    if (posthog) {
        let additionalProperties = {}
        if (hint) {
            additionalProperties = {
                ...(hint.tags || {}),
                ...(hint.extra || {}),
            }
        }

        posthog.captureException(exception, undefined, additionalProperties)
    }
}