多维归因(时间对比)
1. 接口描述
用于对某指标按维度进行时间对比归因。
根据“当前时间”与“对比时间”的差异,计算:增长、增长率、贡献率等。
2. 接口 URL
3. 公共请求参数(HEADERS)
| 参数 | 类型 | 必选 | 描述 |
|---|---|---|---|
| tenant-id | String | 是 | 租户空间标识 |
| auth-type | String | 是 | 认证方式。支持 UID、TOKEN、ACCOUNT、APIKEY |
| auth-value | String | 是 | 与 auth-type 对应的认证值 |
公共参数获取方式
tenant-id 可在 Aloudata CAN 顶部导航栏选择指标应用,左边菜单栏选择 API 集成,在 API 集成界面获取;auth-value 请按 auth-type 填写对应认证值。

4. 请求参数
| 字段 | 类型 | 必选 | 描述 |
|---|---|---|---|
| metric | String | ✔ | 指标名称。可以是原始指标,也可以是 metricDefinitions 中定义的临时指标 |
| metricDefinitions | Map |
✘ | 临时指标定义。用于在本次请求中基于原指标增加过滤条件 |
| dimensions | List |
✔ | 参与归因的维度列表。接口会按每个维度分别返回归因表 |
| attribution | Object | ✔ | 时间对比归因参数 |
metricDefinitions 字段
| 字段 | 类型 | 必选 | 描述 |
|---|---|---|---|
| refMetric | String | ✔ | 被引用的原始指标 code |
| filters | List |
✘ | 作用在该临时指标上的过滤条件表达式 |
attribution 字段
| 字段 | 类型 | 必选 | 描述 |
|---|---|---|---|
| attributionCalculateType | String | ✔ | 归因计算方式。ALL 表示按整体值计算,AVG 表示按均值计算 |
| currentTimeConstraint | String | ✔ | 当前时间范围表达式 |
| compareTimeConstraint | String | ✔ | 对比时间范围表达式 |
| filters | List |
✘ | 全局过滤条件。下钻时可把上一层点击的维度值放到这里 |
请求示例
{
"metric": "order_count_temp1",
"metricDefinitions":{
"order_count_temp1": {
"refMetric": "order_count",
"filters": ["IN([province],\"浙江\",\"江苏\")"]
}
},
"dimensions": ["channel"],
"attribution":{
"attributionCalculateType": "AVG",
"currentTimeConstraint": "datetrunc[metric_time],\"day\")=\"2025-07-07\"",
"compareTimeConstraint": "datetrunc[metric_time],\"day\")=\"2025-07-06\"",
"filters": []
}
}
5. 响应参数说明
| 字段 | 类型 | 描述 |
|---|---|---|
| data | Object | 业务数据。请求成功时返回;请求失败时可能为 null 或不包含有效业务字段 |
| data.tables | Array | 归因结果列表。每个元素对应一个分析维度,最后通常会包含一条 dimension = null 的总体汇总结果 |
| data.warning | String | null | 整体级告警信息,例如部分维度无数据、归因结果不可计算等;没有告警时为 null |
| success | Boolean | 请求是否成功。为 true 表示接口正常返回业务结果;为 false 时应读取 errorMsg / detailErrorMsg |
| code | String | 响应码,例如 "200"。业务方可结合 success 判断请求状态 |
| errorMsg | String | null | 错误信息。请求成功时通常为 null;请求失败时用于展示简要错误原因 |
| detailErrorMsg | String | null | 详细错误信息。 |
| traceId | String | 链路追踪 ID。排查接口问题时请提供该值,便于服务端定位日志 |
tables 元素
| 字段 | 类型 | 描述 |
|---|---|---|
| dimension | String | null | 当前归因结果对应的维度名,例如 brand、price_band。为 null 时表示当前时间和对比时间的总体汇总,不对应某个具体维度 |
| table | Object | 当前维度下的归因结果表。表内各数组按相同下标对齐,例如 brand[0]、@currentValue[0]、@compareValue[0]、@growth[0] 表示同一行数据 |
| warning | String | null | 当前维度的告警信息,例如该维度无可归因数据;没有告警时为 null |
| empty | Boolean | 当前维度结果是否为空。 |
table 字段
| 字段 | 类型 | 含义 |
|---|---|---|
| 维度字段名 | Array |
维度值列表。字段名与 dimension 的值一致,例如 dimension = "brand" 时,表内会返回 brand: [...];总体汇总行 dimension = null 时不返回维度字段 |
| @currentValue | Array |
当前时间范围内的指标值。与维度值数组按下标一一对应 |
| @compareValue | Array |
对比时间范围内的指标值。与 @currentValue 使用相同下标对齐 |
| @growth | Array |
增长值,通常为 @currentValue - @compareValue。大于 0 表示该维度项拉动指标上升,小于 0 表示拉低指标 |
| @growthRate | Array |
增长率,通常为 (@currentValue - @compareValue) / @compareValue。对比值为 0 或不可计算时可能为 null,前端需要兜底展示 |
| @overallContributionRate | Array |
整体贡献率,用于表示该维度项对总体变化的贡献占比。仅维度归因表中返回;总体汇总行通常不返回该字段 |
6. 使用说明
-
用于“同一维度下,不同时点”的指标变化贡献分析。
-
AVG表示归因按均值计算,ALL按整体值计算。
7. 调用示例
查询请求
curl --location --request POST 'http://127.0.0.1:8083/anymetrics/api/v1/attribution/dim_time_compare' \
--header 'auth-type: UID' \
--header 'tenant-id: tn_19487697' \
--header 'auth-value: 463663891121963008' \
--header 'Accept-Language: zh-CN' \
--header 'Content-Type: application/json' \
--data-raw '{
"compareType": "SAME_PERIOD",
"metric": "countDistinct_product_id",
"dimensions": [
"brand",
"price_band"
],
"attribution": {
"attributionCalculateType": "ALL",
"currentTimeConstraint": "DateTrunc([metric_time],\"DAY\") = DateTrunc(Cast(\"2025-07-11 00:00:00\",\"Timestamp\"),\"DAY\")",
"compareTimeConstraint": "DateTrunc([metric_time],\"DAY\") = DateAdd(DateTrunc(Cast(\"2025-07-11 00:00:00\",\"Timestamp\"),\"DAY\"),-1,\"DAY\")"
}
}'
响应示例
{
"data": {
"tables": [
{
"dimension": "brand",
"table": {
"@currentValue": [
6,
8,
13,
8,
7,
7,
11
],
"@compareValue": [
6,
8,
13,
8,
7,
7,
11
],
"@overallContributionRate": [
null,
null,
null,
null,
null,
null,
null
],
"@growthRate": [
"0",
"0",
"0",
"0",
"0",
"0",
"0"
],
"@growth": [
"0",
"0",
"0",
"0",
"0",
"0",
"0"
],
"brand": [
"乐品",
"康逸",
"百味",
"优选",
"典范",
"清泉",
"新鲜派"
]
},
"warning": null,
"empty": false
},
{
"dimension": "price_band",
"table": {
"@currentValue": [
23,
6,
31
],
"@compareValue": [
23,
6,
31
],
"@overallContributionRate": [
null,
null,
null
],
"price_band": [
"40-60",
"0-20",
"20-40"
],
"@growthRate": [
"0",
"0",
"0"
],
"@growth": [
"0",
"0",
"0"
]
},
"warning": null,
"empty": false
},
{
"dimension": null,
"table": {
"@currentValue": [
60
],
"@compareValue": [
60
],
"@growthRate": [
"0"
],
"@growth": [
"0"
]
},
"warning": null,
"empty": false
}
],
"warning": null
},
"success": true,
"code": "200",
"errorMsg": null,
"detailErrorMsg": null,
"traceId": "2537fe72c3f84f51892047a537d3592a.185.17780503785580007"
}