: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { join } from 'node:path'
import { z } from 'zod'
import type { LoadedConfig } from '@/core/config'
import type { Context } from '@/core/context'
import { absolutePath } from '@/core/paths'
export const LANGUAGES = ['typescript', 'golang', 'python'] as const
export type Language = (typeof LANGUAGES)[number]
interface LanguageInfo {
name: string
// Python modules can't have hyphens in their names.
defaultFile: string
nextSteps: (file: string) => string[]
}
export const LANGUAGE_INFO: Record<Language, LanguageInfo> = {
typescript: {
name: 'TypeScript',
defaultFile: 'posthog-typed.ts',
nextSteps: (file) => [
`Import PostHog from the generated module: import posthog from './${file}'`,
"Known events get autocomplete and type checks: posthog.capture('event_name', { property: 'value' })",
"captureRaw() skips the type checks: posthog.captureRaw('dynamic_event_name', { whatever: 'data' })",
],
},
golang: {
name: 'Go',
defaultFile: 'posthog-typed.go',
nextSteps: (file) => [
'Install the PostHog Go SDK if you have not yet: go get github.com/posthog/posthog-go',
`Move ${file} into a folder named typed, or change the package line at the top of the file`,
'Capture typed events: client.Enqueue(typed.EventNameCapture("user_id", requiredProp))',
],
},
python: {
name: 'Python',
defaultFile: 'posthog_typed.py',
nextSteps: (file) => [
`Import the typed client from ${file}: from posthog_typed import PosthogTyped`,
'Create it like the PostHog client: client = PosthogTyped("<project token>", host="<host>")',
'Capture typed events: client.capture_event_name(required_property="value", distinct_id="user_123")',
],
},
}
export function isLanguage(value: string): value is Language {
return (LANGUAGES as readonly string[]).includes(value)
}
const Definitions = z.object({ content: z.string(), event_count: z.number(), schema_hash: z.string() })
export type Definitions = z.infer<typeof Definitions>
export function fetchDefinitions(ctx: Context, language: Language): Promise<Definitions> {
return ctx.posthog().then((api) => api.get(`event_definitions/${language}/`, Definitions))
}
// posthog.json stores paths relative to its own folder. A new file goes in the working folder.
export function configRoot(ctx: Context, loaded: LoadedConfig | undefined): string {
return loaded?.root ?? ctx.runtime.cwd()
}
// Resolves a path the caller typed. A folder, or a path that ends with a slash, gets the
// language's default file name.
export async function chosenFile(ctx: Context, path: string, language: Language): Promise<string> {
const absolute = absolutePath(ctx.runtime.cwd(), path)
const asFolder = /[\\/]$/.test(path) || (await ctx.runtime.isDirectory(absolute))
return asFolder ? join(absolute, LANGUAGE_INFO[language].defaultFile) : absolute
}