[英]Google Analytics Python API “Invalid JSON payload received. Unknown name ”segment“ at 'report_requests[0]': Cannot find field.”
I followed the Google Analytics (GA) Hello Analytics tutorial and used the query explorer to call the GA API via Python and this worked great until I tried to include a segment in my query.我遵循了 Google Analytics (GA) Hello Analytics 教程,并使用查询资源管理器通过 Python 调用 GA API,这非常有效,直到我尝试在查询中包含一个段。 It seems that the query explorer's use of segments (or my understanding of how to use this) is not correct.
似乎查询资源管理器对段的使用(或我对如何使用它的理解)不正确。 Here is my get_report function:
这是我的 get_report function:
def get_report(analytics, segment):
if segment == "all":
bdy = {
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '2021-01-19', 'endDate': 'yesterday'}],
'metrics': [{'expression': 'ga:uniqueEvents'}],
'dimensions': [{'name': 'ga:eventLabel'}]
}]
}
elif segment == "internal":
bdy = {
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '2021-01-19', 'endDate': 'yesterday'}],
'metrics': [{'expression': 'ga:uniqueEvents'}],
'dimensions': [{'name': 'ga:eventLabel'}],
"segment": "gaid::aqYF4QvFTX6JhLKfs0R84A"
}]
}
return analytics.reports().batchGet(body=bdy).execute()
The first if block works perfectly fine but when I try to run the second I get this error:第一个 if 块工作得很好,但是当我尝试运行第二个时,我得到了这个错误:
HttpError: <HttpError 400 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Invalid JSON payload received. Unknown name "segment" at 'report_requests[0]': Cannot find field."
HttpError:<HttpError 400 在请求https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json时返回“无效 JSON 有效负载收到。在'report 中找不到未知名称“report_”。 " .
. Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'report_requests[0]', 'description': 'Invalid JSON payload received. Unknown name "ga:segment" at 'report_requests[0]': Cannot find field.'}]}]">
详细信息:“[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'report_requests[0]', 'description': '收到无效的 JSON 有效载荷. 'report_requests[0]' 中的未知名称“ga:segment”:找不到字段。'}]}]">
It seems like the "segment" key in the second body dictionary is not correct, but I can't find what it should be.似乎第二个正文字典中的“段”键不正确,但我找不到它应该是什么。 Does anyone know how to correct this?
有谁知道如何纠正这个?
In case this is helpful, this is what the query explorer suggested I use:如果这有帮助,这是查询资源管理器建议我使用的:
"query": {
"start-date": "2021-01-19",
"end-date": "yesterday",
"ids": "ga:xxxxxxxx",
"dimensions": "ga:eventCategory,ga:eventLabel",
"metrics": [
"ga:uniqueEvents"
],
"segment": "gaid::aqYF4QvFTX6JhLKfs0R84A",
"start-index": 1,
"max-results": 1000
} }
I think the query explorer might use v3 of the API, while I'm using v4....我认为查询资源管理器可能使用 API 的 v3,而我使用的是 v4....
Thanks in advance!提前致谢!
Try尝试
"segments":[
{
"segmentId": "gaid::aqYF4QvFTX6JhLKfs0R84A"
}]
https://developers.google.com/analytics/devguides/reporting/core/v4/samples#segments https://developers.google.com/analytics/devguides/reporting/core/v4/samples#segments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.