如何使用batchRunReports?如何同时批量查询多个不同属性?
如何通过batchRunReports批量获取多个不同属性的数据
你目前使用的batchRunReports接口是针对单个Google Analytics属性的(请求URL中的${property}为单个属性ID,比如properties/123456:batchRunReports),无法在同一个batchRunReports请求的requests数组中直接指定多个不同属性。要一次性批量获取多个属性的数据,你可以使用Google API的批量请求机制,将多个针对不同属性的batchRunReports请求打包成一个HTTP请求发送。
批量请求的实现方式
- 请求URL:
https://analyticsdata.googleapis.com/batch - 请求头:设置
Content-Type为multipart/mixed; boundary=YOUR_BOUNDARY,其中YOUR_BOUNDARY是自定义的分隔符(比如batch_boundary_123) - 请求体:由多个子请求组成,每个子请求对应一个属性的
batchRunReports调用,子请求之间用分隔符分隔,最后以分隔符结尾。
示例请求体
假设你要查询属性123456和属性789012的数据,请求体如下:
--batch_boundary_123 Content-Type: application/http Content-Transfer-Encoding: binary POST /v1beta/properties/123456:batchRunReports HTTP/1.1 Content-Type: application/json { "requests": [ { "dateRanges": [{"startDate": "2023-11-08", "endDate": "2023-11-08"}], "dimensions": [{"name": "country"}], "metrics": [{"name": "totalUsers"}], "orderBys": [{"desc": true, "metric": {"metricName": "totalUsers"}}] } ] } --batch_boundary_123 Content-Type: application/http Content-Transfer-Encoding: binary POST /v1beta/properties/789012:batchRunReports HTTP/1.1 Content-Type: application/json { "requests": [ { "dateRanges": [{"startDate": "2023-11-08", "endDate": "2023-11-08"}], "dimensions": [{"name": "country"}], "metrics": [{"name": "totalUsers"}], "orderBys": [{"desc": true, "metric": {"metricName": "totalUsers"}}] } ] } --batch_boundary_123--
注意事项
- 每个子请求的结构和你之前调用单个属性
batchRunReports的请求结构完全一致,只是URL中的属性ID不同。 - 分隔符需要前后一致,且不能出现在任何子请求的内容中。
- 批量请求的响应也会是multipart格式,每个子响应对应一个子请求的结果,你需要按分隔符拆分后分别解析。
内容的提问来源于stack exchange,提问作者vinnitu
相关产品推荐
相关产品推荐

