codeburn/windows/src/lib/telemetry.ts
iamtoruk 91706484d6 feat(windows): consent-gated telemetry from the tray app and the Capacity Dock
The tray had no telemetry. It now sends the same envelope as the desktop
app to the same endpoint, with app name codeburn-menubar. Consent comes
from the desktop app's telemetry state file when that app is installed,
so one decision covers both apps and events share one anonymous id;
otherwise the tray asks once, defaulting off for EU, EEA, UK and Swiss
locales and for an unknown locale, and keeps its own toggle under
Settings, General, Privacy. Switching off rotates the id.

Events, all name-only or bucketed: app_open, app_close with session
minutes, popover_open, settings_open with the pane, update_click with
the action, glance_open, dock_enabled and dock_disabled with the edge
and a scale bucket, dock_provider_switch, dock_drag_end with the edge,
and the CLI's usage_snapshot once a day, skipped when the desktop app is
the consent source so nothing is counted twice. The queue persists under
the app's local data directory, capped at two hundred events, and flushes
every five minutes and on quit; a 4xx drops the batch, anything else
keeps it. Debug builds never send.
2026-09-03 01:18:23 -07:00

73 lines
2.9 KiB
TypeScript

/// Anonymous, consent-gated product telemetry, from the page's side.
///
/// Every decision lives in Rust (`src-tauri/src/telemetry.rs`): whether there is consent at
/// all, whose consent it is, what a prop may contain, when a batch is sent. This file only
/// names the events and gets them across, which is why `track` neither asks whether it is
/// allowed nor reports when it was not: a page that had to know the current decision before
/// reporting anything would be one more place for that decision to go stale.
///
/// Nothing here is queued before a decision has been made, and nothing is sent from a debug
/// build. See the module doc in telemetry.rs for the whole set of invariants.
import { invoke } from '@tauri-apps/api/core'
/// What the desktop app's consent screen links to, so both apps explain themselves in the
/// same place.
export const TELEMETRY_DOCS_URL = 'https://www.codeburn.app/telemetry'
/// The events the pages send. Rust drops anything not on its own list, so this type is here
/// to catch a typo at build time rather than to be the gate.
export type TelemetryEvent =
| 'popover_open'
| 'settings_open'
| 'update_click'
| 'glance_open'
| 'dock_provider_switch'
| 'dock_drag_end'
/// Which app made the decision this app is running under. `desktop` means the desktop app's
/// state file is present and answering for both, so the toggle here is a readout.
export type TelemetrySource = 'desktop' | 'app'
export type TelemetryStatus = {
enabled: boolean
onboarded: boolean
source: TelemetrySource
/// The region the default came from, or null when the locale does not name one.
country: string | null
defaultEnabled: boolean
}
/// Fire and forget. A failed report is not worth an error path in the caller: it is a
/// metric, and the queue on the Rust side is what makes one worth keeping at all.
export function track(name: TelemetryEvent, props?: Record<string, unknown>): void {
void invoke('telemetry_track', { name, props: props ?? null }).catch(() => {})
}
/// Null when Rust has not finished setting up, which is only ever the first moment of a
/// launch; the caller shows nothing rather than guessing at a decision.
export async function telemetryStatus(): Promise<TelemetryStatus | null> {
try {
return await invoke<TelemetryStatus | null>('telemetry_status')
} catch {
return null
}
}
export async function setTelemetryEnabled(enabled: boolean): Promise<TelemetryStatus | null> {
try {
return await invoke<TelemetryStatus | null>('telemetry_set_enabled', { enabled })
} catch {
return null
}
}
/// The one-time notice's answer. Both answers count as an answer, which is what stops the
/// notice coming back and what unlocks sending.
export async function completeTelemetryConsent(enabled: boolean): Promise<TelemetryStatus | null> {
try {
return await invoke<TelemetryStatus | null>('telemetry_consent', { enabled })
} catch {
return null
}
}