Skip to content

Commit 3d699db

Browse files
committed
order_by, time labels example, json highlighting
1 parent 3c9c35a commit 3d699db

File tree

5 files changed

+119
-14
lines changed

5 files changed

+119
-14
lines changed

docs/stats-api-v2.md

+64-12
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ API keys have a rate limit of 600 requests per hour by default. If you have spec
4747

4848
Domain of your site on Plausible to be queried.
4949

50-
### date_range <Required />
50+
### date_range <Required /> {#date_range}
5151

5252
Date range to be queried.
5353

@@ -63,7 +63,7 @@ Date range to be queried.
6363
| `"year"` | Since the start of this year |
6464
| `"all"` | Since the start of stats in Plausible |
6565

66-
### metrics <Required />
66+
### metrics <Required /> {#metrics}
6767

6868
Metrics represent values to be calculated with the query.
6969

@@ -91,13 +91,28 @@ List of dimensions to group by.
9191
Dimensions are attributes of your dataset. Using them in queries enables analyzing and compare multiple groups against each other.
9292
Think of them as `GROUP BY` in SQL.
9393

94+
#### Event dimensions
95+
9496
Valid dimensions include:
9597

9698
| Dimension | Example | Description |
9799
| --- | --- | --- |
98100
| `event:goal` | Register | A custom action that you want your users to take. To use this dimension, you first need to configure some goals in the site settings, or via the Sites API. Learn more about goals here. |
99101
| `event:page` | /blog/remove-google-analytics | Pathname of the page where the event is triggered. You can also use an asterisk to group multiple pages (/blog*) |
100102
| `event:hostname` | example.com | Hostname of the event. |
103+
:::warning
104+
105+
Mixing session metrics `bounce_rate`, `views_per_visit` and `visit_duration` with event dimensions is not allowed.
106+
:::
107+
108+
109+
#### Visit dimensions
110+
111+
Values of these dimensions are determined by the first pageview in a session.
112+
113+
114+
| Dimension | Example | Description |
115+
| --- | --- | --- |
101116
| `visit:entry_page` | /home | Page on which the visit session started (landing page). |
102117
| `visit:exit_page` | /home | Page on which the visit session ended (last page viewed). |
103118
| `visit:source` | Twitter | Visit source, populated from an url query parameter tag (utm_source, source or ref) or the Referer HTTP header. |
@@ -118,16 +133,26 @@ Valid dimensions include:
118133
| `visit:country_name` | United States | Name of the visitor country. |
119134
| `visit:region_name` | California | Name of the visitor region. |
120135
| `visit:city_name` | San Francisco | Name of the visitor city. |
136+
137+
#### Time dimensions {#time-dimensions}
138+
139+
It's useful to be able to group data by time, which can be done via the following dimensions.
140+
141+
| Dimension | Example | Description |
142+
| -- | -- | -- |
121143
| `time` | 2024-01-01 | Time or date to group by. Automatically figures out the appropriate time:bucket value from date range. Response is a valid ISO8601 date or timestamp |
122144
| `time:hour` | 2021-01-27T23:43:10Z | Time grouped by hour. ISO8601 |
123145
| `time:day` | 2021-01-27 | Time grouped by date |
124146
| `time:week` | 2021-01-04 | Time grouped by start of the week |
125147
| `time:month` | 2021-01-01 | Time grouped by start of month |
126148

127-
:::warning
149+
Note that:
150+
- `time` dimensions are not usable in filters. Set [`date_range`](#date_range) instead.
151+
- If no data falls into a given time bucket, no values are returned. [See `include.time_labels` option](#time-labels) for a workaround.
128152

129-
Mixing session metrics `bounce_rate`, `views_per_visit` and `visit_duration` with event dimensions is not allowed.
130-
:::
153+
Note behavior when no data matches - nothing returned. See time-labels.
154+
155+
Not usable in filters.
131156

132157
#### Custom properties
133158

@@ -162,6 +187,27 @@ Note that only `is` operator is valid for `event:goal` dimension.
162187

163188
List of values to match against. A data point matches filter if _any_ of the clauses matches.
164189

190+
### order_by <Optional />
191+
192+
Allows for custom ordering of query results.
193+
194+
List of tuples `[dimension_or_metric, direction]`, where:
195+
- `dimension_or_metric` needs to be listed in query [`metrics`](#metrics) or [`dimensions`](#dimensions) respectively.
196+
- `direction` can be one of `"asc"` or `"desc"`
197+
198+
For example:
199+
200+
```json
201+
[["visitors", "desc"], ["visit:country_name", "asc"]]
202+
```
203+
204+
When not specified, the default ordering is:
205+
206+
1. If a [time dimensions](#time-dimensions) is present, `[time_dimension, "asc"]`
207+
2. By the first metric specified, descending.
208+
209+
[See full query example](#example-order-by)
210+
165211
### include <Optional />
166212

167213
Default: `{}`
@@ -185,7 +231,7 @@ Default: `false`
185231
Requires a `time` dimension being set. If true, sets meta.time_labels in response containing all
186232
time labels valid for `date_range`.
187233

188-
TODO: Link to an example
234+
[See example](#example-time-labels)
189235

190236
## Examples
191237

@@ -194,15 +240,16 @@ TODO: Link to an example
194240
The following example queries are interactive and can be edited and run against your own data.
195241
:::
196242

197-
### Simple aggregate query {#aggregate}
243+
### Simple aggregate query {#example-aggregate}
198244

199245
<ApiV2Example
200-
id="aggregate"
201246
request="apiv2-examples/aggregate-request.json"
202247
response="apiv2-examples/aggregate-response.json"
203248
/>
204249

205-
### Best-performing UTM tags
250+
### Custom date range {#example-custom-date-range}
251+
252+
### Best-performing UTM tags {#example-utm}
206253

207254
### Filtering by utm and country {#filtering-basic}
208255

@@ -213,12 +260,17 @@ Event, visit and custom properties
213260
### Timeseries query {#timeseries}
214261

215262
<ApiV2Example
216-
id="timeseries-query"
217263
request="apiv2-examples/timeseries-request.json"
218264
response="apiv2-examples/timeseries-response.json"
219265
/>
220266

221-
### Timeseries query with labels
267+
### Timeseries query hourly, with labels {#example-time-labels}
268+
269+
<ApiV2Example
270+
request="apiv2-examples/time-labels-request.json"
271+
response="apiv2-examples/time-labels-response.json"
272+
/>
273+
222274

223275
### Using custom properties {#custom-properties-example}
224276

@@ -228,7 +280,7 @@ Event, visit and custom properties
228280

229281
### Including imported data failed
230282

231-
### Explicit ordering of results
283+
### Explicit ordering of results {#example-order-by}
232284

233285
### Filtering by hostname
234286

docusaurus.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module.exports = {
106106
copyright: `Copyright © ${new Date().getFullYear()} Plausible Analytics. Built with Docusaurus.`,
107107
},
108108
prism: {
109-
additionalLanguages: ['bash'],
109+
additionalLanguages: ['bash', 'json'],
110110
},
111111
},
112112
presets: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"site_id": "dummy.site",
3+
"metrics": ["visitors"],
4+
"date_range": "day",
5+
"dimensions": ["time:hour"],
6+
"include": {
7+
"time_labels": true
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"results": [
3+
{"metrics": [4], "dimensions": ["2024-08-17 01:00:00"]},
4+
{"metrics": [4], "dimensions": ["2024-08-17 02:00:00"]},
5+
{"metrics": [1], "dimensions": ["2024-08-17 23:00:00"]}
6+
],
7+
"meta": {
8+
"time_labels": [
9+
"2024-08-17 00:00:00",
10+
"2024-08-17 01:00:00",
11+
"2024-08-17 02:00:00",
12+
"2024-08-17 03:00:00",
13+
"2024-08-17 04:00:00",
14+
"2024-08-17 05:00:00",
15+
"2024-08-17 06:00:00",
16+
"2024-08-17 07:00:00",
17+
"2024-08-17 08:00:00",
18+
"2024-08-17 09:00:00",
19+
"2024-08-17 10:00:00",
20+
"2024-08-17 11:00:00",
21+
"2024-08-17 12:00:00",
22+
"2024-08-17 13:00:00",
23+
"2024-08-17 14:00:00",
24+
"2024-08-17 15:00:00",
25+
"2024-08-17 16:00:00",
26+
"2024-08-17 17:00:00",
27+
"2024-08-17 18:00:00",
28+
"2024-08-17 19:00:00",
29+
"2024-08-17 20:00:00",
30+
"2024-08-17 21:00:00",
31+
"2024-08-17 22:00:00",
32+
"2024-08-17 23:00:00"
33+
]
34+
},
35+
"query": {
36+
"metrics": ["visitors"],
37+
"date_range": ["2024-08-17", "2024-08-17"],
38+
"filters": [],
39+
"dimensions": ["time:hour"],
40+
"order_by": [["time:hour", "asc"], ["visitors", "desc"]]
41+
}
42+
}

src/js/examples.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ function read(path) {
55
const examplePaths = [
66
"apiv2-examples/aggregate-request.json",
77
"apiv2-examples/aggregate-response.json",
8-
"apiv2-examples/timeseries-request.json"
8+
"apiv2-examples/timeseries-request.json",
9+
"apiv2-examples/time-labels-request.json",
10+
"apiv2-examples/time-labels-response.json",
911
]
1012

1113
export default Object.fromEntries(examplePaths.map((path) => [path, read(path)]))

0 commit comments

Comments
 (0)