@vllnt/convex-analytics 0.1.0-canary.a68a60c → 0.1.0-canary.e8e8f2b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/LICENSE +0 -21
  3. package/README.md +0 -209
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vllnt/convex-analytics",
3
- "version": "0.1.0-canary.a68a60c",
3
+ "version": "0.1.0-canary.e8e8f2b",
4
4
  "description": "Generic, configurable analytics component for Convex — any event, any dimension, any domain; rollup-on-write with funnels, retention, uniques",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -39,7 +39,7 @@
39
39
  "LICENSE"
40
40
  ],
41
41
  "scripts": {
42
- "build": "rm -rf dist && tsc && cp ../../README.md ../../LICENSE ./",
42
+ "build": "rm -rf dist && tsc",
43
43
  "test": "vitest run",
44
44
  "test:watch": "vitest",
45
45
  "test:coverage": "vitest run --coverage",
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 bntvllnt
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/README.md DELETED
@@ -1,209 +0,0 @@
1
- <!-- Badges -->
2
- [![convex-component](https://img.shields.io/badge/convex-component-EE342F.svg)](https://www.convex.dev/components)
3
- [![npm](https://img.shields.io/npm/v/@vllnt/convex-analytics.svg)](https://www.npmjs.com/package/@vllnt/convex-analytics)
4
- [![CI](https://github.com/vllnt/convex-analytics/actions/workflows/ci.yml/badge.svg)](https://github.com/vllnt/convex-analytics/actions/workflows/ci.yml)
5
- [![license](https://img.shields.io/npm/l/@vllnt/convex-analytics.svg)](LICENSE)
6
-
7
- # @vllnt/convex-analytics
8
-
9
- A generic, configurable, embedded analytics component for Convex — any event,
10
- any dimension, any domain — with rollup-on-write so reads stay O(1).
11
-
12
- ```ts
13
- import { AnalyticsClient } from "@vllnt/convex-analytics";
14
- import { components } from "./_generated/api";
15
-
16
- const analytics = new AnalyticsClient(components.analytics, {
17
- dimensions: ["plan", "country"], // prop keys to roll up on
18
- });
19
-
20
- await analytics.track(ctx, "signup", { subjectRef: userId, props: { plan: "pro", country: "FR" } });
21
- const byPlan = await analytics.top(ctx, "signup", "plan"); // [{ value: "pro", count: 1 }]
22
- ```
23
-
24
- ## Features
25
-
26
- - **Generic events** — free-string event name, opaque `subjectRef` / `sessionRef`, host-typed `props`.
27
- - **Host-declared dimensions** — you pass the prop keys to roll up on; nothing is hardcoded.
28
- - **Rollup-on-write** — counts are incremented as events land, so `metric` / `top` / `timeseries` read in O(1) (backed by `@convex-dev/aggregate` + `@convex-dev/sharded-counter`).
29
- - **Rich verb set** — `metric`, `top`, `timeseries`, `uniques`, `funnel`, `retention`, `distribution`, plus paginated raw `list`.
30
- - **Configurable** — `scope`, `dimensions`, `granularities`, `retentionDays`, `sampleRate`, `propsValidator`; sensible defaults, zero config required.
31
- - **Per-session rate limiting + dedupe + sampling** built into `track`.
32
- - **Opt-in web preset** — `@vllnt/convex-analytics/web` adds web dimensions + UA/geo helpers when you want them.
33
- - **Optional React hooks** — `@vllnt/convex-analytics/react` for reactive aggregate reads.
34
- - **Zero hardcoded domain** — no `v.any()`, no baked-in web fields; the host owns meaning and auth.
35
-
36
- ## Generic core vs web preset
37
-
38
- The core is domain-neutral: it knows nothing about web, mobile, or any vertical.
39
- Web analytics — pageview dimensions (`path`, `referrer`, `device`, `browser`, `os`,
40
- `country`, UTM) plus `parseUserAgent` / `geoFromHeaders` / `trackPageview` helpers —
41
- lives in the opt-in `@vllnt/convex-analytics/web` preset. You turn it on by passing
42
- `webDimensions` to the client's `dimensions` config; otherwise the core ships no web
43
- fields. See [docs/web-preset.md](docs/web-preset.md).
44
-
45
- ## Installation
46
-
47
- ```bash
48
- npm install @vllnt/convex-analytics
49
- ```
50
-
51
- ## Usage
52
-
53
- ```ts
54
- // convex/convex.config.ts
55
- import { defineApp } from "convex/server";
56
- import analytics from "@vllnt/convex-analytics/convex.config";
57
-
58
- const app = defineApp();
59
- app.use(analytics);
60
- export default app;
61
- ```
62
-
63
- ```ts
64
- // convex/analytics.ts — track + read, server-side
65
- import { AnalyticsClient } from "@vllnt/convex-analytics";
66
- import { components } from "./_generated/api";
67
-
68
- type MyProps = { plan?: string; country?: string };
69
-
70
- const analytics = new AnalyticsClient<MyProps>(components.analytics, {
71
- dimensions: ["plan", "country"],
72
- granularities: ["day"],
73
- });
74
-
75
- await analytics.track(ctx, "signup", { subjectRef: userId, props: { plan: "pro" } });
76
- const total = await analytics.metric(ctx, "signup");
77
- ```
78
-
79
- ## Config
80
-
81
- Passed to the `AnalyticsClient` constructor. All optional — the defaults work with zero config.
82
-
83
- | Option | Type | Default | Purpose |
84
- |--------|------|---------|---------|
85
- | `scope` | `string` | `"default"` | Multi-tenant partition; isolates one tenant's data. |
86
- | `dimensions` | `string[]` | `[]` | Prop keys to roll up on (drives rollup-on-write). Empty = count by event name only. |
87
- | `granularities` | `("minute" \| "hour" \| "day")[]` | `["day"]` | Rollup bucket sizes (`minute` for short live windows). |
88
- | `retentionDays` | `number` | `90` | Raw-event TTL in days (rollups are kept forever). Applied via `configure`. |
89
- | `sampleRate` | `number` | `1` | Fraction `0..1` of events to keep at ingest. |
90
- | `sessionIdleMs` | `number` | `1800000` | Idle timeout before a session is closed. Applied via `configure`. |
91
- | `propsValidator` | Convex validator | typed scalar record | Optional host validator narrowing `props` at the boundary. |
92
-
93
- `scope`, `dimensions`, `granularities`, and `sampleRate` apply per call from the client.
94
- `retentionDays`, `sampleRate`, and `sessionIdleMs` are persisted for the prune/close-sessions
95
- crons by calling `analytics.configure(ctx, { ... })` once.
96
-
97
- ## API
98
-
99
- | Verb | Kind | Purpose |
100
- |------|------|---------|
101
- | `track(ctx, name, opts)` | mutation | Ingest an event; rollup-on-write + raw event + counter. Returns `"tracked" \| "dropped" \| "duplicate"`. |
102
- | `metric(ctx, name, opts)` | query | Total count over a range, optionally filtered by a dimension value. |
103
- | `top(ctx, name, dimension, opts)` | query | Top values of a dimension (breakdown). |
104
- | `timeseries(ctx, name, opts)` | query | Bucketed counts over a range. |
105
- | `uniques(ctx, opts)` | query | DAU / WAU / MAU from subjects. |
106
- | `funnel(ctx, steps, opts)` | query | Ordered step conversion, keyed by `subjectRef`. |
107
- | `retention(ctx, opts)` | query | Cohort return rates by first-seen period. |
108
- | `distribution(ctx, name, measure, opts)` | query | Histogram of a numeric measure over declared buckets + overflow (with `count` / `sum`). |
109
- | `list(ctx, name, paginationOpts, opts)` | query | Paginated raw events, newest first. |
110
- | `configure(ctx, opts)` | mutation | Persist cron-relevant config (retention / sampling / idle). |
111
-
112
- Full reference: [docs/client-sdk.md](docs/client-sdk.md). REST surface: [docs/api-reference.md](docs/api-reference.md).
113
-
114
- ## What it serves
115
-
116
- The Convex-native, authoritative in-app aggregate layer — predefined-dimension rollups served
117
- **reactively** in O(1). It complements a product-analytics warehouse; it doesn't replace one.
118
- Declare the dimensions and measures you'll query and the rollups answer instantly; ad-hoc or
119
- retroactive questions over **non-declared** dimensions, or beyond the raw-event retention window,
120
- are an export concern. `metric` / `top` / `timeseries` read pre-aggregated rollups (kept forever);
121
- `funnel` / `retention` / `distribution` read raw events in range (index-backed, bounded, TTL-pruned).
122
-
123
- ## Web preset
124
-
125
- ```ts
126
- import { AnalyticsClient } from "@vllnt/convex-analytics";
127
- import { webDimensions, trackPageview } from "@vllnt/convex-analytics/web";
128
-
129
- const analytics = new AnalyticsClient(components.analytics, { dimensions: webDimensions });
130
-
131
- await trackPageview(analytics, ctx, {
132
- path: "/pricing",
133
- ua: request.headers.get("user-agent") ?? undefined,
134
- headers: request.headers,
135
- });
136
- ```
137
-
138
- `webDimensions` opts the rollups into `path` / `referrer` / `device` / `browser` / `os` /
139
- `country` / UTM; `parseUserAgent` and `geoFromHeaders` build those props from a request.
140
- See [docs/web-preset.md](docs/web-preset.md).
141
-
142
- ## React
143
-
144
- ```tsx
145
- import { useMetric, useTop } from "@vllnt/convex-analytics/react";
146
- import { api } from "../convex/_generated/api";
147
-
148
- const signups = useMetric(api.analytics.metric, { name: "signup" });
149
- const byPlan = useTop(api.analytics.top, { name: "signup", dimension: "plan" });
150
- ```
151
-
152
- Thin reactive wrappers over the host's re-exported query refs (`metric`, `top`, `timeseries`,
153
- `uniques`). `react` + `convex` are optional peer deps; backend-only consumers pull no React.
154
-
155
- ## MCP (Claude Code)
156
-
157
- ```bash
158
- claude mcp add convex-analytics-mcp \
159
- --env CONVEX_URL=https://your-deployment.convex.cloud \
160
- --env ANALYTICS_API_KEY=your-key
161
- ```
162
-
163
- 7 tools: `track`, `get_metric`, `get_top`, `get_timeseries`, `get_uniques`, `detect_anomalies`,
164
- `query_analytics` (NL router). See [docs/mcp-tools.md](docs/mcp-tools.md).
165
-
166
- ## Security
167
-
168
- - Auth-agnostic mount — the host gates access and passes opaque `subjectRef` / `sessionRef` in.
169
- - Tables are sandboxed; the host reaches them only through the exported functions.
170
- - REST endpoints require an `x-api-key` header, compared timing-safe.
171
-
172
- See [docs/architecture.md](docs/architecture.md).
173
-
174
- ## Testing
175
-
176
- ```bash
177
- pnpm test
178
- ```
179
-
180
- ```ts
181
- import analyticsTest from "@vllnt/convex-analytics/test";
182
- import { convexTest } from "convex-test";
183
-
184
- const t = convexTest(hostSchema, hostModules);
185
- analyticsTest.register(t);
186
- ```
187
-
188
- ## Documentation
189
-
190
- [Schema](docs/schema.md) · [Client SDK](docs/client-sdk.md) · [REST API](docs/api-reference.md) ·
191
- [Web Preset](docs/web-preset.md) · [Multi-Product](docs/multi-product.md) ·
192
- [Architecture](docs/architecture.md) · [MCP Tools](docs/mcp-tools.md). For AI agents:
193
- [`llms.txt`](llms.txt) · [`llms-full.txt`](llms-full.txt).
194
-
195
- ## Contributing
196
-
197
- See [CONTRIBUTING.md](CONTRIBUTING.md).
198
-
199
- ## Author
200
-
201
- Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
202
-
203
- Part of the [@vllnt](https://github.com/vllnt) Convex component fleet — [vllnt.com](https://vllnt.com)
204
-
205
- If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
206
-
207
- ## License
208
-
209
- MIT — see [LICENSE](LICENSE).