Latest placed orders from customers with specific email domains

How to use the Metrics API to get the last placed orders containing specific email domains in the aggregated details

The problem

You want to get the last ten placed orders that contain two specific email domain in the aggregated details full text, over a selected date and time range, chronologically ordered by date of placement.

The solution

Query

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

Key
Value

limit

10

sort_by

order.placed_at

If you want to focus on some specific information about the carts you're searching for, just fill in the fields array with the list of attributes you want to get in the response.

Filter

Set the desired date and time range using the date_from and date_to keys and add an additional filter on the order field to restrict the related search on orders whose aggregated details matches the query:

Attribute
Operator

aggregated_details

"query": "(*@gmail.com | *@hotmail.com) + placed"

As shown in the example below, use placed_at as the date_field in the date filter if you want the results to count all the orders that were placed in the selected date and time range (read more about this).

Example

The following request uses the Metrics API to search a specific query within the aggregated details of an ordert:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/orders/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "search": {
      "limit": 10,
      "sort": "asc",
      "sort_by": "order.placed_at",
      "fields": [ "order.id", "order.number", "order.status", "customer.email" ]
    },
    "filter": {
      "order": {
        "date_from": "2021-01-01T00:00:00Z",
        "date_to": "2021-12-31T23:59:00Z",
        "date_field": "placed_at",
        "aggregated_details": {"query": "(*@gmail.com | *@hotmail.com) + placed" }
      }
    }
  }'

Similar cases

Just changing a couple of query keys and/or filter parameters you can address lots of very similar use cases, such as:

Last updated