alepha@docs:~/docs/framework/reference/primitives$
cat $analytics.md | pretty
1 min read

#$analytics

#Import

typescript
1import { $analytics } from "alepha/api/analytics";

#Overview

Declares an analytics dataset: what you record, and what you can ask.

The same declaration runs on Workers Analytics Engine, on a relational database and in memory. Which one is bound is a runtime decision made by the module, so app code never names a backend.

#Options

Option Type Required Description
name string No Storage-facing dataset name

#Examples

ts
 1class PageViews { 2  views = $analytics({ 3    index: "app", 4    dimensions: z.object({ app: z.text(), path: z.text(), country: z.text() }), 5    measures: z.object({ count: z.integer() }), 6    // The wire format, append only: a new name goes on the END. 7    slots: { dimensions: ["app", "path", "country"], measures: ["count"] }, 8    retention: { hot: "60d", rollup: "day", cold: "400d" }, 9  });10 11  async onPageView(app: string, path: string, country: string) {12    await this.views.record({ app, path, country, count: 1 });13  }14 15  async topPaths(app: string) {16    return this.views.query({17      since: "2026-01-01",18      where: { app },19      groupBy: ["path"],20      select: { count: "sum" },21      orderBy: { key: "count", direction: "desc" },22      limit: 20,23    });24  }25}