KosmoKrator

analytics

Plausible Analytics Lua API for KosmoKrator Agents

Agent-facing Lua documentation and function reference for the Plausible Analytics KosmoKrator integration.

8 functions 4 read 4 write API key auth

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.

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

NameTypeRequiredDescription
site_idstringyesSite domain, e.g. "example.com"
metricsarrayyesMetrics to retrieve (see list below)
date_rangestringyesTime period (see options below)
dimensionsarraynoGroup results by dimension (see list below)
filtersstringnoJSON-encoded filter expressions
date_fromstringnoStart date (ISO 8601) when date_range="custom"
date_tostringnoEnd date (ISO 8601) when date_range="custom"
order_bystringnoJSON-encoded sort order
limitintegernoMax 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

DimensionDescription
visit:sourceTraffic source (Google, Twitter, etc.)
visit:countryCountry code
visit:cityCity name
visit:deviceDevice type (Desktop, Mobile, Tablet)
visit:browserBrowser name
visit:osOperating system
event:pagePage path
event:nameCustom event name
time:dayDay-level timeseries
time:monthMonth-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.

Metadata-Derived Lua Example

local result = app.integrations.plausible.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

plausible_query_stats

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.

Operation
Read read
Full name
plausible.plausible_query_stats
ParameterTypeRequiredDescription
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).

plausible_realtime_visitors

Get the current number of realtime visitors on a website (visitors in the last 5 minutes).

Operation
Read read
Full name
plausible.plausible_realtime_visitors
ParameterTypeRequiredDescription
site_id string yes The site domain (e.g., "example.com").

plausible_list_sites

List websites tracked in Plausible Analytics. Returns site domains you can query for analytics data.

Operation
Read read
Full name
plausible.plausible_list_sites
ParameterTypeRequiredDescription
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.

plausible_create_site

Register a new website for tracking in Plausible Analytics.

Operation
Write write
Full name
plausible.plausible_create_site
ParameterTypeRequiredDescription
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".

plausible_delete_site

Remove a website from Plausible Analytics tracking. This deletes all associated data.

Operation
Write write
Full name
plausible.plausible_delete_site
ParameterTypeRequiredDescription
site_id string yes The site domain to delete (e.g., "example.com").

plausible_list_goals

List all goals (conversion tracking) configured for a website in Plausible.

Operation
Read read
Full name
plausible.plausible_list_goals
ParameterTypeRequiredDescription
site_id string yes The site domain (e.g., "example.com").

plausible_create_goal

Create a conversion goal for a website in Plausible. Goals can track pageviews to specific pages or custom events.

Operation
Write write
Full name
plausible.plausible_create_goal
ParameterTypeRequiredDescription
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.

plausible_delete_goal

Delete a conversion goal from a website in Plausible.

Operation
Write write
Full name
plausible.plausible_delete_goal
ParameterTypeRequiredDescription
site_id string yes The site domain (e.g., "example.com").
goal_id integer yes The ID of the goal to delete.