analytics
Plausible Analytics Lua API for KosmoKrator Agents
Agent-facing Lua documentation and function reference for the Plausible Analytics KosmoKrator integration.Lua Namespace
Agents call this integration through app.integrations.plausible.*.
Use lua_read_doc("integrations.plausible") inside KosmoKrator to discover the same reference at runtime.
Call Lua from the Headless CLI
Use kosmo integrations:lua when a shell script, CI job, cron job, or another coding CLI should run a deterministic
Plausible Analytics workflow without starting an interactive agent session.
kosmo integrations:lua --eval 'dump(app.integrations.plausible.query_stats({site_id = "example_site_id", metrics = "example_metrics", date_range = "example_date_range", dimensions = "example_dimensions", filters = "example_filters", date_from = "example_date_from", date_to = "example_date_to", order_by = "example_order_by"}))' --json kosmo integrations:lua --eval 'print(docs.read("plausible"))' --json
kosmo integrations:lua --eval 'print(docs.read("plausible.query_stats"))' --json Workflow file
Put repeatable logic in a Lua file, then execute it with JSON output for the calling process.
local plausible = app.integrations.plausible
local result = plausible.query_stats({site_id = "example_site_id", metrics = "example_metrics", date_range = "example_date_range", dimensions = "example_dimensions", filters = "example_filters", date_from = "example_date_from", date_to = "example_date_to", order_by = "example_order_by"})
dump(result) kosmo integrations:lua workflow.lua --json
kosmo integrations:lua workflow.lua --force --json integrations:lua exposes app.integrations.plausible, app.mcp.*, docs.*, json.*, and regex.*. Use app.integrations.plausible.default.* or app.integrations.plausible.work.* when you configured named credential accounts.
MCP-only Lua
If the script only needs configured MCP servers and does not need Plausible Analytics, use the narrower mcp:lua command.
# Use mcp:lua for MCP-only scripts; use integrations:lua for this integration namespace.
kosmo mcp:lua --eval 'dump(mcp.servers())' --json Agent-Facing Lua Docs
This is the rendered version of the full Lua documentation exposed to agents when they inspect the integration namespace.
Plausible Analytics — Lua API Reference
query_stats
Query website analytics with aggregate stats, timeseries, or breakdowns.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | Site domain, e.g. "example.com" |
metrics | array | yes | Metrics to retrieve (see list below) |
date_range | string | yes | Time period (see options below) |
dimensions | array | no | Group results by dimension (see list below) |
filters | string | no | JSON-encoded filter expressions |
date_from | string | no | Start date (ISO 8601) when date_range="custom" |
date_to | string | no | End date (ISO 8601) when date_range="custom" |
order_by | string | no | JSON-encoded sort order |
limit | integer | no | Max results (default: 10000) |
Date Range Options
"7d", "28d", "30d", "month", "3mo", "6mo", "12mo", "custom"
When using "custom", you must also pass date_from and date_to.
Available Metrics
visitors, pageviews, visits, bounce_rate, visit_duration, views_per_visit, events, conversion_rate
Available Dimensions
| Dimension | Description |
|---|---|
visit:source | Traffic source (Google, Twitter, etc.) |
visit:country | Country code |
visit:city | City name |
visit:device | Device type (Desktop, Mobile, Tablet) |
visit:browser | Browser name |
visit:os | Operating system |
event:page | Page path |
event:name | Custom event name |
time:day | Day-level timeseries |
time:month | Month-level timeseries |
Filter Syntax
Filters are a JSON string containing an array of filter expressions:
[["operator", "dimension", ["value1", "value2"]]]
Operators: is, is_not, contains, does_not_contain, matches, does_not_match
Order By Syntax
[["metric_name", "desc"]]
Examples
Top pages by visitors (last 30 days)
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "30d",
dimensions = {"event:page"},
order_by = '[["visitors", "desc"]]',
limit = 20
})
for _, row in ipairs(result.rows) do
print(row["event:page"] .. ": " .. row.visitors .. " visitors")
end
Traffic by country (custom date range)
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "visits", "bounce_rate"},
date_range = "custom",
date_from = "2026-01-01",
date_to = "2026-01-31",
dimensions = {"visit:country"},
order_by = '[["visitors", "desc"]]',
limit = 10
})
for _, row in ipairs(result.rows) do
print(row["visit:country"] .. ": " .. row.visitors .. " visitors, " .. row.bounce_rate .. "% bounce")
end
Filter to specific country
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "7d",
dimensions = {"event:page"},
filters = '[["is", "visit:country", ["US"]]]'
})
Filter pages containing /blog
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "30d",
dimensions = {"event:page"},
filters = '[["contains", "event:page", ["/blog"]]]',
order_by = '[["pageviews", "desc"]]'
})
Daily timeseries
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors"},
date_range = "30d",
dimensions = {"time:day"}
})
Aggregate totals (no dimensions)
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews", "bounce_rate", "visit_duration"},
date_range = "30d"
})
Multi-Account Usage
If you have multiple plausible accounts configured, use account-specific namespaces:
-- Default account (always works)
app.integrations.plausible.function_name({...})
-- Explicit default (portable across setups)
app.integrations.plausible.default.function_name({...})
-- Named accounts
app.integrations.plausible.work.function_name({...})
app.integrations.plausible.personal.function_name({...})
All functions are identical across accounts — only the credentials differ.
Raw agent markdown
# Plausible Analytics — Lua API Reference
## query_stats
Query website analytics with aggregate stats, timeseries, or breakdowns.
### Parameters
| Name | Type | Required | Description |
|------|------|----------|-------------|
| `site_id` | string | yes | Site domain, e.g. `"example.com"` |
| `metrics` | array | yes | Metrics to retrieve (see list below) |
| `date_range` | string | yes | Time period (see options below) |
| `dimensions` | array | no | Group results by dimension (see list below) |
| `filters` | string | no | JSON-encoded filter expressions |
| `date_from` | string | no | Start date (ISO 8601) when `date_range="custom"` |
| `date_to` | string | no | End date (ISO 8601) when `date_range="custom"` |
| `order_by` | string | no | JSON-encoded sort order |
| `limit` | integer | no | Max results (default: 10000) |
### Date Range Options
`"7d"`, `"28d"`, `"30d"`, `"month"`, `"3mo"`, `"6mo"`, `"12mo"`, `"custom"`
When using `"custom"`, you must also pass `date_from` and `date_to`.
### Available Metrics
`visitors`, `pageviews`, `visits`, `bounce_rate`, `visit_duration`, `views_per_visit`, `events`, `conversion_rate`
### Available Dimensions
| Dimension | Description |
|-----------|-------------|
| `visit:source` | Traffic source (Google, Twitter, etc.) |
| `visit:country` | Country code |
| `visit:city` | City name |
| `visit:device` | Device type (Desktop, Mobile, Tablet) |
| `visit:browser` | Browser name |
| `visit:os` | Operating system |
| `event:page` | Page path |
| `event:name` | Custom event name |
| `time:day` | Day-level timeseries |
| `time:month` | Month-level timeseries |
### Filter Syntax
Filters are a JSON string containing an array of filter expressions:
```
[["operator", "dimension", ["value1", "value2"]]]
```
Operators: `is`, `is_not`, `contains`, `does_not_contain`, `matches`, `does_not_match`
### Order By Syntax
```
[["metric_name", "desc"]]
```
## Examples
### Top pages by visitors (last 30 days)
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "30d",
dimensions = {"event:page"},
order_by = '[["visitors", "desc"]]',
limit = 20
})
for _, row in ipairs(result.rows) do
print(row["event:page"] .. ": " .. row.visitors .. " visitors")
end
```
### Traffic by country (custom date range)
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "visits", "bounce_rate"},
date_range = "custom",
date_from = "2026-01-01",
date_to = "2026-01-31",
dimensions = {"visit:country"},
order_by = '[["visitors", "desc"]]',
limit = 10
})
for _, row in ipairs(result.rows) do
print(row["visit:country"] .. ": " .. row.visitors .. " visitors, " .. row.bounce_rate .. "% bounce")
end
```
### Filter to specific country
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "7d",
dimensions = {"event:page"},
filters = '[["is", "visit:country", ["US"]]]'
})
```
### Filter pages containing /blog
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews"},
date_range = "30d",
dimensions = {"event:page"},
filters = '[["contains", "event:page", ["/blog"]]]',
order_by = '[["pageviews", "desc"]]'
})
```
### Daily timeseries
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors"},
date_range = "30d",
dimensions = {"time:day"}
})
```
### Aggregate totals (no dimensions)
```lua
local result = app.integrations.plausible.query_stats({
site_id = "example.com",
metrics = {"visitors", "pageviews", "bounce_rate", "visit_duration"},
date_range = "30d"
})
```
---
## Multi-Account Usage
If you have multiple plausible accounts configured, use account-specific namespaces:
```lua
-- Default account (always works)
app.integrations.plausible.function_name({...})
-- Explicit default (portable across setups)
app.integrations.plausible.default.function_name({...})
-- Named accounts
app.integrations.plausible.work.function_name({...})
app.integrations.plausible.personal.function_name({...})
```
All functions are identical across accounts — only the credentials differ. local result = app.integrations.plausible.query_stats({site_id = "example_site_id", metrics = "example_metrics", date_range = "example_date_range", dimensions = "example_dimensions", filters = "example_filters", date_from = "example_date_from", date_to = "example_date_to", order_by = "example_order_by"})
print(result) Functions
query_stats Read
Query website analytics from Plausible. Supports aggregate stats, timeseries, and breakdowns by dimension. Use dimensions to group results (e.g., by country, source, page). Omit dimensions for simple aggregate totals.
- Lua path
app.integrations.plausible.query_stats- Full name
plausible.plausible_query_stats
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain (e.g., "example.com"). |
metrics | array | yes | Metrics to retrieve: visitors, pageviews, visits, bounce_rate, visit_duration, views_per_visit, events, conversion_rate. |
date_range | string | yes | Time period: "7d", "28d", "30d", "month", "3mo", "6mo", "12mo", or "custom" (requires date_from/date_to). |
dimensions | array | no | Dimensions to group by: visit:source, visit:country, visit:city, visit:device, visit:browser, visit:os, event:page, event:name, time:day, time:month, etc. |
filters | string | no | JSON-encoded filter expressions, e.g., [["is", "visit:country", ["NL"]]]. Pass as a JSON string. |
date_from | string | no | Start date (ISO 8601, e.g., "2025-01-01") when date_range is "custom". |
date_to | string | no | End date (ISO 8601, e.g., "2025-01-31") when date_range is "custom". |
order_by | string | no | JSON-encoded order, e.g., [["visitors", "desc"]]. Pass as a JSON string. |
limit | integer | no | Maximum number of results to return. Sent as pagination.limit (default: 10000). |
realtime_visitors Read
Get the current number of realtime visitors on a website (visitors in the last 5 minutes).
- Lua path
app.integrations.plausible.realtime_visitors- Full name
plausible.plausible_realtime_visitors
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain (e.g., "example.com"). |
list_sites Read
List websites tracked in Plausible Analytics. Returns site domains you can query for analytics data.
- Lua path
app.integrations.plausible.list_sites- Full name
plausible.plausible_list_sites
| Parameter | Type | Required | Description |
|---|---|---|---|
limit | integer | no | Maximum number of sites to return (default: 100). |
after | string | no | Cursor for pagination — pass the value from a previous response to get the next page. |
create_site Write
Register a new website for tracking in Plausible Analytics.
- Lua path
app.integrations.plausible.create_site- Full name
plausible.plausible_create_site
| Parameter | Type | Required | Description |
|---|---|---|---|
domain | string | yes | The domain to track (e.g., "example.com"). |
timezone | string | no | Timezone for the site (e.g., "Europe/Amsterdam"). Defaults to "Etc/UTC". |
delete_site Write
Remove a website from Plausible Analytics tracking. This deletes all associated data.
- Lua path
app.integrations.plausible.delete_site- Full name
plausible.plausible_delete_site
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain to delete (e.g., "example.com"). |
list_goals Read
List all goals (conversion tracking) configured for a website in Plausible.
- Lua path
app.integrations.plausible.list_goals- Full name
plausible.plausible_list_goals
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain (e.g., "example.com"). |
create_goal Write
Create a conversion goal for a website in Plausible. Goals can track pageviews to specific pages or custom events.
- Lua path
app.integrations.plausible.create_goal- Full name
plausible.plausible_create_goal
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain (e.g., "example.com"). |
event_name | string | no | Custom event name to track (e.g., "Signup"). Use this OR page_path, not both. |
page_path | string | no | Page path to track visits to (e.g., "/thank-you"). Use this OR event_name, not both. |
delete_goal Write
Delete a conversion goal from a website in Plausible.
- Lua path
app.integrations.plausible.delete_goal- Full name
plausible.plausible_delete_goal
| Parameter | Type | Required | Description |
|---|---|---|---|
site_id | string | yes | The site domain (e.g., "example.com"). |
goal_id | integer | yes | The ID of the goal to delete. |