Implement Reusable Metric Store
Overview
With, AML Extend, you can define metrics in one AML file and reuse them in different datasets. This is useful because:
- You want to avoid duplicating logics for every new business question.
- You want to manage important business logics in one central place.
- You want to have a lean analytics code base to ease the learning curve for new data members.
How to reuse metics
You can define metrics in a single AML file:
// In metrics.aml
// Define a Partial Type of Dataset which contains a group of metrics
PartialDataset revenue_metrics {
metric gmv { ... }
metric mrr { ... }
// A sample metric
metric order_value {
label: "Order Value"
type: "number"
hidden: false
definition: @aql orders | sum(order_item.quantity * products.price)
}
}
Then you can declare a dataset and reuse these metrics:
// In company.dataset.aml,
Dataset comnpany {
// Omitted for brevity
}
Dataset company_with_revenue = company.extend(revenue_metrics)
You can also modify metrics dynamically by chaining extends as well:
// In store.dataset.aml,
Dataset store {
// Omitted for brevity
}
Dataset store_with_revenue = store.extend(revenue_metrics).extend({
metric order_value {
label: "Order Value"
type: "number"
hidden: false
definition: @aql orders | sum(order_item.quantity * products.price * (1 - orders.discount))
}
})
How to reuse parameterized metrics
Sometimes, different business logics may even share some common factors. For example, you could decide to price products in the US and UK differently using two separate data models. Then you may calculate Order Value metric for both the US and UK market as follows:
PartialDataset revenue_metrics {
metric order_value_us {
label: "Order Value"
type: "number"
hidden: false
definition: @aql orders | sum(order_item.quantity * products_us.price)
}
metric order_value_uk {
label: "Order Value"
type: "number"
hidden: false
definition: @aql orders | sum(order_item.quantity * products_uk.price)
}
}
In essence, the formula to calculate the Order Value metric is the same. The only difference is that the data model in use. We can further modularize this metric logic using AML Function:
// Declare a function to parameterize your reusable metrics
Func getRevenueMetrics(product_model: Model) {
PartialDataset revenue_metrics {
metric order_value {
label: "Order Value"
type: "number"
hidden: false
definition: @aql orders | sum(order_item.quantity * ${product_model.name}.price)
}
}
}
Finally, we can reuse this metric logic in two different datasets, one for the US and one for the UK:
// In us_data.dataset.aml
Dataset us_market {
// Omitted for brevity
}
Dataset us_market_with_revenue = us_market.extend(getRevenueMetrics(products_us))
// In uk_data.dataset.aml
Dataset uk_market {
// Omitted for brevity
}
Dataset uk_market_with_revenue = uk_market.extend(getRevenueMetrics(products_uk))