# `core/telemetry/telemetry.ts`

<p class="generated-note">Generated from the exports of <a href="https://github.com/altasystem-ai/alta/blob/main/core/telemetry/telemetry.ts"><code>core/telemetry/telemetry.ts</code></a> · <code>npm run docs:gen</code></p>

Telemetry (docs/oss/TELEMETRY.md): opt-in, off until the person answers, minimal. The only network code of the CLI and the server that is not a command the person typed. Shared by `alta` (bundled) and the server (type stripping): erasable TypeScript only. The one module of core/ that uses Node (fs, fetch): the packages leave it out of their type checks (packages/*/tsconfig*.json), and none of them imports it. Nothing is sent unless ALL of these hold: - the person answered "yes" (`alta telemetry on`, the game's question or its setting); no answer = off; - DO_NOT_TRACK is not set (any value but "", "0" or "false" turns telemetry off, whatever the answer); - a project key is configured: ALTA_TELEMETRY_KEY, or `projectKey` in ~/.config/alta/telemetry.json. No key is committed anywhere; without one, this module sends nothing; - the event is in the closed list below, with only its allowed properties. The payload holds: a random install id (made when the person says yes, dropped when they say no), the app ("cli", "server"), its version, the OS family, the event, and for chart.installed the chart's locality. Never a chart id, a quest id, a path, a name, a prompt, a secret, an address or the machine's name. PostHog is told not to look up the address ($geoip_disable) and not to build a person profile; the project must also discard client IPs.

**Standards that cite this file**: No standard page cites this file yet.

## Exports

### `TELEMETRY_SCHEMA` 
```ts
const TELEMETRY_SCHEMA = "alta.telemetry/0.1"
```

### `DEFAULT_HOST` 
PostHog's EU cloud. ALTA_TELEMETRY_HOST replaces it (https only; http://127.0.0.1 only with ALTA_TEST_HTTP=1).

```ts
const DEFAULT_HOST = "https://eu.i.posthog.com"
```

### `CAPTURE_PATH` 
```ts
const CAPTURE_PATH = "/i/v0/e/"
```

### `TIMEOUT_MS` 
```ts
const TIMEOUT_MS = 3000
```

### `TELEMETRY_EVENTS` 
The closed list of events, and the properties each may carry with their allowed values.

```ts
const TELEMETRY_EVENTS: Record<string, Record<string, readonly string[]>>
```

### `APPS` 
```ts
const APPS = ["cli", "server"] as const
```

### `ALLOWED_PROPERTIES` 
Every key a payload's properties may hold: the base fields, the events' own fields, PostHog's two switches.

```ts
const ALLOWED_PROPERTIES = ["app", "version", "os", "locality", "$geoip_disable", "$process_person_profile"]
```

### `Env` 
```ts
type Env = Record<string, string | undefined>;
```

### `Answer` 
```ts
type Answer = "yes" | "no";
```

### `TelemetryState` 
```ts
interface TelemetryState { schema: string; answer?: Answer; installId?: string; answeredAt?: string; projectKey?: string }
```

### `Reason` 
```ts
type Reason = "sent" | "not-answered" | "off" | "do-not-track" | "no-key" | "bad-host" | "unknown-event" | "failed";
```

### `Status` 
```ts
interface Status { enabled: boolean; answer?: Answer; reason: Reason; host: string; file: string }
```

### `telemetryFile` 
```ts
const telemetryFile = (env: Env = process.env) => …
```

### `readState` 
```ts
function readState(env: Env = process.env): TelemetryState
```

### `writeAnswer` 
Records the person's answer. "yes" makes an install id if none; "no" drops it (no identifier is kept).

```ts
function writeAnswer(answer: Answer, env: Env = process.env, now = new Date()): TelemetryState
```

### `answered` 
```ts
const answered = (env: Env = process.env) => …
```

### `doNotTrack` 
DO_NOT_TRACK=1 (https://consoledonottrack.com): any value but "", "0" or "false" turns telemetry off.

```ts
function doNotTrack(env: Env = process.env): boolean
```

### `hostOf` 
```ts
function hostOf(env: Env = process.env): string | null
```

### `status` 
```ts
function status(env: Env = process.env): Status
```

### `osFamily` 
```ts
function osFamily(platform: string = process.platform): string
```

### `cleanProps` 
The properties an event may carry: only those of its row, only their allowed values; anything else is dropped.

```ts
function cleanProps(event: string, props: Record<string, unknown> = {}): Record<string, string> | null
```

### `Payload` 
```ts
interface Payload {
  api_key: string; event: string; distinct_id: string; timestamp: string;
  properties: Record<string, string | boolean>;
}
```

### `payloadFor` 
The exact body that would be sent, or why nothing would be.

```ts
function payloadFor(event: string, props: Record<string, unknown>, ctx: { app: (typeof APPS)[number]; version: string; env?: Env; now?: Date }):
  { payload: Payload; url: string } | { reason: Reason }
```

### `track` 
Sends one event if telemetry is on. Never throws, never waits more than TIMEOUT_MS, never retries.

```ts
async function track(event: string, props: Record<string, unknown>, ctx: { app: (typeof APPS)[number]; version: string; env?: Env; now?: Date; fetchImpl?: typeof fetch }):
  Promise<{ sent: boolean; reason: Reason }>
```
