Total number of active returns by market name and return status

POST https://{{subdomain}}.commercelayer.io/metrics/returns/breakdown

The problem

Among the ones requested for your orders, you want to get the total number of returns that weren't cancelled or rejected, over a selected date and time range, grouped by the different markets of your organization. For each market, you also want to group the results by the return status.

The solution

Query

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

KeyValue
bymarket_name
fieldreturn.id
operatorvalue_count

You also need to add a nested breakdown setting the related query keys as follows:

KeyValue
byreturn.status
fieldrefunds.id
operatorvalue_count

Filter

Make sure to set the desired date and time range using the date_from and date_to keys or as your environment variables and add an additional filter on the return field to exclude from the related search the cancelled and rejected returns:

AttributeOperator
statuses"not_in": [ "cancelled", "rejected" ]

In this example, since we use created_at as the date_field, you'll get in the response all the returns that were created within the selected date and time range (read more about this).

Request Body

{"breakdown"=>{"by"=>"market.name", "field"=>"return.id", "operator"=>"value_count", "sort"=>"desc", "limit"=>20, "breakdown"=>{"by"=>"return.status", "field"=>"return.id", "operator"=>"value_count", "sort"=>"desc", "limit"=>5}}, "filter"=>{"return"=>{"date_from"=>"{{date_from}}", "date_to"=>"{{date_to}}", "date_field"=>"created_at", "statuses"=>{"not_in"=>["cancelled", "rejected"]}}}}

HEADERS

KeyDatatypeRequiredDescription
Acceptstring
Content-Typestring