跳转至

多维归因(同类对比)

1. 接口描述

多维归因-同类对比接口用于在同一时间范围内,对两个“同类对象”进行归因分析对比。例如:

  • 杭州店 vs 南京店

  • 华东大区 vs 华南大区

  • 自营渠道 vs 代理渠道

通过指定:

  • 对比指标

  • 参与分析的维度(如 channel)

  • 对比对象所属维度(如 store)

  • 当前对象与对比对象的过滤条件与时间范围

接口会返回:

  • 当前对象与对比对象的指标值

  • 增长值、增长率

  • 各维度组合下的整体贡献率、相对贡献率等。

2. 接口 URL

POST /anymetrics/api/v1/attribution/dim_peer_compare

3. 公共请求参数(HEADERS)

参数名 类型 是否必选 最大长度 描述
tenant-id String 32 租户 ID,用于指定指标所在的业务空间
auth-type String 32 认证方式。支持 UIDTOKENACCOUNTAPIKEY
auth-value String 32 auth-type 对应的认证值

公共参数获取方式

tenant-id 可在 Aloudata CAN 顶部导航栏选择指标应用,左边菜单栏选择 API 集成,在 API 集成界面获取;auth-value 请按 auth-type 填写对应认证值。

image.png

4. 请求参数

4.1 Body 总体结构

字段名 类型 是否必选 描述
metric String 参与归因分析的指标名称。可以是原始指标,也可以是 metricDefinitions 中定义的临时指标
metricDefinitions Map 临时指标定义,用于在本次请求中基于原指标增加过滤条件
dimensions Array 参与归因的维度列表,例如 ["brand","price_band"]。接口会按每个维度分别返回归因表
attribution Object 同类对比归因参数

4.2 metricDefinitions 结构(可选)

若使用临时指标(例如对原始指标增加区域过滤),可通过 metricDefinitions 定义:

字段名 类型 描述
refMetric String 被引用的原始指标 code
filters Array 作用在该临时指标上的过滤条件表达式

示例:

"metricDefinitions": {
  "order_count_temp1": {
    "refMetric": "order_count",
    "filters": [
      "IN([province],\"浙江\",\"江苏\")"
    ]
  }
}

4.3 attribution 结构(必选)

字段名 类型 是否必选 描述
attributionCalculateType String 归因计算方式。ALL 表示按整体值计算,AVG 表示按均值计算
compareDimension String 对比对象所属的维度,如 "store_type""store""province"
filters Array 作用于整体(currentObject + compareObject)的全局过滤条件。下钻时可把上一层点击的维度值放到这里
timeConstraint String 对整体的时间过滤条件。若当前对象和对比对象使用同一时间范围,建议填写该字段
currentObject Object 当前对象配置
compareObject Object 对比对象配置

currentObject / compareObject 结构相同:

字段名 类型 是否必选 描述
filters Array 当前/对比对象的过滤条件(如某门店、某区域)
timeConstraint String 当前/对比对象的时间范围表达式。仅当两个对象需要使用不同时间范围时填写

4.4 请求示例

{
    "metric": "countDistinct_product_id",
    "dimensions": [
        "brand",
        "price_band"
    ],
    "attribution": {
        "attributionCalculateType": "ALL",
        "compareDimension": "store_type",
        "timeConstraint": "DateTrunc([metric_time],\"DAY\") = DateTrunc(Cast(\"2025-07-03 00:00:00\",\"Timestamp\"),\"DAY\")",
        "currentObject": {
            "filters": [
                "IN([store_type],\"自营\")"
            ]
        },
        "compareObject": {
            "filters": [
                "IN([store_type],\"加盟\")"
            ]
        }
    }
}

说明:


5. 响应参数

5.1 顶层结构

字段名 类型 描述
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。排查接口问题时请提供该值,便于服务端定位日志

每个元素结构如下:

字段名 类型 描述
dimension String | null 当前归因结果对应的维度名,例如 brandprice_band。为 null 时表示当前对象和对比对象的总体汇总,不对应某个具体维度
table Object 当前维度下的归因结果表。表内各数组按相同下标对齐,例如 brand[0]@currentValue[0]@compareValue[0]@growth[0] 表示同一行数据
warning String | null 当前维度的告警信息,例如该维度无可归因数据;没有告警时为 null
empty Boolean 当前维度结果是否为空。

5.2 table 内部字段定义

字段名 类型 描述
<各维度字段名> Array 每个维度的值列表,如 "brand": ["乐品","康逸"]。字段名与 dimension 的值一致;总体汇总行 dimension = null 时不返回维度字段
@currentValue Array 当前对象的指标值,例如 currentObject 过滤条件对应的自营门店指标值。与维度值数组按下标一一对应
@compareValue Array 对比对象的指标值,例如 compareObject 过滤条件对应的加盟门店指标值。与 @currentValue 使用相同下标对齐
@growth Array 增长值,通常为 @currentValue - @compareValue。大于 0 表示当前对象高于对比对象,小于 0 表示当前对象低于对比对象
@growthRate Array 增长率,通常为 (@currentValue - @compareValue) / @compareValue。对比值为 0 或不可计算时可能为 null,前端需要兜底展示
@overallContributionRate Array 整体贡献率,用于表示该维度项对当前对象与对比对象总体差异的贡献占比。仅维度归因表中返回;总体汇总行通常不返回该字段

5.3 响应示例

{
    "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": "cced9e32f6aa4b3eb03905b51054ad4d.192.17780531304030053"
}

6.调用示例

请求示例

{
    "metric": "countDistinct_product_id",
    "dimensions": [
        "brand",
        "price_band"
    ],
    "attribution": {
        "attributionCalculateType": "ALL",
        "compareDimension": "store_type",
        "timeConstraint": "DateTrunc([metric_time],\"DAY\") = DateTrunc(Cast(\"2025-07-03 00:00:00\",\"Timestamp\"),\"DAY\")",
        "currentObject": {
            "filters": [
                "IN([store_type],\"自营\")"
            ]
        },
        "compareObject": {
            "filters": [
                "IN([store_type],\"加盟\")"
            ]
        }
    }
}

响应示例

{
    "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": "cced9e32f6aa4b3eb03905b51054ad4d.192.17780531304030053"
}

6.1实现下钻效果

查询请求

curl --location --request POST 'http://127.0.0.1:8083/anymetrics/api/v1/attribution/dim_peer_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 '{

    "metric": "countDistinct_product_id",
    "dimensions": [
        "price_band"
    ],
    "attribution": {
        "attributionCalculateType": "ALL",
        "compareDimension": "store_type",
        "timeConstraint": "DateTrunc([metric_time],\"DAY\") = DateTrunc(Cast(\"2025-07-03 00:00:00\",\"Timestamp\"),\"DAY\")",
        "filters": [
            "IN([brand],\"乐品\")"
        ],
        "currentObject": {
            "filters": [
                "IN([store_type],\"自营\")"
            ]
        },
        "compareObject": {
            "filters": [
                "IN([store_type],\"加盟\")"
            ]
        }
    }
}'

响应示例

{
    "data": {
        "tables": [
            {
                "dimension": "price_band",
                "table": {
                    "@currentValue": [
                        1,
                        3,
                        2
                    ],
                    "@compareValue": [
                        1,
                        3,
                        2
                    ],
                    "@overallContributionRate": [
                        null,
                        null,
                        null
                    ],
                    "price_band": [
                        "0-20",
                        "40-60",
                        "20-40"
                    ],
                    "@growthRate": [
                        "0",
                        "0",
                        "0"
                    ],
                    "@growth": [
                        "0",
                        "0",
                        "0"
                    ]
                },
                "warning": null,
                "empty": false
            },
            {
                "dimension": null,
                "table": {
                    "@currentValue": [
                        6
                    ],
                    "@compareValue": [
                        6
                    ],
                    "@growthRate": [
                        "0"
                    ],
                    "@growth": [
                        "0"
                    ]
                },
                "warning": null,
                "empty": false
            }
        ],
        "warning": null
    },
    "success": true,
    "code": "200",
    "errorMsg": null,
    "detailErrorMsg": null,
    "traceId": "2537fe72c3f84f51892047a537d3592a.187.17780530872390023"
}