Sell everywhere with Commerce Layer Links.

Tutorials

Build a business dashboard with Retool and the Commerce Layer Metrics API.

February 20, 2024 Fabrizio Picca

Introduction

A few months ago, one of our customers asked us if we could help them answer a question that had been on their minds for a long time. Were their customers first-time buyers or return purchasers of their core product? They wanted a deeper understanding of their customer segments and how their business was growing. Furthermore, the answer to this question would help them plan new sales tactics like promotions that targeted returning versus new buyers. So we pointed them to the Metrics API and helped them access their sales data so they could study customer orders.

This story describes just one of the many uses of the Metrics API. It is a powerful resource you can use to measure the state of your operations. You can use it to

  • Create your own organization reports
  • Set up metric-based alerts
  • Send metric data to any open-source or in-house developed tool, library, or framework
  • Build custom metrics dashboards

These bullets set us up perfectly for the purpose of this post. We've integrated the Metrics API with Retool, a powerful tool they describe as "the fastest way to develop effective softeware". We use Retool to create a dashboard that not only presents data in a user-friendly manner, but also allows for real-time tracking and analysis.

In today's data-driven business environment, visualizing data through dashboards is crucial. Dashboards provide a clear and concise view of business metrics and KPIs, enabling better decision-making. This tutorial guides you through how to make dashboards using Commerce Layer’s Metrics API, providing you with the data views you need to make informed business decisions.

The Goal: Implement a dashboard with Retool that displays Commerce Layer Metrics API data

Our goal is straightforward: Build a dashboard to visualize data coming from Commerce Layer Metrics API. To do this, we will set up a new dashboard application in Retool and we will create queries to fetch data from the Metrics API. Our dashboard will take in two parameters date_from and date_to and will display three metrics:

  • Number of orders by currency
  • Best selling products by market
  • Orders by month

The final result is what you see below in the screenshot below:

A Retool dashboard that visualizes data from the Commerce Layer Metrics API

Let’s start building!

Requirements

Before we dive into the tutorial, let's make sure we have all the necessary requirements in place:

  • A Commerce Layer Account: Create one for free simply by clicking the sign up button on this page.
  • A Retool Account: It's also free at https://www.retool.com.

And that’s everything you will need today to start building your Metrics API dashboards.

Step-by-step tutorial

Step 1: Setting up Commerce Layer environment

After you've created your Commerce Layer account, follow the Onboarding Tutorial that explains how to create an organization and seed it with test data. If you already have an account, you can use an existing organization or create a new one. It's up to you.

Assuming everything goes smoothly, at the end of this step, you will have:

  • A Commerce Layer account
  • An organization with some data
  • The Commerce Layer CLI installed on your computer

It’s now time to create a new integration application in Commerce Layer that will be used to access Metrics API data. Inside the Dashboard, click on Applications:

Click on Applications within the Dashboard interface

Click Add new as per below:

Adding a new application in the Commerce Layer dashboard

The Metrics API needs an Integration Application token so let’s create one:

Create an Integration application and token for the Metrics API

Please note that you might want to use a read-only role for this application as it will only read data from the Metrics API. You can also specify an admin role, but as long as this app will be used only to get Metrics API data, we suggest read only.

Now press Get Credentials and you will be presented with the following page:

The application credentials used to configure access to the Metrics API.

The Client ID, Client secret, and Base endpoint will be used in the next step to configure access to the Metrics API. And that’s all for Step 1. You are done here. Celebrate momentarily, then let's get back to work.

Step 2: Creating a new Retool Dashboard

We are going to setup three things in Retool:

  1. A new REST API resource to connect to Commerce Layer’s Metrics API
  2. Three queries that will be used to get data from the REST API resource that we set up
  3. A web application that will be our visual dashboard with charts and controls

Let’s begin.

Setting up a REST API resource in Retool

From the Retool main page click on “Connect a resource” under the Resources section.

Click on Resources to connect the Metrics API

Select REST API from the list of available resource templates:

Select the REST API option to begin working on the connection.

Once selected, you will be presented with a form to set up your new REST API resource. The picture below is the setup for my organization.

You can start with the General section. Choose a Name for your dashboard, something like:

  • “Commerce Layer Metrics API” (This is the name I use for my setup)

Then you will need to update the a number of items in the Credentials section. Start with the Base URL, using the Metrics API base endpoint from above. This will be something like

  • https://<YOUR_ORGANIZATION_SLUG>.commercelayer.io/metrics/

Add the following to the Headers section:

  • Authorization: Bearer OAUTH2_TOKEN
  • Content-Type: application/vnd.api+json
  • Accept: application/vnd.api.v1+json

Update the Authentication section with following:

  • Select from the drop down OAuth 2.0
  • Check the box Use Client Credential Flow
  • Set your Access Token URL as https://<YOUR_ORGANIZATION_SLUG>.commercelayer.io/oauth/token
  • Set your Client ID and Client secret according to the integration application you created in the previous step.
Configure a new REST API resource in Retool by inputting the Commerce Layer Metrics API information.

Setting up queries to fetch data from the Metrics API

We are setting up three queries, one for each widget we’ll be showing in the dashboard:

  • orders by currency
  • best selling products by market
  • orders by month

A full explanation of how the Metrics API works can be found in our documentation.

Orders by currency

This query uses the orders/breakdown endpoint. Our documentation describes the orders breakown as:

"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."

To create the query, let’s open the Query Library of Retool and click +New

Retool's Query Library used to input the orders/breakdown query from the Commerce Layer Metrics API.

In the Resource field, select the resource you previously created. In this case, we use the Commerce Layer Metrics API.

As Action Type, use the following: - Select POST as the method. The default selection is Get, as shown in the screenshot. - The base URL will be pre-populated when the resource is selected - Make the endpoint (next to the base URL) orders/breakdown - Set raw as the type in the Body section, and use the below code as the request payload:

{
	"breakdown": {
		"by": "order.currency_code",
		"field": "order.id",
		"operator": "value_count",
		"sort": "desc",
		"limit": 20
	},
	"filter": {
		"order": {
			"date_from": "{{date_from}}",
			"date_to": "{{date_to}}"
		}
	}
}

Now let’s analyze the query in more detail. We want the number of orders (value_count operator applied on order.id field) broken down by order.currency_code. By default, the Metrics API returns data from the last month and can’t cover spans longer than a year. We want to filter orders by date, specified by two variables {{date_from}} and {{date_to}}. These variables will be filled by the input fields we will create on the dashboard in the next step. The result should look something like this:

The Metrics API orders breakdown query

Best selling products by Market

Similar to what we just did, we have to create another query on the orders/breakdown endpoint. This time, use the payload below:

{
    "breakdown": {
      "by": "market.name",
      "field": "order.id",
      "operator": "value_count",
      "sort": "desc",
      "limit": 100,
      "breakdown": {
        "by": "line_items.name",
        "field": "order.id",
        "operator": "value_count",
        "sort": "desc",
        "limit": 5
      }
    },
    "filter": {
      "order": {
        "date_from": {{date_from}},
        "date_to": {{date_to}},
        "date_field": "placed_at"
      },
      "line_items": {
        "types": {
          "in": ["skus"]
        }
      }
    }
  }

The query is structurally similar to the previous one we used but makes use of a dobule breakdown to get order data split by market and by line items. For each order we’ll be then selecting the top five by value count. Notice the “double” filter we have here. We want to filter by date (as we did in the previous query), but also per line item type. For this dashboard, we only want to consider line items corresponding to skus. In Commerce Layer, line items can also be shipping methods and other resources linked to the order. In this way we are getting the 5 best performing products (that is the 5 more frequent appearing in the line items) for each market.

Orders by month

This one is slightly different from the previous two queries. The orders by month query will make use of a different endpoint: orders/date_breakdown .

Date Breakdowns are:

"aggregations that show the frequency of occurrence of a specific date value within a dataset and let you apply a specific operator over a selected field of the records that are present on that date. When performing a date breakdown query on the Metrics API endpoint you get in the response the list by date of the values of the computation (based on the selected operator) on the selected field, over the selected time interval, aggregated by another field."

We will use the orders/date_breakdown as the endpoint for the query, but still select the resource we created at the beginning of this section, and use the below as the query payload:

{
  "date_breakdown": {
    "by": "order.placed_at",
    "field": "order.total_amount_with_taxes",
    "operator": "stats",
    "interval": "month"
  },
  "filter": {
    "order": {
      "date_from": "{{date_from}}", 
      "date_to": "{{date_to}}", 
      "date_field": "current_date"
    }
  }
}

This query will retrieve all orders in the date_from / date_to time window. For each order, we want the total amount.

You might have noticed that if you try to run these queries, you will get an error. That’s expected. The query needs input from the application to fill the variables defining the time window. If you just run the query and no value is provided, the result is an API error.

Creating the web application

Now that we have all the data sources in place, it’s time to build the actual dashboard. Retool visual editor will help us in this task.

Retool is a very powerful tool, and of course we can’t cover the basics in this article. To learn more about Retool and their dashboard components, please refer to the product documentation. For specific info on the available components please refer to the components library. You can download the package of this blog here…

The application layout is structured as a component tree where you can easily drag and drop components. Our application has a single page and contains:

  • A header containing the title
  • A container for the dates selector
  • A container for the widget displaying data.
The structure of our dashboard

In this next section, we’ll focus on the core elements of the dashboard, leaving behind the layout and the overall presentation. For now, we’ll skip the header so we can focus on the date selector.

The Date Selector

The panel beside the title contains two Date Time widgets: one for the date_from and one for the date_to. These are the two variables sent to the queries we created in the previous section. In order to comply with the date format expected by the Metrics API, we need to configure the widget accordingly:

In the Content section:

  • Set the default value as {{ new Date() }} Every time the dashboard is loaded, the default date will be the current date.

In the Date section:

  • Set Format as yyyy-MM-dd
  • Leave the other settings with the default values

In the Time section:

  • Set Format as HH:mm:ss
  • Leave the rest untouched

In the Time Zone section:

  • Leave untouched

In the Add-ons section:

  • Set Label as Date From
  • Leave the default prefix icon

Leave all the remaining sections untouched.

Configuration of the Date/Time widgets

The date_to Date Time widget is configured in the same exact way as the date_from.

The Orders by currency widget

This widget will show a breakdown by currency of the orders done in the given time window. We want to see a result like this:

Orders by currency widget

For this widget, we will use the table component. First, we add it to the layout and give it a name. Then, we attach a data source to our table. To do this, open the “code” panel of our app, add a new code section, and select “import query from library” as shown in the screenshot below.

Adding a code section to our project.

A new panel will appear, pictured in the screenshot below. Starting in the General Tab:

  • Select Import from Query Library as the Resource. We are going to select one of the queries previously created.

  • Just below, select Run query automatically when inputs change from the dropdown.

**Every time we change our time window using the previously created date selectors, a new query will be performed and the dashboard will update accordingly.

  • Select the query Orders by Currency as created in the previous section. As you’ll recall, the query needs two variables to run: date_from and date_to. The variables will show in the panel, which allows us to “wire” the input coming from our date selector.

  • Set date_from as {{date_from.value.substring(0, date_from.value.length-4)}}Z

  • Set date_to as {{date_to.value.substring(0, date_to.value.length-4)}}Z

You might be wondering why we truncate the last four digits of the value coming from the Date Time component. We trim the milliseconds because the format is not compatible with the date format expected by Metrics API. (e.g. %Y-%m-%dT%H:%M:%SZ or %Y-%m-%dT%H:%M:%S+/-HH:MM)

  • Finally, in the Transform results section, replace the default value with return data.data.

The Metrics API returns the actual data wrapped in a data object, ( documentation), while the first data is Retool’s wrapper for the overall response.

Configuring the Orders by Currency dashboard view.

You can leave the remaining settings with their default values. Before attaching the data source to the table, let’s take a quick look at the response data we get from the Metrics API.

  1. Visit the code section of the app and select ordersbycurrency
  2. Set the date selectors of the dashboard
  3. Click the Run button on the top of the code panel:
Check the Output section to see the results coming from the Metrics API

The Output section now shows the results coming from the Metrics API. Keep in mind that this is the result of the transformation we configured, so is not the raw data.

{
  "order.currency_code": [
    {
      "label": "EUR",
      "value": 24
    },
    {
      "label": "USD",
      "value": 8
    }
  ]
}

Now let’s go back to our Table component and attach the data source to it. From the Data source drop down, select the code snippet we created before. I used the name ordersbycurrency.

In the Columns section,create the first column named Currency code, and change the following:

  • Set Source as order.currency_code
  • Set Label as “Currency code”
  • Set Format as String
  • Set Mapped value as {{item.label}}

In Retool, an item corresponds to the current item of the array that is received as the data source. label is the attribute of the original data we want to be displayed as the column value according to the response of the API we showed before.

Now, create a second column named # of orders and change the following:

  • Set Source as order.currency_code
  • Set Label as “# of orders”
  • Set Format as Number
  • Set Mapped value as {{item.value}}

Finally, set the Currency code as the Primary key column. Leave the remaining settings with default values. And that’s it. You should now have a nice table that looks like the example we showed at the beginning of this section.

The Best Selling Product By Market widget

Since this widget is based on the Table component, follow the same steps as in the previous section. However, since we are interested in a different metric, you will create another code snippet that uses the “Best-selling products by market” query we created earlier. Here is the configuration:

Configure the settings for the "Best selling products by market" dashboard view

In this case, the output of the query will be something similar:

{
  "market.name": [
    {
      "label": "Europe",
      "value": 23,
      "line_items.name": [
        {
          "label": "White Men T-shirt with Black Logo (XL)",
          "value": 11
        },
        {
          "label": "Black Baby Onesie Short Sleeve with Pink Logo (12 Months)",
          "value": 4
        },
        {
          "label": "Black Baby Onesie Short Sleeve with Pink Logo (6 Months)",
          "value": 3
        },
        {
          "label": "L.A. Confidential ",
          "value": 3
        },
        {
          "label": "White Men T-shirt with Pink Logo (M)",
          "value": 3
        }
      ]
    },
    {
      "label": "USA",
      "value": 8,
      "line_items.name": [
        {
          "label": "Black Baby Onesie Short Sleeve with Pink Logo (12 Months)",
          "value": 5
        },
        {
          "label": "Black Baby Onesie Short Sleeve with White Logo (12 Months)",
          "value": 5
        },
        {
          "label": "White Baby Onesie Short Sleeve with Black Logo (12 Months)",
          "value": 5
        },
        {
          "label": "White Baby Onesie Short Sleeve with Pink Logo (12 Months)",
          "value": 5
        },
        {
          "label": "L.A. Confidential ",
          "value": 3
        }
      ]
    }
  ]
}

Similar to what we did before, we can now create a new table and attach the datasource we just created.

From the Data source drop down, select the code snippet we created before (bestsellingproductbymarket if you named it as I did)

In the Columns section, create a first column named Market name and update the following:

  • Set Source as market.name
  • Set Label as “Market name”
  • Set Format as String
  • Set Mapped value as {{item.label}}

Create a second column named Best Seller, and update these:

  • Set Source as order.currency_code
  • Set Label as “Best Seller”
  • Set Format as String
  • Set Mapped value as {{(item['line_items.name'][0]).label}} We use the “get 0 pattern” because the query is set up to return data in descending order as seen from the output example above.

Finally, set the Primary key to the Market name column. Leave the remaining settings with default values.

The result will be something like this:

The Best Selling Products By Market data view.

The Orders by Month chart

It’s chart time!

We are aiming to visualize the count and value of orders for each month on a timeline. To do this, we use the Orders by month query that we created before that leverages date breakdowns of Metrics API. Also, in this case, we create a code snippet that first imports the query from the library.

The Orders by Month chart configuration page

Let’s give a look at the data returned by our API in this case:

[
  ...,
  {
    "date": "2023-03-01T00:00:00.000Z",
    "value": {
      "count": 2,
      "min": 35.38,
      "max": 35.38,
      "avg": 35.38,
      "sum": 70.76
    }
  },
  {
    "date": "2023-04-01T00:00:00.000Z",
    "value": {
      "count": 12,
      "min": 35.38,
      "max": 73.2,
      "avg": 38.53,
      "sum": 462.38
    }
  },
  ...
]

For each month, the Metrics API will return stats about the orders, namely order count, min value, max value, average, and total value. Now that we have set up the data source, we can create our chart. We are going to use Retool's Chart component.

Start by adding a new chart to the dashboard layout tree. Then, in the Content section, update the following:

  • Select UI Form (default)
  • Set Data source as {{orders_by_month.data}}
  • Set Chart Type as Line Chart
  • Set X-axis values as {{formatDataAsObject(orders_by_month.data)['date']}} so we extract all the months from the response query.

Take note of the formatDataAsObject. It is a Retool helper method used to transform data structures in different forms, an object in this case. More information can be found .

In the Datasets section we are creating two datasets. Our chart will display both order count and order total value per month. Create the first data set:

  • Set the Dataset name as “orders per month”
  • Set the Dataset values with this command {{formatDataAsObject(orders_by_month.data).value.map((value)=>value.count)}} .

What we are doing is extracting the count attribute in the value object and creating an array of all the values for the different months. Now, create the second data set:

  • Set the Dataset name as “revenue per month”
  • Set the Dataset values with this command {{formatDataAsObject(orders_by_month.data).value.map((value)=>value.sum)}} .

Similar to just above, we extract the count attribute in the value object and create an array of all the values for the different months.

Finally, in the Interaction section:

  • Set X-axis title as "Month"
  • Set X-axis type as "Date"

The result should be something like the below:

Retool dashboard visualization showing an "orders by month" chart

The chart is interactive, by clicking on the items in the legend, you can deactivate or activate the data set you want to hide/show. This is it, we are done with our web application!

Conclusion

If you've made it this far, there's not much to wrap up. You already understand the importance of measuring the health of your business by creating data visualizations for your team. This tutorial walked you through the process of building a business dashboard with Retool and the Commerce Layer Metrics API. We discussed setting up a REST API resource in Retool, creating queries to fetch data from the Metrics API, and creating a web application to serve as a visual dashboard. By following these steps, you can effectively visualize and track your business metrics, leading to better data-driven decisions.

But, that doesn't mean that you have all of your questions answered. If you, don't we are here to help. Feel free to connect with me on LinkedIn so we can discuss what you are trying to do. You can also join the Commerce Layer Slack #Community. Just follow the sign up form below.

We hope you enjoyed this mega blog post. I know it was a long post, but it had to be done. :) Happy dashboarding!

Join the Commerce Layer community on