feat(analytics): add PostHog analytics module

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-10 15:53:00 +09:00
parent 3fe5f0a153
commit 046ab1060d
+46
View File
@@ -0,0 +1,46 @@
import os from "os"
import { PostHog } from "posthog-node"
type PostHogClient = Pick<PostHog, "capture" | "captureException" | "shutdown">
const NO_OP_POSTHOG: PostHogClient = {
capture: () => undefined,
captureException: () => undefined,
shutdown: async () => undefined,
}
function shouldDisablePostHog(): boolean {
return process.env.OMO_DISABLE_POSTHOG === "true"
}
function hasPostHogApiKey(): boolean {
return (process.env.POSTHOG_API_KEY ?? "").trim().length > 0
}
function createPostHogClient(options: ConstructorParameters<typeof PostHog>[1]): PostHogClient {
if (shouldDisablePostHog() || !hasPostHogApiKey()) {
return NO_OP_POSTHOG
}
return new PostHog(process.env.POSTHOG_API_KEY ?? "", options)
}
export function getPostHogDistinctId(): string {
return os.hostname()
}
export function createCliPostHog(): PostHogClient {
return createPostHogClient({
host: process.env.POSTHOG_HOST,
enableExceptionAutocapture: true,
flushAt: 1,
flushInterval: 0,
})
}
export function createPluginPostHog(): PostHogClient {
return createPostHogClient({
host: process.env.POSTHOG_HOST,
enableExceptionAutocapture: true,
})
}