-
Notifications
You must be signed in to change notification settings - Fork 611
feat: analytics #4068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: analytics #4068
Changes from all commits
ebc5be6
57867a3
8945cc6
53adcc1
94fd640
66e4d92
8e9bdef
09c3ccb
360e0be
d9f8d67
123636a
adea30f
d66e1a1
5257d84
cf31846
a8b1158
82bffbe
ff7814b
6319210
37c298d
6f4d004
272b32c
2969ebb
5a6e0c4
1aaf038
4f5a6bb
c686139
0ea4027
eadfb81
269e56f
d42d5aa
2685606
182a0aa
d202637
e915c1e
a9d13fc
748a478
9555c6a
dc1fefc
5ecea3e
7167d2c
ac90686
4bd1c7a
e45251e
30a9c57
97bd065
5e72c17
8a24680
c27a1fc
53de9d1
db88452
5fb3036
cf4fcd0
e955e0a
02fb3b9
c83b88e
e123d7b
9589bdf
3e29276
2fcc3fe
c0c5c52
1929ee3
8219188
cd406ad
a4dbfe1
00adfff
76e5b00
616edd6
e7239f9
e46fa79
b2ee63e
71b07e5
6859704
68b282a
2b567b5
0241db8
58af255
7568a68
4425186
91954b6
84401e9
95dbf04
7cf1e61
a4f6c12
a5be3fb
f0cb0c6
b737958
b7ae657
ea94b68
77aa422
8026fbc
d77d3e2
0e773a7
972eb87
fa2bf01
1ca4e74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { SettingCard } from "@unkey/ui"; | ||
| import { CopyButton } from "@unkey/ui"; | ||
|
|
||
| export const CopyKeySpaceId = ({ keySpaceId }: { keySpaceId: string }) => { | ||
| return ( | ||
| <SettingCard | ||
| title={"KeySpace ID"} | ||
| description={<div className="max-w-[380px]">Identifier for the underlying keyspace.</div>} | ||
| border="bottom" | ||
| contentWidth="w-full lg:w-[420px] justify-end" | ||
| > | ||
| {/* TODO: make this a Code component in UI for CopyKeys with optional hidden button like in Code.*/} | ||
| <div className="flex flex-row justify-end items-center"> | ||
| <div | ||
| className={ | ||
| "flex flex-row justify-between min-w-[327px] pl-4 pr-2 py-1.5 bg-gray-2 dark:bg-black border rounded-lg border-grayA-5" | ||
| } | ||
| > | ||
| <div className="text-sm text-gray-11">{keySpaceId}</div> | ||
| <CopyButton value={keySpaceId} variant="ghost" toastMessage={keySpaceId} /> | ||
| </div> | ||
| </div> | ||
| </SettingCard> | ||
| ); | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth to mention that queries supports CH functions and other utils of CH. I didn't see that so that might come in handy for users.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
do you mean them? or where would you want to mention it instead? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| --- | ||
| title: Getting Started | ||
| description: "Request access and run your first analytics query" | ||
| --- | ||
|
|
||
| ## Request Access | ||
|
|
||
| <Warning> | ||
| **Analytics is currently in private beta and available by request only.** | ||
| </Warning> | ||
|
|
||
| To get started: | ||
|
|
||
| 1. **Find your workspace ID** in the Unkey dashboard settings | ||
| 2. **Email us** at [support@unkey.dev](mailto:support@unkey.dev) with: | ||
| - Your workspace ID | ||
| - Your use case (billing, dashboards, reporting, etc.) | ||
| - Expected query volume | ||
|
|
||
| We'll enable analytics for your workspace and send you confirmation. | ||
|
|
||
| ## Authentication | ||
|
|
||
| Analytics queries require a root key with analytics permissions. Create one in your dashboard: | ||
|
|
||
| 1. Go to **Settings** → **Root Keys** | ||
| 2. Click **Create New Root Key** | ||
| 3. Select permissions: `api.*.read_analytics` OR `api.<api_id>.read_analytics` | ||
| 4. Copy and securely store your root key | ||
|
|
||
| <Warning> | ||
| Root keys have powerful permissions. Store them securely and never commit them | ||
| to version control. | ||
| </Warning> | ||
|
|
||
| ## Your First Query | ||
|
|
||
| Once you have access, execute your first analytics query using the `/v2/analytics.getVerifications` endpoint. | ||
|
|
||
| ### Count Total Verifications | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```sql | ||
| SELECT COUNT(*) as total | ||
| FROM key_verifications_v1 | ||
| WHERE time >= now() - INTERVAL 7 DAY | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| Execute this query with curl: | ||
|
|
||
| ```bash | ||
| curl -X POST https://api.unkey.com/v2/analytics.getVerifications \ | ||
| -H "Authorization: Bearer <YOUR_ROOT_KEY>" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "query": "SELECT COUNT(*) as total FROM key_verifications_v1 WHERE time >= now() - INTERVAL 7 DAY" | ||
| }' | ||
| ``` | ||
|
|
||
| ### Break Down by Outcome | ||
|
|
||
| ```sql | ||
| SELECT | ||
| outcome, | ||
| COUNT(*) as count | ||
| FROM key_verifications_v1 | ||
| WHERE time >= now() - INTERVAL 24 HOUR | ||
| GROUP BY outcome | ||
| ORDER BY count DESC | ||
| ``` | ||
|
|
||
| Execute this query with curl: | ||
|
|
||
| ```bash | ||
| curl -X POST https://api.unkey.com/v2/analytics.getVerifications \ | ||
| -H "Authorization: Bearer <YOUR_ROOT_KEY>" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "query": "SELECT outcome, COUNT(*) as count FROM key_verifications_v1 WHERE time >= now() - INTERVAL 24 HOUR GROUP BY outcome ORDER BY count DESC" | ||
| }' | ||
| ``` | ||
|
|
||
| ### Top Users by Usage | ||
|
|
||
| ```sql | ||
| SELECT | ||
| external_id, | ||
| SUM(count) as verifications | ||
| FROM key_verifications_per_day_v1 | ||
| WHERE time >= now() - INTERVAL 30 DAY | ||
| GROUP BY external_id | ||
| ORDER BY verifications DESC | ||
| LIMIT 10 | ||
| ``` | ||
|
|
||
| Execute this query with curl: | ||
|
|
||
| ```bash | ||
| curl -X POST https://api.unkey.com/v2/analytics.getVerifications \ | ||
| -H "Authorization: Bearer <YOUR_ROOT_KEY>" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "query": "SELECT external_id, SUM(count) as verifications FROM key_verifications_per_day_v1 WHERE time >= now() - INTERVAL 30 DAY GROUP BY external_id ORDER BY verifications DESC LIMIT 10" | ||
| }' | ||
| ``` | ||
|
|
||
| <Tip> | ||
| **Performance tip:** For longer time ranges, use pre-aggregated tables instead of the raw table: | ||
| - `key_verifications_per_minute_v1` - For queries spanning hours | ||
| - `key_verifications_per_hour_v1` - For queries spanning days | ||
| - `key_verifications_per_day_v1` - For queries spanning weeks/months | ||
| - `key_verifications_per_month_v1` - For queries spanning years | ||
|
|
||
| Use `SUM(count)` instead of `COUNT(*)` with aggregated tables. They scan far fewer rows and are much faster. | ||
|
|
||
| </Tip> | ||
|
|
||
| <Tip> | ||
| Check out the [Query Examples](/analytics/query-examples) page for 30+ | ||
| ready-to-use queries covering billing, monitoring, and analytics use cases. | ||
| </Tip> | ||
|
|
||
| ## Understanding the Response | ||
|
|
||
| Analytics queries return data as an array of objects: | ||
|
|
||
| ```json | ||
| { | ||
| "meta": { | ||
| "requestId": "req_xxx" | ||
| }, | ||
| "data": [ | ||
| { "outcome": "VALID", "count": 1234 }, | ||
| { "outcome": "RATE_LIMITED", "count": 56 }, | ||
| { "outcome": "USAGE_EXCEEDED", "count": 12 } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| Each object in the `data` array contains fields from your SELECT clause. The field names match the column names or aliases you specified in your query. | ||
|
|
||
| ## Filtering by API or User | ||
|
|
||
| You can filter queries to specific APIs or users. Use `key_space_id` to filter by API (find this identifier in your API settings) and `external_id` to filter by user. These fields support standard SQL operators: `=`, `!=`, `IN`, `NOT IN`, `<`, `>`, etc. | ||
|
|
||
| <Note> | ||
| Queries are subject to resource limits (execution time, memory, result size, | ||
| and quota). See [Query Restrictions](/analytics/query-restrictions) for | ||
| complete details on limits and error codes. | ||
| </Note> | ||
|
|
||
| ## Next Steps | ||
|
|
||
| <CardGroup cols={2}> | ||
| <Card title="Query Examples" icon="code" href="/analytics/query-examples"> | ||
| Explore common SQL patterns for analytics and billing | ||
| </Card> | ||
| <Card | ||
| title="Schema Reference" | ||
| icon="table" | ||
| href="/analytics/schema-reference" | ||
| > | ||
| Browse available tables, columns, and data types | ||
| </Card> | ||
| <Card | ||
| title="Query Restrictions" | ||
| icon="shield-check" | ||
| href="/analytics/query-restrictions" | ||
| > | ||
| View limits, quotas, and permissions | ||
| </Card> | ||
| </CardGroup> | ||
Uh oh!
There was an error while loading. Please reload this page.