feat(analytics): build-time bake + telemetry depth (#336)

Bake PostHog + Sentry into the published Docker image (SNAPOTTER_ANALYTICS
build arg, codegen script). Delete entire consent system. Move event emission
to BullMQ worker. Add cross-tier identity stitching, Sentry performance
tracing on both tiers, frontend funnel events. Fix stateful regex bug.

86 files changed, 1593 insertions(+), 3747 deletions(-)
This commit is contained in:
SnapOtter
2026-06-24 11:05:39 +08:00
committed by GitHub
parent a53038ed96
commit 5d36ac06d8
86 changed files with 1594 additions and 3748 deletions
+47 -213
View File
@@ -1,23 +1,20 @@
// @vitest-environment node
//
// Proves the invariant: PostHog and Sentry are NEVER called when
// analytics is disabled or the user has not granted consent.
// analytics is disabled in the baked config.
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const mockPosthogInit = vi.fn(() => ({
capture: mockCapture,
identify: mockIdentify,
startSessionRecording: mockStartSessionRecording,
startSessionRecording: vi.fn(),
opt_in_capturing: vi.fn(),
opt_out_capturing: mockOptOut,
reset: mockReset,
opt_out_capturing: vi.fn(),
reset: vi.fn(),
register: vi.fn(),
get_distinct_id: vi.fn(() => "test-id"),
persistence: { disabled: false },
}));
const mockCapture = vi.fn();
const mockIdentify = vi.fn();
const mockStartSessionRecording = vi.fn();
const mockOptOut = vi.fn();
const mockReset = vi.fn();
vi.mock("posthog-js", () => ({
__esModule: true,
@@ -25,8 +22,10 @@ vi.mock("posthog-js", () => ({
}));
const mockSentryInit = vi.fn();
const mockBrowserTracingIntegration = vi.fn(() => ({ name: "BrowserTracing" }));
vi.mock("@sentry/react", () => ({
init: mockSentryInit,
browserTracingIntegration: mockBrowserTracingIntegration,
}));
const noop = () => {};
@@ -43,15 +42,6 @@ afterAll(() => {
vi.restoreAllMocks();
});
import {
identify,
initAnalytics,
setAnalyticsConsent,
shutdownAnalytics,
startErrorReplay,
track,
} from "@/lib/analytics";
const enabledConfig = {
enabled: true,
posthogApiKey: "phc_test",
@@ -70,222 +60,66 @@ const disabledConfig = {
instanceId: "",
};
function clearAllMocks() {
shutdownAnalytics();
mockPosthogInit.mockClear();
mockCapture.mockClear();
mockIdentify.mockClear();
mockStartSessionRecording.mockClear();
mockOptOut.mockClear();
mockReset.mockClear();
mockSentryInit.mockClear();
}
type AnalyticsModule = typeof import("../../../apps/web/src/lib/analytics");
let mod: AnalyticsModule;
describe("Analytics No-Leak Invariant", () => {
beforeEach(clearAllMocks);
describe("Analytics No-Leak Invariant (baked model)", () => {
beforeEach(async () => {
mockPosthogInit.mockClear();
mockCapture.mockClear();
mockSentryInit.mockClear();
vi.resetModules();
mod = await import("../../../apps/web/src/lib/analytics");
});
// ── Scenario 1: Server has analytics disabled ─────────────────────
describe("when server config.enabled is false", () => {
describe("when config.enabled is false", () => {
it("initAnalytics never calls posthog.init", async () => {
setAnalyticsConsent(true);
await initAnalytics(disabledConfig);
await mod.initAnalytics(disabledConfig);
expect(mockPosthogInit).not.toHaveBeenCalled();
});
it("initAnalytics never calls Sentry.init", async () => {
setAnalyticsConsent(true);
await initAnalytics(disabledConfig);
await mod.initAnalytics(disabledConfig);
expect(mockSentryInit).not.toHaveBeenCalled();
});
it("track() is a silent no-op", async () => {
setAnalyticsConsent(true);
await initAnalytics(disabledConfig);
track("test_event", { key: "value" });
await mod.initAnalytics(disabledConfig);
mod.track("test_event", { key: "value" });
expect(mockCapture).not.toHaveBeenCalled();
});
it("identify() is a silent no-op", async () => {
setAnalyticsConsent(true);
await initAnalytics(disabledConfig);
identify("inst-1", { version: "1.0" });
expect(mockIdentify).not.toHaveBeenCalled();
});
it("startErrorReplay() is a silent no-op", async () => {
setAnalyticsConsent(true);
await initAnalytics(disabledConfig);
startErrorReplay();
expect(mockStartSessionRecording).not.toHaveBeenCalled();
});
});
// ── Scenario 2: Consent never granted ─────────────────────────────
describe("when consent is never granted (fresh user)", () => {
it("initAnalytics with enabled config but no consent skips PostHog", async () => {
// Do NOT call setAnalyticsConsent(true) -- simulates fresh user
await initAnalytics(enabledConfig);
expect(mockPosthogInit).not.toHaveBeenCalled();
});
it("track() never calls posthog.capture", () => {
track("should_not_fire");
expect(mockCapture).not.toHaveBeenCalled();
});
it("identify() never calls posthog.identify", () => {
identify("inst-1", {});
expect(mockIdentify).not.toHaveBeenCalled();
});
it("startErrorReplay() never calls posthog.startSessionRecording", () => {
startErrorReplay();
expect(mockStartSessionRecording).not.toHaveBeenCalled();
});
it("no PostHog or Sentry SDK is loaded at all", async () => {
await initAnalytics(enabledConfig);
expect(mockPosthogInit).not.toHaveBeenCalled();
expect(mockSentryInit).not.toHaveBeenCalled();
});
});
// ── Scenario 3: Consent explicitly revoked ────────────────────────
describe("when consent is revoked after being granted", () => {
it("shutdownAnalytics opts out and resets PostHog", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
describe("when config.enabled is true", () => {
it("initAnalytics calls posthog.init", async () => {
await mod.initAnalytics(enabledConfig);
expect(mockPosthogInit).toHaveBeenCalledOnce();
setAnalyticsConsent(false);
expect(mockOptOut).toHaveBeenCalledOnce();
expect(mockReset).toHaveBeenCalledOnce();
});
it("track() is silent after revocation", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
mockCapture.mockClear();
setAnalyticsConsent(false);
track("should_not_fire");
expect(mockCapture).not.toHaveBeenCalled();
it("track() forwards to posthog.capture", async () => {
await mod.initAnalytics(enabledConfig);
mod.track("tool_used", { tool: "resize" });
expect(mockCapture).toHaveBeenCalledWith("tool_used", { tool: "resize" });
});
it("identify() is silent after revocation", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
mockIdentify.mockClear();
setAnalyticsConsent(false);
identify("inst-1", { phase: 2 });
expect(mockIdentify).not.toHaveBeenCalled();
});
it("startErrorReplay() is silent after revocation", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
mockStartSessionRecording.mockClear();
setAnalyticsConsent(false);
startErrorReplay();
expect(mockStartSessionRecording).not.toHaveBeenCalled();
});
it("Sentry beforeSend returns null after revocation", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find((call: unknown[]) => call[0]?.beforeSend);
expect(sentryCall).toBeDefined();
const beforeSend = sentryCall?.[0].beforeSend;
setAnalyticsConsent(false);
const result = beforeSend({ exception: { values: [] } });
expect(result).toBeNull();
});
it("Sentry beforeBreadcrumb returns null after revocation", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find(
(call: unknown[]) => call[0]?.beforeBreadcrumb,
it("initAnalytics initializes Sentry when sentryDsn provided", async () => {
await mod.initAnalytics(enabledConfig);
expect(mockSentryInit).toHaveBeenCalledOnce();
expect(mockSentryInit).toHaveBeenCalledWith(
expect.objectContaining({
dsn: "https://sentry.test/123",
sendDefaultPii: false,
}),
);
expect(sentryCall).toBeDefined();
const beforeBreadcrumb = sentryCall?.[0].beforeBreadcrumb;
setAnalyticsConsent(false);
const result = beforeBreadcrumb({ category: "console", message: "test" });
expect(result).toBeNull();
});
});
// ── Scenario 4: Remind-later state ────────────────────────────────
describe("when user is in remind-later state (consent = null)", () => {
it("remind-later calls setAnalyticsConsent(false), not true", () => {
// This is tested in the store, but verify the invariant:
// When consent hasn't been given, setAnalyticsConsent(false) is called,
// which means PostHog/Sentry are never active during the remind period.
setAnalyticsConsent(false);
track("during_remind_period");
expect(mockCapture).not.toHaveBeenCalled();
});
it("no SDK activity after remind-later even with enabled config", async () => {
// Simulate: user hit "Not right now" -- consent was never true
setAnalyticsConsent(false);
await initAnalytics(enabledConfig);
expect(mockPosthogInit).not.toHaveBeenCalled();
expect(mockSentryInit).not.toHaveBeenCalled();
track("event_during_remind");
identify("inst-1", {});
startErrorReplay();
expect(mockCapture).not.toHaveBeenCalled();
expect(mockIdentify).not.toHaveBeenCalled();
expect(mockStartSessionRecording).not.toHaveBeenCalled();
});
});
// ── Scenario 5: Consent race condition ────────────────────────────
describe("consent revoked during async SDK import", () => {
it("PostHog is not active if consent revoked while import resolves", async () => {
setAnalyticsConsent(true);
const initPromise = initAnalytics(enabledConfig);
setAnalyticsConsent(false);
await initPromise;
track("after_race_condition");
expect(mockCapture).not.toHaveBeenCalled();
});
});
// ── Scenario 6: Multiple rapid toggles ────────────────────────────
describe("rapid consent toggles", () => {
it("ending on false means nothing is active", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
setAnalyticsConsent(false);
setAnalyticsConsent(true);
setAnalyticsConsent(false);
setAnalyticsConsent(true);
setAnalyticsConsent(false);
mockCapture.mockClear();
mockIdentify.mockClear();
track("after_toggles");
identify("inst-1", {});
expect(mockCapture).not.toHaveBeenCalled();
expect(mockIdentify).not.toHaveBeenCalled();
});
});
// ── Scenario 7: PII scrubbing even when consent is granted ────────
describe("PII never leaks even with consent", () => {
describe("PII never leaks even when analytics enabled", () => {
it("Sentry strips user.email and user.username from events", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
await mod.initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find((call: unknown[]) => call[0]?.beforeSend);
const beforeSend = sentryCall?.[0].beforeSend;
if (!beforeSend) return;
const event = {
user: { email: "user@example.com", username: "admin", id: "123" },
@@ -298,10 +132,10 @@ describe("Analytics No-Leak Invariant", () => {
});
it("Sentry redacts file paths from exception values", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
await mod.initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find((call: unknown[]) => call[0]?.beforeSend);
const beforeSend = sentryCall?.[0].beforeSend;
if (!beforeSend) return;
const event = {
exception: {
@@ -322,23 +156,23 @@ describe("Analytics No-Leak Invariant", () => {
});
it("Sentry blocks ui.click breadcrumbs", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
await mod.initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find(
(call: unknown[]) => call[0]?.beforeBreadcrumb,
);
const beforeBreadcrumb = sentryCall?.[0].beforeBreadcrumb;
if (!beforeBreadcrumb) return;
expect(beforeBreadcrumb({ category: "ui.click" })).toBeNull();
});
it("Sentry blocks fetch breadcrumbs to file URLs", async () => {
setAnalyticsConsent(true);
await initAnalytics(enabledConfig);
await mod.initAnalytics(enabledConfig);
const sentryCall = mockSentryInit.mock.calls.find(
(call: unknown[]) => call[0]?.beforeBreadcrumb,
);
const beforeBreadcrumb = sentryCall?.[0].beforeBreadcrumb;
if (!beforeBreadcrumb) return;
expect(
beforeBreadcrumb({