> The index of all pages is at https://docs.afffast.com/llms.txt.

# Stats and forecast

Read revenue, conversions, clicks, EPC, AOV and CR with the API. Compare periods, get the top items of a grouping, and get a revenue forecast.

The stats API answers from the conversions and the clicks of your workspace. All amounts are in the currency of your workspace, or in the `currency` that you send. The API converts each sale with the ECB rate of its sale day.

Three endpoints give the numbers. They all need the scope `stats:read`. A test key reads only test data.

| Endpoint                 | Use                                                                      |
| ------------------------ | ------------------------------------------------------------------------ |
| `GET /v1/stats`          | Totals and rows of conversions, with the optional comparison of periods. |
| `GET /v1/stats/top`      | The best items of one grouping, with share and change.                   |
| `GET /v1/stats/forecast` | The projected revenue, the years and the months.                         |

## The metrics

| Metric         | Meaning                                                                        |
| -------------- | ------------------------------------------------------------------------------ |
| `revenue`      | The sum of the commissions.                                                    |
| `conversions`  | The number of conversions.                                                     |
| `clicks`       | The number of human clicks of the redirector. Bots are not counted.            |
| `order_amount` | The sum of the order amounts.                                                  |
| `epc`          | Earnings per click: `revenue` / `clicks`.                                      |
| `aov`          | Average order value: `order_amount` / `conversions`.                           |
| `cr`           | Conversion rate: `conversions` / `clicks`. It is a ratio: `0.05` is 5 percent. |

A ratio is `null` when its divisor is 0. Clicks have no site and no status, so `clicks`, `epc` and `cr` are `null` for the grouping by site and for a request with the filter `site` or `status`.

`GET /v1/stats` calls the revenue `commission`. It counts all statuses. `GET /v1/stats/top` and `GET /v1/stats/forecast` do not count rejected conversions.

## Compare with an earlier period

Add `compare=previous_period` to `GET /v1/stats`. The response then has `comparison`: the totals and the rows of the period of the same length that ends just before `date[gte]`, and the change of each total in percent.

```bash
curl -g "https://api.afffast.com/v1/stats?date[gte]=2026-11-01&date[lte]=2026-11-30&group_by=day&compare=previous_period" \
  -H "Authorization: Bearer $AFFFAST_API_KEY"
```

Use `compare_from` and `compare_to` for another period, for example the same month of the year before. Do not use them with `compare`.

- A range of whole days gives a range of the same number of days.
- A change is `(current - previous) / previous * 100`, rounded to 2 decimals. It is `null` when the previous value is 0.
- `comparison.rows` has the same grouping as `rows`. With `group_by=day`, the row of the same position is the same point of the other period.
- A CSV file has only the current period.

## Top items

`GET /v1/stats/top` groups by `network`, `program`, `brand`, `site`, `country` or `device`, and sorts by `metric` (the default is `revenue`). It gives the `limit` best items (the default is 5, the most is 50) and one `other` row that adds up the rest. The items and `other` give the total.

```bash
curl -g "https://api.afffast.com/v1/stats/top?group_by=program&metric=revenue&limit=5&date[gte]=2026-11-01&date[lte]=2026-11-30&compare=previous_period" \
  -H "Authorization: Bearer $AFFFAST_API_KEY"
```

Each item has the `metrics`, its `share` of the total of the chosen metric, and, with `compare`, `previous` and `change`. The `share` is `null` for `epc`, `aov` and `cr`, because a ratio has no total to share. The `other` row calculates its ratios from its own sums.

## Forecast

`GET /v1/stats/forecast` gives the projected revenue for the rest of the current month and year, the revenue of each year with its growth, and each month against the average of its year.

The method is simple. It is an estimate and not a promise.

1. The run rate is the average revenue of each of the last 28 complete days. The projection of the rest of a month is the run rate times the days that remain.
2. With 12 complete months of history or more, the forecast also uses the seasonality of the months. The seasonal index of a calendar month is its average revenue divided by the average revenue of all complete months. The level is the average of the last 3 complete months, each divided by its index. A future month is the level times its index.

`method` shows which method the response used: `run_rate` or `seasonal`.

`projected_months` gives the projected revenue of each month from the current month to December, so a chart can follow the season. The current month has the revenue until now plus the projected rest. A later month is the level times its index (`seasonal`), or the run rate times its days (`run_rate`).

In `months`, `index` is the revenue of a month divided by the average month of its year. The value 1 is an average month. Use it for a heatmap. The current month has no `index`, because it is not complete.

```bash
curl "https://api.afffast.com/v1/stats/forecast" \
  -H "Authorization: Bearer $AFFFAST_API_KEY"
```
