# Flexible Filtering

> Filter, group and aggregate any property in your JSON events with JPath. There's no schema to register and no tagging or flattening step.

Part of [Real-Time Metrics](https://aggregations.io/real-time-metrics.md). Full reference: [Filters docs](https://aggregations.io/docs/metrics/filters.md), [JPath docs](https://aggregations.io/docs/jpath.md).

## What's in a filter

A filter has four parts, all set in the app. The Metrics API, Grafana and Data Endpoints all read from what you define here.

### Filter: which events count

A JPath condition evaluated against the payload as you sent it, so new properties work as soon as you start sending them.

```text
@.eventName == 'checkout' && @.cart.total > 0
```

Operators: `==`, `!=`, `>`, `>=`, `<`, `<=`, `LIKE`, `ILIKE`, `=~` (regex), `in [...]`, field existence, `!`, `&&`, `||`, and `.length` for arrays. JPath is based on JSONPath, which is widely documented, so AI assistants already write it well and there's no new query language to learn.

### Groupings: how to split results

Up to 10 properties, each with an optional alias used in legends and API responses. For example `@.device.type` aliased to `Device` and `@.properties.geo.country` aliased to `Country`.

Any grouping can be ignored at query time: keep it, filter it to a few values, or roll it up as if it weren't there. A filter grouped by device and country also gives you totals by device, by country and overall.

### Interval: how often results are calculated

From 30 seconds to 100 years, including odd sizes like 13 minutes for one in-game day. Results can be read back at any wider interval, so a 5-minute filter can be charted hourly or daily without keeping separate hourly and daily aggregates. That includes unique counts and percentiles, which are merged rather than added or averaged.

### Aggregations: what to calculate

Up to 10 per filter. Each has a field, its calculations (count, sum, min, max, average, percentiles, approximate distinct count) and an optional sub-filter. Sub-filters keep slices like big orders or cold starts in the same filter as the totals:

| Aggregation | Field | Sub-filter | Calculations |
| --- | --- | --- | --- |
| Revenue | `@.cart.total` | | sum, avg, percentiles |
| Buyers | `@.userId` | | distinct count |
| Big orders | | `@.cart.total > 500` | count |

## Writing filters

Address any property, however deeply nested, with dot notation (`@.device.platform`) or bracket notation (`@['device']['platform']`) for keys with spaces or special characters. Next to the filter the app shows how it was parsed, so you can check how `&&` and `||` are grouped before saving.

## Arrays inside events

Clients often batch things like impressions into one event with an array; you don't have to split them before sending.

- **Array flattening** treats each item as its own row. Paths starting with `@` read the item and paths starting with `$` read the parent event, so per-item fields and shared context work together. Example: flatten `@.impressions`, filter `$.event_name == 'Impressions' && @.duration_ms > 500`, group by `@.item_id` and `$.device.type`.
- **Array calculations** keep the event whole and aggregate across the array, for example a distinct count of `@.impressions[*].item_id`.

See [Working with arrays](https://aggregations.io/docs/metrics/filters/arrays.md).

## Testing and changing filters

- **Debug Mode** runs your filter, groupings, aggregations and timestamp settings against JSON you paste in, updating as you edit. It runs in your browser; the JSON isn't sent to our servers. [Debug Mode docs](https://aggregations.io/docs/metrics/filters/debug-mode.md)
- **Data Preview** plots any aggregation over a time range by grouping, to confirm a filter matches what you expect. [Testing docs](https://aggregations.io/docs/metrics/testing.md)
- **Editing** a filter needs no code change or deploy. Results follow the new definition from the time you save; earlier results aren't recalculated. For larger changes, duplicate the filter so the original's history stays intact.
