Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a malformed POSTHOG_PROJECT_ID fails the analytics run

David Leb148ce01df07
diff --git a/site/.env.example b/site/.env.example
index 10668667..7fcb2f32 100644
--- a/site/.env.example
+++ b/site/.env.example
@@ -69,7 +69,8 @@ NEXT_PUBLIC_POSTHOG_KEY="replace-with-posthog-project-key"
# PostHog query access for the nightly analytics job's HogQL reads: the
# personal API key (query-read scope) and the numeric project id from
# PostHog → Settings → Project. Optional: unset either and the job skips
-# PostHog extraction, reporting DB metrics only.
+# PostHog extraction, reporting DB metrics only. A project id that is set
+# to something other than the number fails the run.
POSTHOG_PERSONAL_API_KEY="replace-with-posthog-personal-api-key"
POSTHOG_PROJECT_ID="replace-with-posthog-numeric-project-id"
diff --git a/site/src/lib/analytics/posthog.test.ts b/site/src/lib/analytics/posthog.test.ts
new file mode 100644
index 00000000..209679c3
--- /dev/null
+++ b/site/src/lib/analytics/posthog.test.ts
@@ -0,0 +1,58 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { isPosthogQueryConfigured, PosthogConfigError } from "./posthog";
+
+const KEY = "POSTHOG_PERSONAL_API_KEY";
+const ID = "POSTHOG_PROJECT_ID";
+const saved = { key: process.env[KEY], id: process.env[ID] };
+
+const set = (name: string, value: string | undefined) => {
+ if (value === undefined) delete process.env[name];
+ else process.env[name] = value;
+};
+
+const raisesConfigError = (run: () => unknown): boolean => {
+ try {
+ run();
+ } catch (e) {
+ return e instanceof PosthogConfigError;
+ }
+ return false;
+};
+
+afterEach(() => {
+ set(KEY, saved.key);
+ set(ID, saved.id);
+});
+
+describe("PostHog query configuration", () => {
+ test("both present reads as configured", () => {
+ set(KEY, "phx_test");
+ set(ID, "110583");
+ expect(isPosthogQueryConfigured()).toBe(true);
+ });
+
+ test("either one missing skips PostHog", () => {
+ set(KEY, "phx_test");
+ set(ID, undefined);
+ expect(isPosthogQueryConfigured()).toBe(false);
+ set(KEY, undefined);
+ set(ID, "110583");
+ expect(isPosthogQueryConfigured()).toBe(false);
+ });
+
+ test("a project id that is not a number fails the run", () => {
+ set(KEY, "phx_test");
+ for (const bad of ["asdf", "0", "-3", "11.5", "110583abc"]) {
+ set(ID, bad);
+ expect(raisesConfigError(isPosthogQueryConfigured)).toBe(true);
+ }
+ });
+
+ test("blank and whitespace count as unset", () => {
+ set(KEY, "phx_test");
+ for (const blank of ["", " "]) {
+ set(ID, blank);
+ expect(isPosthogQueryConfigured()).toBe(false);
+ }
+ });
+});
diff --git a/site/src/lib/analytics/posthog.ts b/site/src/lib/analytics/posthog.ts
index f7d96980..934e6a73 100644
--- a/site/src/lib/analytics/posthog.ts
+++ b/site/src/lib/analytics/posthog.ts
@@ -7,14 +7,27 @@ import { addUtcDays } from "@/lib/analytics/schema";
const POSTHOG_HOST = "https://us.posthog.com";
-// The numeric project id from PostHog → Settings → Project.
-function posthogProjectId(): number {
- const id = Number(process.env.POSTHOG_PROJECT_ID);
- return Number.isInteger(id) && id > 0 ? id : 0;
+/** A POSTHOG_PROJECT_ID that is not a project id. Permanent: no retry fixes a
+ * bad env value. */
+export class PosthogConfigError extends Error {}
+
+// The numeric project id from PostHog → Settings → Project. Unset leaves this
+// deployment without PostHog. A malformed value raises: a typo here once cost
+// the pipeline months of PostHog data while every run reported success.
+function posthogProjectId(): number | undefined {
+ const raw = process.env.POSTHOG_PROJECT_ID?.trim();
+ if (!raw) return undefined;
+ const id = Number(raw);
+ if (!Number.isInteger(id) || id <= 0) {
+ throw new PosthogConfigError(
+ `POSTHOG_PROJECT_ID must be the numeric project id from PostHog → Settings → Project; got "${raw}".`,
+ );
+ }
+ return id;
}
export function isPosthogQueryConfigured(): boolean {
- return posthogProjectId() > 0 && Boolean(process.env.POSTHOG_PERSONAL_API_KEY);
+ return posthogProjectId() !== undefined && Boolean(process.env.POSTHOG_PERSONAL_API_KEY);
}
/** distinct_ids that fired app_loaded during the UTC day. app_loaded fires
@@ -23,7 +36,7 @@ export function isPosthogQueryConfigured(): boolean {
export async function fetchActiveDistinctIds(day: string): Promise<string[]> {
const apiKey = process.env.POSTHOG_PERSONAL_API_KEY;
const projectId = posthogProjectId();
- if (!apiKey || projectId <= 0) {
+ if (!apiKey || projectId === undefined) {
throw new Error("PostHog query access is not configured.");
}
const query = [
diff --git a/site/src/lib/jobs/analytics-daily.ts b/site/src/lib/jobs/analytics-daily.ts
index 036e96d9..b49cab71 100644
--- a/site/src/lib/jobs/analytics-daily.ts
+++ b/site/src/lib/jobs/analytics-daily.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { R2NotConfiguredError } from "@/cut/server/cloud/r2";
import { InvalidDayError, runAnalyticsDaily } from "@/lib/analytics/pipeline";
+import { PosthogConfigError } from "@/lib/analytics/posthog";
import { defineJob, JobFailure } from "@/lib/jobs/registry";
// The nightly analytics run, also retriggerable by hand. An empty payload is
@@ -21,9 +22,13 @@ export const analyticsDailyJob = defineJob(
try {
return await runAnalyticsDaily(payload);
} catch (e) {
- // Both are permanent: retrying an unconfigured store or an impossible
- // day can never succeed.
- if (e instanceof R2NotConfiguredError || e instanceof InvalidDayError) {
+ // All three are permanent: retrying an unconfigured store, a bad
+ // PostHog project id, or an impossible day can never succeed.
+ if (
+ e instanceof R2NotConfiguredError ||
+ e instanceof InvalidDayError ||
+ e instanceof PosthogConfigError
+ ) {
throw new JobFailure(e.message);
}
throw e;