cloud / internal-packages / installer / src / analytics.tsmain at aacbc74
// analytics.ts — WHERE PEOPLE STOP IN THE WIZARD, as anonymous events in PostHog. The whole of
// what iterate Cloud's pages report is in this file.
//
// cloud page viewed { path } a page of the Cloud, by its path alone
// cloud step reached { step, index } the wizard shows a step (its id)
// cloud deploy asked { recover } the wizard's Deploy step starts a run
// cloud update asked { recover } "Your deployments" starts one
// cloud erase asked {} "Erase and replace" starts one
// cloud deploy done { erase } a run this page watched succeeded
// cloud deploy failed { erase, failed_step } …or failed, at this step of the run
//
// NOTHING THAT NAMES A PERSON OR THEIR THINGS LEAVES THE PAGE: no email, no domain, no Cloudflare
// account or zone id, no token. So nothing is captured on its own (no autocapture, no page view of
// PostHog's, no recording, no errors, no feature flags), every event passes `sanitized`, which
// keeps the properties listed below and no others, and a URL is its origin and path (the erase
// page's query holds an account id and a domain; Cloudflare's return holds a code). The sender's
// IP address and the place PostHog would work out from it are kept out too (`sanitized`). A
// visitor is a random id that the browser keeps; nobody is ever identified.
//
// The key is the Worker's (`/api/config`, its POSTHOG_KEY variable): a deployment without one
// reports nothing, and posthog-js is not even loaded.
import type { CaptureResult } from "posthog-js";
import { failedStepOf } from "./deploy-steps.ts";
import type { DeployState } from "./api.ts";
type Properties = Record<string, string | number | boolean>;
/** Events asked for before posthog-js has loaded, then the function that sends one. */
let waiting: [string, Properties][] | null = [];
let send: ((event: string, properties: Properties) => void) | null = null;
/** Starts reporting, when the deployment has a key. Call once, after the config is read. */
export async function startAnalytics(key: string | null | undefined) {
if (!key) {
waiting = null;
return;
}
const { default: posthog } = await import("posthog-js");
posthog.init(key, {
api_host: "https://eu.i.posthog.com",
ui_host: "https://eu.posthog.com",
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
capture_exceptions: false,
capture_dead_clicks: false,
capture_heatmaps: false,
capture_performance: false,
disable_session_recording: true,
disable_surveys: true,
advanced_disable_flags: true,
// an anonymous visitor only: no person is made, and nobody is identified
person_profiles: "identified_only",
persistence: "localStorage",
before_send: sanitized,
});
send = (event, properties) => posthog.capture(event, properties);
for (const [event, properties] of waiting || []) send(event, properties);
waiting = null;
}
/** One event. Its name and its properties are in the list at the top of this file. */
export function track(event: string, properties: Properties = {}) {
if (send) send(event, properties);
else waiting?.push([event, properties]);
}
/** The properties an event may carry: ours, and what says which browser and visit sent it. */
const KEPT = new Set([
"path",
"step",
"index",
"recover",
"erase",
"failed_step",
"token",
"distinct_id",
"$session_id",
"$window_id",
"$insert_id",
"$time",
"$lib",
"$lib_version",
"$browser",
"$browser_version",
"$os",
"$os_version",
"$device_type",
"$screen_height",
"$screen_width",
"$viewport_height",
"$viewport_width",
"$host",
"$pathname",
"$current_url",
"$process_person_profile",
]);
/** An event with the kept properties alone, and its URL cut to origin and path: the promise that
* nothing personal is sent. posthog-js adds the page's whole URL and its referrer on its own. */
function sanitized(event: CaptureResult | null): CaptureResult | null {
if (!event) return null;
const properties = Object.fromEntries(
Object.entries(event.properties || {}).filter(([name]) => KEPT.has(name)),
);
const url = properties.$current_url;
if (typeof url === "string" && URL.canParse(url))
properties.$current_url = new URL(url).origin + new URL(url).pathname;
// PostHog's own server adds the sender's IP address to an event, and a place worked out from it
// (city, postal code, coordinates). It keeps an `$ip` that the event already carries (a null it
// replaces: both tried on 2026-11-12), and `$geoip_disable` stops the lookup of the place.
properties.$ip = "0.0.0.0";
properties.$geoip_disable = true;
return { ...event, properties, $set: undefined, $set_once: undefined };
}
/** How each run was going when this page last asked, by its domain: kept here, never sent. */
const runs = new Map<string, DeployState["status"]>();
/** What one call to the Worker says about a deploy, an update or an erase. Called with every
* answer (api.ts `call`); most say nothing here. */
export function callAnswered(input: {
method: string;
route: string;
zone: string;
body: unknown;
ok: boolean;
state?: DeployState;
}) {
const { method, route, ok } = input;
if (!ok) return;
if (route !== "/api/deploy") return;
if (method === "POST") {
const asked = (input.body || {}) as Record<string, unknown>;
const recover = asked.recover === true;
// "Erase and replace" names what it read (`erase`); the list of deployments sends no email
if ("erase" in asked) track("cloud erase asked");
else if ("email" in asked) track("cloud deploy asked", { recover });
else track("cloud update asked", { recover });
return;
}
const status = input.state?.status;
if (method !== "GET" || !input.state || !status) return;
const before = runs.get(input.zone);
runs.set(input.zone, status);
// once, when a run this page saw running ends: a finished run met again on a reload says nothing
if (before !== "running" || status === "running") return;
const erase = input.state.erase === true;
if (status === "succeeded") track("cloud deploy done", { erase });
if (status === "failed")
track("cloud deploy failed", {
erase,
failed_step: failedStepOf(input.state) || "before-first-step",
});
}