> ## Documentation Index
> Fetch the complete documentation index at: https://getlago.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Aggregation examples

> Compare how aggregation types calculate different quantities from the same usage events.

The same events can produce different measured quantities depending on the aggregation type. Compare the results to choose the type that matches what you want to measure.

## Events received

A tracking API company measures usage with the `tracked_pages` metric. The customer's contract `contract_example` has a monthly billing period, January 1 to January 31, 2027. Your backend sends two events:

```json Event received #1 theme={"dark"}
{
  "transaction_id": "1111-1111-1111-1111",
  "external_contract_id": "contract_example",
  "code": "tracked_pages",
  "timestamp": 1800057600,
  "properties": {
    "tracked_user_id": "1234-5678-9098-7654",
    "pageviews": 10
  }
}
```

```json Event received #2 theme={"dark"}
{
  "transaction_id": "2222-2222-2222-2222",
  "external_contract_id": "contract_example",
  "code": "tracked_pages",
  "timestamp": 1800144000,
  "properties": {
    "tracked_user_id": "1234-5678-9098-7654",
    "pageviews": 20
  }
}
```

The first event is on January 16, the second on January 17.

## Result per aggregation type

| Aggregation | Calculation | Measured quantity |
| - | - | - |
| **Count** | Count the two matching events. | 2 |
| **Sum** | Add the reported values: 10 + 20. | 30 |
| **Max** | Use the highest reported value. | 20 |
| **Latest** | Use the value from the later event. | 20 |
| **Count unique** | Count the distinct user identifier once. | 1 |
| **Weighted sum** | Weight the running total by how long it applies. | 14.84 |

[Weighted sum](https://getlago.com/docs/v2/guide/billable-metrics/aggregation-types/weighted-sum.md) also accounts for how long the running total applies within the billing period. Here the total is 10 for one day (January 16 to 17), then 30 for the remaining 15 days of January: (10 × 1 day + 30 × 15 days) / 31 days = 14.84.

## Next step

Connect your metric to a [Product](https://getlago.com/docs/v2/guide/product-catalog/metered-products.md), then configure pricing through a [Rate Card](https://getlago.com/docs/v2/guide/rate-cards/overview.md).

> [card] [Aggregation types](https://getlago.com/docs/v2/guide/billable-metrics/aggregation-types/overview.md)
>
> Compare the available aggregation types.

> [card] [Rates](https://getlago.com/docs/v2/guide/rate-cards/rates.md)
>
> Price the aggregated quantity.

> [card] [Create a billable metric (API)](https://getlago.com/docs/v2/api-reference/billable-metrics/create.md)
>
> `POST /billable_metrics`

> [card] [Ingest usage](https://getlago.com/docs/v2/guide/events/ingesting-usage.md)
>
> Send usage events to Lago.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.