@metricraven/cli 0.0.0-stage → 0.1.0
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.
- package/README.md +65 -2
- package/dist/api.js +76 -0
- package/dist/args.js +119 -0
- package/dist/commands.js +702 -0
- package/dist/completion.js +63 -0
- package/dist/config.js +48 -0
- package/dist/format.js +67 -0
- package/dist/index.js +113 -0
- package/dist/prompt.js +48 -0
- package/dist/range.js +117 -0
- package/package.json +30 -4
package/README.md
CHANGED
|
@@ -1,3 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# MetricRaven CLI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Your [MetricRaven](https://metricraven.com) analytics from the terminal: traffic, revenue, goals, visitors and websites. Output is readable by default and raw JSON with `--json`, so it also works well for scripts and AI agents.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx @metricraven/cli login # run without installing
|
|
9
|
+
npm install -g @metricraven/cli # or install the `metricraven` command
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Node.js 20 or later.
|
|
13
|
+
|
|
14
|
+
## Log in
|
|
15
|
+
|
|
16
|
+
Create an access token in **Settings → API & MCP** (https://metricraven.com/app/api), then:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
metricraven login # paste the token (hidden)
|
|
20
|
+
echo "$TOKEN" | metricraven login # or pipe it, e.g. in CI
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The token is saved to `~/.config/metricraven/config.json` (or `$XDG_CONFIG_HOME/metricraven/config.json`), readable only by you. `metricraven logout` removes it.
|
|
24
|
+
|
|
25
|
+
## Commands
|
|
26
|
+
|
|
27
|
+
| Command | What it does |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `login`, `logout`, `whoami` | Save, remove or inspect the access token |
|
|
30
|
+
| `websites list` | Websites this token can use |
|
|
31
|
+
| `websites create --name --domain` | Add a website; prints its ID, secret key and tracking script |
|
|
32
|
+
| `use <website_id>` | Set the default website |
|
|
33
|
+
| `analytics traffic` | Visitors, pageviews, revenue, conversion rate and top sources |
|
|
34
|
+
| `analytics breakdown <dimension>` | Full table for `channel`, `referrer`, `page`, `utmSource`, `country`, `device`, … |
|
|
35
|
+
| `analytics revenue` | Payments, renewals, refunds, LTV, revenue by channel |
|
|
36
|
+
| `analytics conversions` | Goals and funnel steps |
|
|
37
|
+
| `analytics goal <name>` | One goal with its sources and properties |
|
|
38
|
+
| `analytics insights` | What converts and how long buyers take |
|
|
39
|
+
| `analytics search` | Google Search Console queries |
|
|
40
|
+
| `analytics crawlers` | AI crawlers and bots |
|
|
41
|
+
| `realtime` | Visitors online now |
|
|
42
|
+
| `visitors list`, `visitors get <id>` | Visitors and journeys (token needs visitor details access) |
|
|
43
|
+
| `funnels list` | Saved funnels |
|
|
44
|
+
| `notes list`, `notes add --text [--date]` | Chart notes (adding needs write access) |
|
|
45
|
+
| `mentions [--platform x\|reddit] [--q text]` | X and Reddit posts mentioning the website (Growth plan) |
|
|
46
|
+
| `completion bash\|zsh\|fish` | Shell completion script |
|
|
47
|
+
|
|
48
|
+
Reports cover the last 30 days by default. Use `--period today|yesterday|24h|7d|30d|90d|6m|1y|wtd|mtd|all`, or `--from 2026-10-01 --to 2026-10-31`, with `--timezone Europe/Madrid` for day boundaries. Filter with `--filter dimension:operator:value` (repeatable; operators `is`, `is_not`, `contains`, `not_contains`).
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
metricraven analytics traffic --period 7d
|
|
52
|
+
metricraven analytics breakdown referrer --sort revenue --limit 20
|
|
53
|
+
metricraven analytics traffic -f country:is:US -f device:is:mobile --json | jq .overview
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Environment
|
|
57
|
+
|
|
58
|
+
| Variable | Meaning |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `METRICRAVEN_TOKEN` | Access token (overrides the saved one) |
|
|
61
|
+
| `METRICRAVEN_SITE` | Default website ID |
|
|
62
|
+
| `METRICRAVEN_API_BASE` | API origin, default `https://metricraven.com` |
|
|
63
|
+
|
|
64
|
+
Global flags: `--site <id>`, `--json`, `--verbose` (request URLs and status codes on stderr), `--help`, `--version`.
|
|
65
|
+
|
|
66
|
+
Full documentation: https://metricraven.com/docs/cli
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** Thin client for MetricRaven's REST API with friendly errors. */
|
|
2
|
+
export class ApiError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
constructor(message, status, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function createClient(opts) {
|
|
12
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
13
|
+
async function request(method, path, params, body) {
|
|
14
|
+
const url = `${opts.apiBase}/api/v1${path}${params && [...params].length ? `?${params}` : ""}`;
|
|
15
|
+
let res;
|
|
16
|
+
try {
|
|
17
|
+
res = await fetchImpl(url, {
|
|
18
|
+
method,
|
|
19
|
+
headers: {
|
|
20
|
+
Authorization: `Bearer ${opts.token}`,
|
|
21
|
+
Accept: "application/json",
|
|
22
|
+
"User-Agent": "metricraven-cli",
|
|
23
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
24
|
+
},
|
|
25
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
26
|
+
signal: AbortSignal.timeout(60_000),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (opts.verbose)
|
|
31
|
+
process.stderr.write(`${method} ${url} → network error\n`);
|
|
32
|
+
const reason = error.name === "TimeoutError" ? "timed out" : "could not connect";
|
|
33
|
+
throw new ApiError(`Request ${reason} (${opts.apiBase}). Check your connection or METRICRAVEN_API_BASE.`, 0);
|
|
34
|
+
}
|
|
35
|
+
if (opts.verbose)
|
|
36
|
+
process.stderr.write(`${method} ${url} → ${res.status}\n`);
|
|
37
|
+
const text = await res.text();
|
|
38
|
+
let data = null;
|
|
39
|
+
try {
|
|
40
|
+
data = text ? JSON.parse(text) : null;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
data = null;
|
|
44
|
+
}
|
|
45
|
+
if (!res.ok)
|
|
46
|
+
throw toError(res.status, data);
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
get: (path, params) => request("GET", path, params),
|
|
51
|
+
post: (path, body, params) => request("POST", path, params, body),
|
|
52
|
+
patch: (path, body, params) => request("PATCH", path, params, body),
|
|
53
|
+
del: (path, params) => request("DELETE", path, params),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function toError(status, data) {
|
|
57
|
+
const body = (data && typeof data === "object" ? data : {});
|
|
58
|
+
const code = typeof body.error === "string" ? body.error : undefined;
|
|
59
|
+
const message = typeof body.message === "string" ? body.message : undefined;
|
|
60
|
+
switch (status) {
|
|
61
|
+
case 401:
|
|
62
|
+
return new ApiError("Not authorized. Your token may be invalid or deleted, or it lacks the access this command needs (visitor details or write), or it is limited to other websites. Run `metricraven login` with a token from https://metricraven.com/app/api.", 401, code);
|
|
63
|
+
case 403:
|
|
64
|
+
return new ApiError(message ?? "This token is not allowed to do that.", 403, code);
|
|
65
|
+
case 402:
|
|
66
|
+
return new ApiError(message ?? "Your plan doesn't allow this. Upgrade at https://metricraven.com/app/billing.", 402, code);
|
|
67
|
+
case 404:
|
|
68
|
+
return new ApiError(message ?? (code === "not_found" ? "Not found." : `Not found${code ? ` (${code})` : ""}.`), 404, code);
|
|
69
|
+
case 429:
|
|
70
|
+
return new ApiError("Rate limited. Wait a minute and try again.", 429, code);
|
|
71
|
+
default:
|
|
72
|
+
if (status >= 500)
|
|
73
|
+
return new ApiError(`MetricRaven had a problem (HTTP ${status}). Try again shortly.`, status, code);
|
|
74
|
+
return new ApiError(message ?? (code ? `Request failed: ${code.replace(/_/g, " ")}.` : `Request failed (HTTP ${status}).`), status, code);
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/args.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { PERIODS, customRange, presetRange, validTimeZone } from "./range.js";
|
|
3
|
+
export const FILTER_OPERATORS = ["is", "is_not", "contains", "not_contains"];
|
|
4
|
+
/** Every flag the CLI understands; commands ignore the ones that don't apply. */
|
|
5
|
+
const OPTIONS = {
|
|
6
|
+
json: { type: "boolean" },
|
|
7
|
+
site: { type: "string" },
|
|
8
|
+
token: { type: "string" },
|
|
9
|
+
verbose: { type: "boolean", short: "v" },
|
|
10
|
+
help: { type: "boolean", short: "h" },
|
|
11
|
+
version: { type: "boolean" },
|
|
12
|
+
period: { type: "string", short: "p" },
|
|
13
|
+
from: { type: "string" },
|
|
14
|
+
to: { type: "string" },
|
|
15
|
+
timezone: { type: "string", short: "z" },
|
|
16
|
+
filter: { type: "string", short: "f", multiple: true },
|
|
17
|
+
sort: { type: "string" },
|
|
18
|
+
limit: { type: "string", short: "n" },
|
|
19
|
+
offset: { type: "string" },
|
|
20
|
+
q: { type: "string" },
|
|
21
|
+
payment: { type: "string" },
|
|
22
|
+
name: { type: "string" },
|
|
23
|
+
domain: { type: "string" },
|
|
24
|
+
date: { type: "string" },
|
|
25
|
+
text: { type: "string" },
|
|
26
|
+
platform: { type: "string" },
|
|
27
|
+
goal: { type: "string" },
|
|
28
|
+
trigger: { type: "string" },
|
|
29
|
+
subject: { type: "string" },
|
|
30
|
+
message: { type: "string" },
|
|
31
|
+
notify: { type: "string" },
|
|
32
|
+
enabled: { type: "boolean" },
|
|
33
|
+
disabled: { type: "boolean" },
|
|
34
|
+
yes: { type: "boolean", short: "y" },
|
|
35
|
+
alert: { type: "string" },
|
|
36
|
+
role: { type: "string" },
|
|
37
|
+
};
|
|
38
|
+
export class UsageError extends Error {
|
|
39
|
+
}
|
|
40
|
+
export function parseCli(argv) {
|
|
41
|
+
try {
|
|
42
|
+
const { values, positionals } = parseArgs({ args: argv, options: OPTIONS, allowPositionals: true, strict: true });
|
|
43
|
+
return { command: positionals, flags: values };
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new UsageError(error.message.replace(/^TypeError \[[A-Z_]+\]: /, ""));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* `--filter country:is:US` (repeatable). Values may contain colons (`page:contains:/a:b`); the
|
|
51
|
+
* same dimension and operator merge into one filter with several values (OR), like the dashboard.
|
|
52
|
+
*/
|
|
53
|
+
export function buildFilters(raw) {
|
|
54
|
+
const filters = [];
|
|
55
|
+
for (const item of raw ?? []) {
|
|
56
|
+
const first = item.indexOf(":");
|
|
57
|
+
const second = first < 0 ? -1 : item.indexOf(":", first + 1);
|
|
58
|
+
if (first <= 0 || second < 0)
|
|
59
|
+
throw new UsageError(`Filter "${item}" must look like dimension:operator:value, e.g. country:is:US.`);
|
|
60
|
+
const dim = item.slice(0, first);
|
|
61
|
+
const op = item.slice(first + 1, second);
|
|
62
|
+
const value = item.slice(second + 1);
|
|
63
|
+
if (!FILTER_OPERATORS.includes(op))
|
|
64
|
+
throw new UsageError(`Unknown filter operator "${op}". Use ${FILTER_OPERATORS.join(", ")}.`);
|
|
65
|
+
if (!value)
|
|
66
|
+
throw new UsageError(`Filter "${item}" has no value.`);
|
|
67
|
+
const existing = filters.find((f) => f.dim === dim && f.op === op);
|
|
68
|
+
if (existing) {
|
|
69
|
+
if (!existing.values.includes(value))
|
|
70
|
+
existing.values.push(value);
|
|
71
|
+
}
|
|
72
|
+
else
|
|
73
|
+
filters.push({ dim, op, values: [value] });
|
|
74
|
+
}
|
|
75
|
+
if (filters.length > 6)
|
|
76
|
+
throw new UsageError("At most six filters.");
|
|
77
|
+
return filters;
|
|
78
|
+
}
|
|
79
|
+
/** from/to (or range=all), timezone and filters for a dated report. */
|
|
80
|
+
export function rangeParams(flags, now = new Date()) {
|
|
81
|
+
const params = new URLSearchParams();
|
|
82
|
+
const zone = validTimeZone(flags.timezone);
|
|
83
|
+
if (!zone)
|
|
84
|
+
throw new UsageError(`Unknown timezone "${flags.timezone}". Use an IANA name like America/New_York.`);
|
|
85
|
+
params.set("timezone", zone);
|
|
86
|
+
if (flags.from || flags.to) {
|
|
87
|
+
if (!flags.from || !flags.to)
|
|
88
|
+
throw new UsageError("Use --from and --to together (YYYY-MM-DD), or --period.");
|
|
89
|
+
const range = customRange(flags.from, flags.to, zone);
|
|
90
|
+
if (!range)
|
|
91
|
+
throw new UsageError("--from and --to must be real dates like 2026-10-01, with --from on or before --to.");
|
|
92
|
+
params.set("from", range.from.toISOString());
|
|
93
|
+
params.set("to", range.to.toISOString());
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const period = (flags.period ?? "30d");
|
|
97
|
+
if (!PERIODS.includes(period))
|
|
98
|
+
throw new UsageError(`Unknown period "${flags.period}". Use ${PERIODS.join(", ")}.`);
|
|
99
|
+
if (period === "all")
|
|
100
|
+
params.set("range", "all");
|
|
101
|
+
else {
|
|
102
|
+
const range = presetRange(period, zone, now);
|
|
103
|
+
params.set("from", range.from.toISOString());
|
|
104
|
+
params.set("to", range.to.toISOString());
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const filters = buildFilters(flags.filter);
|
|
108
|
+
if (filters.length)
|
|
109
|
+
params.set("filters", JSON.stringify(filters));
|
|
110
|
+
return params;
|
|
111
|
+
}
|
|
112
|
+
export function positiveInt(value, name, max) {
|
|
113
|
+
if (value === undefined)
|
|
114
|
+
return undefined;
|
|
115
|
+
const n = Number(value);
|
|
116
|
+
if (!Number.isInteger(n) || n < 0 || n > max)
|
|
117
|
+
throw new UsageError(`--${name} must be a whole number from 0 to ${max}.`);
|
|
118
|
+
return n;
|
|
119
|
+
}
|