From 046ab1060de52cd103c3b55f955f579a914e0343 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Fri, 10 Apr 2026 15:53:00 +0900 Subject: [PATCH] feat(analytics): add PostHog analytics module Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- src/shared/posthog.ts | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/shared/posthog.ts diff --git a/src/shared/posthog.ts b/src/shared/posthog.ts new file mode 100644 index 000000000..17b692ec1 --- /dev/null +++ b/src/shared/posthog.ts @@ -0,0 +1,46 @@ +import os from "os" +import { PostHog } from "posthog-node" + +type PostHogClient = Pick + +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[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, + }) +}