Orders by day

How to use the Metrics API to get the total number of orders of your organization, grouped by day

The problem

You want to get the total number of orders over a selected date and time range, grouped by day.

The solution

Query

You need to perform a date breakdown query setting the required query keys as follows and adding the optional ones based on your needs:

Key
Value

by

order.current_date

field

order.id

operator

value_count

interval

day

Change the interval key if you want to group the results differently (e.g. by year or hour — default is month).

Filter

Make sure to set the desired date and time range using the date_from and date_to keys in the filter.

In this example, since the date_field isn't specified, the default value current_date will be used. Being the same date time field used in the by key of the query, you'll get in the response all the orders that actually changed their status each day within the selected date and time range (read more about this).

Example

The following request uses the Metrics API to get the total number of orders, grouped by day:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/date_breakdown' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "date_breakdown": {
      "by": "order.current_date",
      "field": "order.id",
      "operator": "value_count",
      "interval": "day"
    },
    "filter": {
      "order": {
        "date_from": "2021-01-01T00:00:00Z",
        "date_to": "2021-12-31T23:59:00Z"
      }
    }
  }'

Last updated