Google Analytics Data API (GA4) Python上的自定义维度



我正在尝试使用这里描述的快速启动代码制作报告,它工作得很好,但是当我尝试添加自定义维度时,我获得以下错误

google.api_core.exceptions。字段uid不是一个有效的维度。有关有效维度和指标的列表,请参见https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema

我能够在谷歌分析中心使用这个自定义维度做报告,所以我不明白为什么我得到这个错误,这是代码(自定义维度是uid)

def sample_run_report(property_id):
"""Runs a simple report on a Google Analytics App+Web property."""
# Using a default constructor instructs the client to use the credentials
# specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
client = AlphaAnalyticsDataClient(credentials=credentials)
request = RunReportRequest(
entity=Entity(property_id=property_id),
dimensions=[Dimension(name='uid')],
metrics=[Metric(name='userEngagementDuration')],
date_ranges=[DateRange(start_date='2020-07-01',end_date='today')])
response = client.run_report(request)
print("Report result:")

for row in response.rows:
print(row.dimension_values[0].value, row.metric_values[0].value)
def main():
sample_run_report(xxxxxx)
if __name__ == '__main__':
main()
编辑:我可以使用name="customUser:uid"创建查询

尝试在请求中从uid更新到customEvent:uid。自定义维度的语法文档在这里。

您可以查询元数据API方法以列出此属性的已注册自定义尺寸。请参阅查询示例。元数据方法可能会返回类似于:

的维度
"dimensions": [
...
{
"apiName": "customEvent:uid",
"uiName": "uid",
"description": "An event scoped custom dimension for your Analytics property."
},
...
],

如果Metadata方法没有返回自定义维度,则没有注册此属性。因为您说"我能够在google分析中心使用此自定义维度制作报告",您可能已经注册了此维度。

最新更新