For the complete documentation index, see llms.txt. This page is also available as Markdown.

Frequently bought together products

How to use the Metrics API to get the products that have been most frequently bought together with other ones

The problem

You want to get the list of products that, over your order history, have been added as line items to the same orders as two specific other products (SKUs or bundles).

The solution

Filter

You need to perform an FBT query setting the required filter on the line items field to specify the array containing the IDs of the two products with which you're searching the correlation:

Attribute
Operator

item_ids

"in": [ "BmDzSVkXAW", "ZrxeSRgOmB" ]

Try it on Postman 🚀

Example

The following request uses the Metrics API to get the list of products that have been most frequently bought together with the items identified by the IDs "BmDzSVkXAW" and "ZrxeSRgOmB":

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/metrics/analysis/fbt' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "filter": {
      "line_items": {
        "item_ids": {
          "in": [ "BmDzSVkXAW", "ZrxeSRgOmB" ]
        }
      }
    }
  }'

On success, the API responds with a 200 OK status code, returning the aggregated values in the data object and additional information in the meta object:

{
  "data": [
    {
      "item_id": "nprzSARKeW",
      "value": 103,
      "type": "skus",
      "name": "Black T-Shirt, Men, Size L"
    },
    {
      "item_id": "ZNRJSQXkjZ",
      "value": 99,
      "type": "skus",
      "name": "Pink T-Shirt, Women, Size XXS"
    },
    {
      "item_id": "BxAkSrDODn",
      "value": 41,
      "type": "skus",
      "name": "Pink Short Pants, Women, Size XS"
    },
    { ... }
  ],
  "meta": {
    "type": "fbt",
    "trace_id": "fe571ea2-8a4f-4a5e-bd26-ac54651bb2e4",
    "mode": "test",
    "organization_id": "xYZkjABcde",
    "market_ids": [ "yzXKjYzaCx", "..." ]
  }
}

Last updated