Breakdown

How to perform a breakdown query and how it works

Breakdowns are aggregations that summarize your data as metrics (based on specific operators) or statistics, computed on field values. When performing a breakdown query on the Metrics API endpoint you get in the response the value of the computation (based on the selected operator) on the selected field, aggregated by another field.

Request

To perform a breakdown query send a POST request to the /{{resource_name}}/breakdown endpoint specifying the query keys and filter parameters:

{
  "breakdown": {
    "by": "...",
    "field": "...",
    "operator": "...",
    "condition": { ... },
    "sort": "...",
    "limit": ...,
    "breakdown": { ... }
  },
  "filter": { ... },
  "meta": { ... }
}

Query keys

KeyTypeRequiredDescriptionValues

by

String

The field you want the results of the query aggragated by.

The available values for this key depend on the resource you're doing statistics on (see orders, returns, or carts for the related lists).

field

String

The field you want the metrics or statistics computed on.

The available values for this key depend on the resource you're doing statistics on (see orders, returns, or carts for the related lists).

operator

String

The computing operator.

The available operators depend on the field key value (see orders, returns, or carts for the related lists).

condition

Object

An additional constraint to fine-tune the set of records shown in the response, applied to the computed results of the query. It is available for operators that return single numeric (float or integer) values.

One of: "eq": ...

"ne": ...

"gt": ...

"gte": ...

"lt": ...

"lte": ...

"gt_lt": [...]

"gte_lte": [...]

"gte_lt": [...]

"gt_lte": [...] (default is no condition).

sort

String

The way you want the results of the query to be sorted.

One of asc or desc (default is desc).

limit

Integer

The maximum number of records shown in the response.

Default is 10, max is 100.

breakdown

Object

The optional nested breakdown.

The nested breakdown by key available values depend of the parent breakdown by key value (see orders, returns, or carts for the related lists).

Nesting breakdowns

Breakdowns can be nested recursively one into the other, up to one level (see example). The valid values allowed for the by key of the nested breakdown are strictly dependent on the value you specified in the by key of the parent breakdown. Hence, they are different for each resource you're doing statistics on (see orders, returns, and carts for the related lists).

You cannot group the nested breakdown by the same field by which you're already grouping the parent breakdown.

Response

The response of a breakdown query returns an aggregation by the field specified in the by key, containing the value of the computation (based on the operator specified in the operator key) on the field specified in the field key. If the query contains a nested breakdown, it is also detailed for each item of the array:

{
  "data": {
    "...": [ // breakdown by
      {
        "label": "...",
        "value": ...,
        "...": [ // nested breakdown by
          {
            "label": "...",
            "value": ...
          },
          {
            "label": "...",
            "value": ...
          },
          { ... }
        ]
      },
      {
        "label": "...",
        "value": ...,
        "...": [ // nested breakdown by
          {
            "label": "...",
            "value": ...
          },
          {
            "label": "...",
            "value": ...
          },
          { ... }
        ]
      },
      { ... }
    ]
  }
}
KeyDescription

label

The different values of the by field the results are aggregated by.

value

The result of the computation for the specific label.

Examples

The following examples will be focused on the query part of the request. So no specific filter or meta options will be defined (i.e. all the results will be filtered by the default filter and the response won't include the request payload). See the use cases section for more complex combinations of queries and filters.

Single breakdown

The following request performs a breakdown query to get the total count of orders by market, as long as the computed result is within a specific range:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "breakdown": {
      "by": "market.name",
      "field": "order.id",
      "operator": "value_count",
      "condition": {
        "gte_lte": [ 10000, 100000 ]
      },
      "sort": "desc",
      "limit": 2
    }
  }'

Nested breakdown

The following request performs a breakdown query to get the total count of orders by market, as long as the computed result is within a specific range. For each market, a breakdown of the orders' total amounts by currency code is also requested:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "breakdown": {
      "by": "market.name",
      "field": "order.id",
      "operator": "value_count",
      "condition": {
        "gte_lte": [ 10000, 100000 ]  
      },
      "sort": "desc",
      "limit": 3,
      "breakdown": {
        "by": "order.currency_code",
        "field": "order.total_amount_with_taxes",
        "operator": "sum"
        "sort": "desc",
        "limit": 2
      }
    }
  }'

Last updated