CLI
Stats, top lists, a live view, server-side events, exports and project setup from the terminal.
my-site · Last 7 days (Nov 17 – Nov 23, Europe/Berlin)
Metric Value Previous Change Trend
Visitors 13,562 13,383 ▲ 1.3% ▆██▄▂▁▄
Visits 14,864 14,716 ▲ 1% ▆██▄▂▁▄
Pageviews 34,929 34,382 ▲ 1.6% ▆█▇▄▁▁▄
Views per visit 2.35 2.34 ▲ 0.4%
Bounce rate 48.8% 49.5% ▼ 1.4% ▃▅▇▁█▃▆
Visit duration 1m 44s 1m 42s ▲ 2%
Events 9,559 9,229 ▲ 3.6%
Revenue 15,639.91 11,246.00 ▲ 39.1% █▂▅▇▅▁▃Install
The CLI isn't published yet. Build it from packages/cli in the Stancelens repository with Bun:
cd packages/cli
bun scripts/build.ts # npm bundle + binaries for every target, in dist/
bun scripts/build.ts --npm # only dist/stancelens.js (runs on Node 20.3+)
bun scripts/build.ts --target linux-x64 # one binaryBinary targets: linux-x64, linux-arm64, darwin-arm64, darwin-x64, windows-x64. Each is a single self-contained file of about 85 to 110 MB, since it embeds the Bun runtime. Copy it onto your PATH as stancelens (stancelens.exe on Windows).
The npm package contains only dist/stancelens.js and the README; bun pm pack and npm publish build it first (prepack). Once it's published, npx stancelens runs it with Node 20.3 or newer.
Log in
stancelens login # prompts for the key, input hidden
stancelens login --host https://analytics.example.com # another Stancelens host
printenv STANCELENS_KEY | stancelens login --site my-site # from a script, key on stdinCreate an API key under Account → API keys. login checks the key with the server before saving anything, then writes it to ~/.config/stancelens/config.json ($XDG_CONFIG_HOME is respected; %APPDATA%\stancelens\config.json on Windows). The file is created with mode 0600 in a 0700 directory and replaced atomically. whoami shows the key's user and warns if the file has become readable by others. logout removes the key from the file (it keeps the host and default site); the key itself keeps working until you revoke it in the dashboard.
- A key whose sites are all on a plan without API access (the REST API needs Growth or higher) is still saved, with a warning. Commands then fail with exit code
3and a link to the billing page. loginwarns when the host uses plainhttp://and isn't on your own machine, because the key would travel unencrypted.
Commands
Most commands take an optional [site]. Without one they use --site, then STANCELENS_SITE, then the default saved with stancelens sites use <id>. Run stancelens <command> --help for every flag and examples.
| Command | What it does |
|---|---|
login, logout, whoami | Save, remove or show the API key, host and default site |
sites | List the sites your key can read, with your role and the site timezone; sites use <id> saves the default |
stats [site] | Visitors, visits, page views, views per visit, bounce rate, visit duration, events and revenue, with sparklines. --compare (previous period) or --compare=year adds the previous value and change; --interval minute|hour|day|week|month sets the sparkline bucket; --csv |
top <report> [site] | Top pages, entry-pages, exit-pages, hostnames, sources, channels, utm-sources, utm-mediums, campaigns, countries, regions, cities, languages, devices, browsers, os, platforms, events or props.<key> (a custom property), sorted by visitors. --limit (1 to 1000, default 10), --page, --csv |
live [site] | Full-screen realtime view, redrawn every 5 s (--interval 2 to 300). Ctrl-C restores the terminal; --once prints one snapshot |
track <event> | Send a server-side event: --prop key=value (repeatable; key:=json keeps numbers and booleans), --user, --visitor, --url, --revenue with --currency (default USD), --time (ISO date or epoch ms, up to 30 days back), --id, --dry-run. Needs an owner or admin key |
export [site] | Stream raw events as CSV, JSON or NDJSON (--format, else the --out extension) to --out <file> or stdout |
init [dir] | Detect the framework and print the install steps; --apply edits the file where that's safe |
open [site] [section] | Open the dashboard, or a section such as realtime, funnels, boards, platforms or heatmaps, in the browser; --print prints the URL |
stancelens stats my-site --period 7d --compare
stancelens stats --period lastmonth --compare=year --csv > report.csv
stancelens top countries --limit 20 --filter device=Mobile
stancelens top props.plan --period 30d
stancelens live my-site --interval 10
stancelens track purchase --revenue 49 --currency EUR --prop seats:=5 --user user_123
stancelens export --from 2026-01-01 --to 2026-01-31 --filter name=signup -o signups.csv
stancelens open my-site realtimeRanges and filters
stats, top, export and open share these flags:
--period today|yesterday|24h|7d|30d|mtd|lastmonth|90d|6mo|12mo|ytd|all(default30d).--from 2026-01-01 --to 2026-01-31for a custom range (inclusive). Periods and dates are days in the site timezone, which the header names (Last 7 days (Nov 17 – Nov 23, Europe/Berlin)).stats,topandexporttake--timezone <IANA zone>to use another one.--filter dim=value, repeatable:country=US,DE(any of),path=~/blog(contains),browser!=Safari(not). Dimensions are the API filter dimensions.
top events leaves out page views and internal events ($web_vital, $exposure, $identify) unless you pass your own --filter name=....
track
track needs a key whose user is an owner or admin of the site. --id <event-id> makes a send safe to repeat: the server drops a resend with the same id within 24 hours, and the CLI reports that as success ("already tracked"). Any other dropped event (bot, excluded, too old) exits with code 1 and the server's reason.
init
| Detected by | Framework | `--apply` edits |
|---|---|---|
next in package.json | Next.js | app/layout.* or src/app/layout.* (adds next/script), else pages/_document.* |
@sveltejs/kit | SvelteKit | src/app.html |
vite | Vite | index.html |
index.html or public/index.html | Plain HTML | That file |
astro, nuxt | Astro, Nuxt | Prints the snippet only |
expo, react-native | Expo, React Native | Prints install and StancelensProvider setup only |
pubspec.yaml with flutter | Flutter | Prints the flutter pub add command (from Git until the pub.dev release) and Stancelens.init |
--apply needs a site ID, never writes a placeholder, and does nothing if the file already loads /e.js with a data-site. --framework <name> skips detection (nextjs, vite, html, sveltekit, astro, nuxt, expo, react-native, flutter).
Scripting
--jsonon every command prints machine-readable JSON on stdout.live --jsonprints one JSON object per refresh (NDJSON) until Ctrl-C.stats --csvandtop --csvprint CSV (text a spreadsheet would run as a formula gets a leading').- Global flags (
--json,--host,--no-color) can go before or after the command. - Errors and notes go to stderr, data to stdout. There's no colour when stdout isn't a terminal, when
NO_COLORis set or with--no-color;FORCE_COLOR=1forces it. exportwithout--outwrites to stdout and waits for a slow pipe instead of buffering. With--outit writes<file>.partialand renames it only when complete, so a failed, stalled or cancelled export leaves no half file.- Rate limits: when the server answers
429with aRetry-Afterof 10 s or less, reads wait and try again (at most twice) and say so on stderr. A longer wait fails with exit code1and the number of seconds.trackis never resent automatically.livekeeps running through rate limits, server errors and network trouble, waiting as long as the server asks. - Redirects aren't followed, so the key is never sent on to another address; the error names the address to use as
--host. sitesusesGET /api/v1/sites. If the server doesn't have it, the CLI checks the saved default site and says the list may be incomplete ("complete": falsein--json).
| Variable | |
|---|---|
STANCELENS_API_KEY | Overrides the saved key |
STANCELENS_HOST | Server URL (default https://stancelens.com) |
STANCELENS_SITE | Default site ID |
STANCELENS_TIMEOUT | Seconds to wait for the server (default 30); for export, the longest pause allowed mid-download |
STANCELENS_CONFIG_DIR | Config folder |
BROWSER | Browser used by open |
STANCELENS_DEBUG=1 | Stack traces for unexpected errors |
Precedence: flag, then environment, then config file, then the default host https://stancelens.com.
Exit codes
| Code | Meaning |
|---|---|
0 | Success |
1 | API or server error, rate limited, unexpected response or redirect, file error, dropped event |
2 | Usage error: unknown command or flag, bad value, missing site, parameter rejected by the server |
3 | Not logged in, key rejected, the key can't access the site, or the site's plan has no API access |
4 | Host unreachable or too slow (DNS, connection refused, timeout, connection lost mid-export) |
130 | Export cancelled with Ctrl-C |