Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions apps/api/src/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { PostHog } from "posthog-node";

const posthogDisabled = !process.env.POSTHOG_KEY || !process.env.POSTHOG_HOST;
// PostHog requires a non-empty API key even when disabled.
// Docker Compose sets env vars to empty strings (not undefined) when unset,
// so we normalize empty strings to undefined before using nullish coalescing.
function nonEmpty(value: string | undefined): string | undefined {
if (!value) {
return undefined;
}
return value;
}
Comment on lines +6 to +11

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nonEmpty function uses a falsy check which correctly handles empty strings, but it won't catch whitespace-only strings like " " or "\t". Consider using .trim() before the check to ensure whitespace-only strings are also normalized to undefined. For example: if (!value?.trim()) { return undefined; }

Copilot uses AI. Check for mistakes.

// PostHog requires a non-empty API key even when disabled, so we use a placeholder
export const posthog = new PostHog(
process.env.POSTHOG_KEY ?? "phc_placeholder",
{
host: process.env.POSTHOG_HOST ?? "https://localhost",
disabled: posthogDisabled,
},
);
const posthogKey = nonEmpty(process.env.POSTHOG_KEY);
const posthogHost = nonEmpty(process.env.POSTHOG_HOST);
const posthogDisabled = !posthogKey || !posthogHost;

export const posthog = new PostHog(posthogKey ?? "phc_placeholder", {
host: posthogHost ?? "https://localhost",
disabled: posthogDisabled,
});
Loading