@vllnt/convex-analytics 0.2.0-canary.d5fa959 → 0.2.0-canary.dee16f7

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 (58) hide show
  1. package/CHANGELOG.md +113 -49
  2. package/README.md +461 -157
  3. package/dist/client/index.d.ts +34 -12
  4. package/dist/client/index.d.ts.map +1 -1
  5. package/dist/client/index.js +34 -12
  6. package/dist/client/index.js.map +1 -1
  7. package/dist/client/types.d.ts +9 -5
  8. package/dist/client/types.d.ts.map +1 -1
  9. package/dist/component/convex.config.d.ts +1 -0
  10. package/dist/component/convex.config.d.ts.map +1 -1
  11. package/dist/component/convex.config.js +1 -0
  12. package/dist/component/convex.config.js.map +1 -1
  13. package/dist/component/internal_mutations.d.ts +1 -1
  14. package/dist/component/internal_mutations.d.ts.map +1 -1
  15. package/dist/component/internal_mutations.js +16 -6
  16. package/dist/component/internal_mutations.js.map +1 -1
  17. package/dist/component/mutations.d.ts.map +1 -1
  18. package/dist/component/mutations.js +22 -20
  19. package/dist/component/mutations.js.map +1 -1
  20. package/dist/component/queries.d.ts.map +1 -1
  21. package/dist/component/queries.js +67 -17
  22. package/dist/component/queries.js.map +1 -1
  23. package/dist/component/validators.d.ts +15 -1
  24. package/dist/component/validators.d.ts.map +1 -1
  25. package/dist/component/validators.js +32 -0
  26. package/dist/component/validators.js.map +1 -1
  27. package/dist/react/index.d.ts +2 -3
  28. package/dist/react/index.d.ts.map +1 -1
  29. package/dist/react/index.js +2 -3
  30. package/dist/react/index.js.map +1 -1
  31. package/dist/test.d.ts +3 -1
  32. package/dist/test.d.ts.map +1 -1
  33. package/dist/test.js +3 -1
  34. package/dist/test.js.map +1 -1
  35. package/dist/web/index.d.ts +3 -2
  36. package/dist/web/index.d.ts.map +1 -1
  37. package/dist/web/index.js +2 -1
  38. package/dist/web/index.js.map +1 -1
  39. package/docs/api-reference.md +29 -10
  40. package/docs/architecture.md +33 -20
  41. package/docs/client-sdk.md +358 -140
  42. package/docs/multi-product.md +51 -40
  43. package/docs/quick-start.md +67 -70
  44. package/docs/react.md +146 -0
  45. package/docs/schema.md +13 -9
  46. package/docs/web-preset.md +96 -61
  47. package/llms.txt +43 -25
  48. package/package.json +14 -10
  49. package/src/client/index.ts +34 -12
  50. package/src/client/types.ts +9 -5
  51. package/src/component/convex.config.ts +1 -0
  52. package/src/component/internal_mutations.ts +16 -6
  53. package/src/component/mutations.ts +26 -20
  54. package/src/component/queries.ts +68 -16
  55. package/src/component/validators.ts +39 -0
  56. package/src/react/index.tsx +2 -3
  57. package/src/test.ts +3 -1
  58. package/src/web/index.ts +3 -2
@@ -2,11 +2,12 @@
2
2
 
3
3
  A lean, generic HTTP surface. Routes are namespaced under the host's chosen `httpPrefix`
4
4
  when the component is mounted with one. Every route requires an `x-api-key` header matching
5
- the JSON-array config at `(scope, "apiKeys")`, compared timing-safe.
5
+ the JSON-array config at `(scope, "apiKeys")`. The comparison does not stop at the first
6
+ differing character; it returns early only when the lengths differ.
6
7
 
7
8
  Mount with `app.use(analytics, { httpPrefix: "/analytics" })`; `/metric` below then means
8
9
  `https://<deployment>.convex.site/analytics/metric`. Without `httpPrefix`, these routes are not
9
- exposed. See [Quick Start](quick-start.md#6-optional-rest-access) for key setup through the
10
+ exposed. See [Quick Start](quick-start.md#4-optional-rest-access) for key setup through the
10
11
  component's `mutations.configSet` (there is no `AnalyticsClient.configSet` method).
11
12
 
12
13
  Source: `src/component/http.ts` in the package (`packages/convex-analytics/src/component/http.ts`
@@ -41,11 +42,22 @@ Ingest one event.
41
42
  | `sampleRate` | No | number | `0..1`. |
42
43
  | `scope` | No | string | Partition. Default `"default"`. |
43
44
 
45
+ A `scope` that is not a string, or is `""`, is read as `"default"` (not rejected), on every
46
+ route. `POST /track` checks the body in this order: invalid JSON (`400 Invalid JSON body`) and a
47
+ body that is not a JSON object (`400 Body must be a JSON object`) are rejected before the API
48
+ key is checked; every other body check runs after it.
49
+
50
+ `subjectRef`, `sessionRef`, `dedupeKey` (strings) and `ts`, `sampleRate` (numbers) are ignored,
51
+ not rejected, when they have another type.
52
+
44
53
  **Response**: `201` with `{ "result": "tracked" | "dropped" | "duplicate" }`.
45
54
  A 201 does not imply storage: inspect `result`. With `sessionRef`, a fixed token bucket per
46
55
  `(scope, sessionRef)` refills at 100/minute with capacity 10; exhausted requests return
47
56
  `"dropped"` (not HTTP 429). Sampling also returns `"dropped"`. No `sessionRef` means no
48
- session rate-limit check.
57
+ session rate-limit check. A `name`, ref, `dedupeKey` or `dimensions` entry over 512 characters
58
+ (Unicode code points) returns `400` with code `STRING_TOO_LONG`. A `scope` that long usually
59
+ gets `401 API keys not configured` first, because `configSet` refuses to store its `apiKeys`
60
+ row; an `apiKeys` row stored for it before 0.2.0 passes the key check, and then `400` follows.
49
61
 
50
62
  ## GET /metric
51
63
 
@@ -53,7 +65,7 @@ session rate-limit check.
53
65
  | ------------ | -------- | ------------------------------------------------------- |
54
66
  | `name` | Yes | Event name. |
55
67
  | `from`, `to` | No | Range bounds (epoch ms). |
56
- | `dim`, `val` | No | Filter to one dimension value (both required together). |
68
+ | `dim`, `val` | No | Filter to one dimension value. Ignored unless both are set. |
57
69
  | `scope` | No | Partition. |
58
70
 
59
71
  **Response**: `{ "name": "...", "count": N }`.
@@ -86,15 +98,17 @@ session rate-limit check.
86
98
  | Param | Required | Description |
87
99
  | ------------- | -------- | -------------------------------------------- |
88
100
  | `granularity` | No | `minute` \| `hour` \| `day` (default `day`). |
89
- | `from`, `to` | No | Range bounds (epoch ms). |
101
+ | `from`, `to` | No | Range bounds (epoch ms). Default: the 30 days before `to` (or now). |
90
102
  | `scope` | No | Partition. |
91
103
 
92
104
  **Response**: `{ "dau": N, "wau": N, "mau": N, "trend": [{ "bucket": N, "uniques": N }] }`.
105
+ See [`uniques`](client-sdk.md#uniques) for how each number is computed.
93
106
 
94
107
  ## Auth & errors
95
108
 
96
109
  - Configure keys by writing a JSON string array to config `(scope, "apiKeys")` (the `apiKeys` config key).
97
- - Missing or invalid key → `401 { "error": "..." }`.
110
+ - Missing key, no `apiKeys` for the scope (or a stored `""`), or a key not in the list →
111
+ `401 { "error": "..." }`.
98
112
  - Invalid input returns `400`. Typed errors from the table below (for example a non-numeric
99
113
  `from`, `to`, or `limit`, nested or array `props` values, prop keys Convex cannot store)
100
114
  return `{ "error": "...", "code": "..." }`. Checks done in the route itself return
@@ -104,23 +118,28 @@ session rate-limit check.
104
118
  Any other failure (for example a Convex argument-validator error) still fails the action.
105
119
  - A stored `apiKeys` value that is not a JSON array of strings returns `500 { "error": "API keys
106
120
  misconfigured" }`.
121
+ - `POST /track` returns `500` when a burst of parallel calls runs out of Convex's optimistic
122
+ concurrency retries. That body carries no `error` or `code` from this package, the event is not
123
+ stored, and the call is safe to retry.
107
124
 
108
125
  ## Input errors
109
126
 
110
127
  Mutations and queries reject clearly invalid input with a `ConvexError` whose data is
111
- `{ code, message }`. Catch it and branch on `code`:
128
+ `{ code, message }`. Catch it and branch on `code`. Every error, with its fix, is in the README
129
+ [Errors and fixes](../README.md#errors-and-fixes) table.
112
130
 
113
131
  | Code | Raised when |
114
132
  | --------------------- | ------------------------------------------------------------------------------------------------- |
115
- | `EMPTY_STRING` | `scope`, `name`, `subjectRef`, `sessionRef`, `dedupeKey`, a dimension, or a config `key` is `""`. |
133
+ | `EMPTY_STRING` | A `scope`, `name`, ref, `dedupeKey`, dimension, funnel step, measure, or config `key` is `""`. |
134
+ | `STRING_TOO_LONG` | A write gets a `scope`, `name`, ref, `dedupeKey`, dimension or config `key` over 512 characters. |
116
135
  | `INVALID_TIMESTAMP` | `ts`, or a range `from` / `to`, is `NaN` or infinite. |
117
136
  | `INVALID_SAMPLE_RATE` | `sampleRate` is not a number from 0 to 1. |
118
137
  | `INVALID_PROP_VALUE` | A prop is `NaN`, infinite, an object, or an array. |
119
138
  | `INVALID_PROP_KEY` | A prop key is not printable ASCII, is over 1024 characters, or starts with `$` (HTTP). |
120
139
  | `INVALID_COUNT` | `limit`, `periods`, or `numItems` is not a positive integer. |
121
- | `INVALID_CURSOR` | A `list` cursor is not one returned by `list`. |
140
+ | `INVALID_CURSOR` | A `list` cursor is `""`, blank, or not in the form `list` returns. |
122
141
  | `INVALID_BUCKETS` | A `distribution` bucket bound is `NaN` or infinite. |
123
- | `INVALID_CONFIG` | `retentionDays` or `sessionIdleMs` is not a positive finite number. |
142
+ | `INVALID_CONFIG` | `retentionDays` / `sessionIdleMs` is not a positive finite number, or a `configSet` `sampleRate` is outside 0 to 1. |
124
143
 
125
144
  Everything the component stores comes from the host: `props`, `subjectRef`, and so on are
126
145
  stored as given, and the component never logs event data. Keep personal data (emails, IP
@@ -6,10 +6,17 @@
6
6
  fixed-shard counter; ranged/filtered metrics, top values, and time series scan matching rollup
7
7
  rows. They avoid raw-event scans but are not generally O(1).
8
8
 
9
- ```
9
+ Every `track` of one `(scope, name)` updates the same total rollup row for each bucket and the
10
+ same lifetime counter, and calls that share one `sessionRef` or `subjectRef` update that
11
+ session's rate-limit bucket and its session or subject row, so write throughput per
12
+ `(scope, name)` is limited by these shared rows. Under a burst of
13
+ parallel calls, some fail with an optimistic concurrency error and the event is not stored (REST:
14
+ `POST /track` returns `500`): retry failed calls or queue ingest.
15
+
16
+ ```text
10
17
  track(ctx, name, opts)
11
18
  |
12
- +- 1. Rate limit (when sessionRef set: per-session token bucket) -> "dropped" if over
19
+ +- 1. Rate limit (when sessionRef set: token bucket per (scope, sessionRef)) -> "dropped" if over
13
20
  |
14
21
  +- 2. Sampling (when sampleRate < 1) -> "dropped" for the sampled-out fraction
15
22
  |
@@ -22,8 +29,8 @@ track(ctx, name, opts)
22
29
  |
23
30
  +- 6. Upsert subject (when subjectRef set: firstSeen/lastSeen/eventCount)
24
31
  |
25
- +- 7. Rollup-on-write: for each granularity bucket, increment the total row
26
- | and one row per host-declared dimension present in props
32
+ +- 7. Rollup-on-write: for each distinct granularity bucket, increment the total row
33
+ | and one row per distinct host-declared dimension present in props
27
34
  |
28
35
  +- 8. Sharded counter increment (keyed by (scope, name))
29
36
  |
@@ -38,30 +45,36 @@ unbounded total) — `metric`, `top`, and `timeseries` never scan raw events. `f
38
45
 
39
46
  | Component | Package | Role |
40
47
  | -------------- | ----------------------------- | -------------------------------------------------- |
41
- | aggregate | `@convex-dev/aggregate` | Mounted for compatibility; not written or read. |
48
+ | aggregate | `@convex-dev/aggregate` | Mounted; not written or read. |
42
49
  | shardedCounter | `@convex-dev/sharded-counter` | O(1) total per `(scope, name)` (16 shards). |
43
- | rateLimiter | `@convex-dev/rate-limiter` | Per-`sessionRef` token bucket (100/min, burst 10). |
44
-
45
- Mounted in `convex.config.ts`:
50
+ | rateLimiter | `@convex-dev/rate-limiter` | Token bucket per `(scope, sessionRef)` (100/min, burst 10). |
46
51
 
47
- ```ts
48
- const component = defineComponent("analytics");
49
- component.use(aggregate);
50
- component.use(shardedCounter);
51
- component.use(rateLimiter);
52
- ```
52
+ All three are mounted in `src/component/convex.config.ts` with `component.use(...)`.
53
53
 
54
54
  ## Crons
55
55
 
56
56
  Both run inside the component, are idempotent, and visit every scope in the `scopes` table.
57
57
 
58
- | Cron | Schedule | What it does |
59
- | --------------- | -------- | ----------------------------------------------------------------------------------------------- |
60
- | `prune` | 24 hours | Deletes raw events older than `retentionDays` per scope (rollups kept forever). Capped per run. |
61
- | `closeSessions` | 30 min | Sets `endTs` on sessions idle past `sessionIdleMs`. |
58
+ The dashboard lists them by cron name; each calls an internal mutation in
59
+ `internal_mutations.ts`.
60
+
61
+ | Cron name | Function | Schedule | What it does |
62
+ | -------------------------- | --------------- | -------- | ---------------------------------------------------------------------------------- |
63
+ | `analytics:prune-events` | `prune` | 24 hours | Deletes raw events older than `retentionDays` per scope; it never touches rollups. |
64
+ | `analytics:close-sessions` | `closeSessions` | 30 min | Sets `endTs` (to the last event time) on sessions idle past `sessionIdleMs`. |
65
+
66
+ Each run handles at most 500 rows per scope and schedules itself again when it hit that cap; scopes
67
+ are visited 100 at a time. `retentionDays` and `sessionIdleMs` come from the `config` table
68
+ (written by `configure`), else 90 days and 30 minutes; a stored value that is not a positive
69
+ finite number is ignored and the default is used.
62
70
 
63
- `backfill` is a non-cron internal mutation that re-derives a `(scope, name)`'s rollups from
64
- the retained raw events — useful after changing dimensions.
71
+ `backfill` is a non-cron internal mutation that deletes and re-derives a `(scope, name)`'s
72
+ rollups from the retained raw events, for example after changing dimensions. Rollups are kept
73
+ forever unless you run `backfill`: it deletes every rollup row of that name and rebuilds them only
74
+ from raw events still retained, so buckets older than `retentionDays` drop to 0. It is not part
75
+ of the host-facing API; run it with
76
+ `npx convex run --component analytics internal_mutations:backfill '{...}'`. It throws when more
77
+ than 50,000 raw events exist for the name. It does not change the lifetime counter.
65
78
 
66
79
  ## Isolation
67
80