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

问题描述

我正在尝试使用 Here 中描述的快速入门代码制作报告,它运行良好,但是当我尝试添加自定义维度时,出现以下错误

google.api_core.exceptions.InvalidArgument:400 字段 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。自定义维度的语法为 documented here

您可以查询元数据 API 方法以列出已为此属性注册的自定义维度。请参阅 example of the query。元数据方法可能会返回类似于以下内容的维度:

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

如果元数据方法未返回自定义维度,则不会为该属性注册一个。因为您说“我可以在 google 分析中心使用这个自定义维度制作报告”,所以您可能已经注册了这个维度。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...