alepha@docs:~/docs/reference/primitives$
cat $analytics.md | pretty1 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 retention: { hot: "60d", rollup: "day", cold: "400d" }, 7 }); 8 9 async onPageView(app: string, path: string, country: string) {10 await this.views.record({ app, path, country, count: 1 });11 }12 13 async topPaths(app: string) {14 return this.views.query({15 since: "2026-01-01",16 where: { app },17 groupBy: ["path"],18 select: { count: "sum" },19 orderBy: { key: "count", direction: "desc" },20 limit: 20,21 });22 }23}