无法通过API v4定义的段访问群组数据

时间:2016-06-09 13:40:46

标签: python google-analytics google-analytics-api google-analytics-v4

我尝试使用下面的Python代码访问具有同类群组维度的数据:

return analytics.reports().batchGet(
    body={
      'reportRequests': [
      {
        'viewId': VIEW_ID, 
        'metrics': [
          {"expression": "ga:cohortActiveUsers"},
          {"expression": "ga:cohortTotalUsers"}
        ],
        "dimensions":
          [
            {
              "name": "ga:segment"
            },
            {
              "name": "ga:cohort"
            },
            {
              "name": "ga:cohortNthDay"
            }
          ],
        "segments": [
          {
            "dynamicSegment":
              {
                "name": "Sessions from Poznan",
                "userSegment":
                  {
                    "segmentFilters": [
                      {
                        "simpleSegment":
                          {
                            "orFiltersForSegment":
                              {
                                "segmentFilterClauses": [
                                  {
                                    "dimensionFilter":
                                      {
                                        "dimensionName": "ga:city",
                                        "operator": "EXACT",
                                        "expressions": ["Poznan"]
                                      }
                                  }]
                              }
                          }
                      }]
                  }
              }
          }],
        "cohortGroup": {
          "cohorts": [{
            "name": "cohort 1",
            "type": "FIRST_VISIT_DATE",
            "dateRange": {"startDate": "2016-05-10", "endDate": "2016-05-10"}
          }, {
            "name": "cohort 2",
            "type": "FIRST_VISIT_DATE",
            "dateRange": {"startDate": "2016-05-10", "endDate": "2016-05-10"}
          }]
        }
      }]
    }
).execute()

执行此代码时,收到错误Selected dimensions and metrics cannot be queried together。但是当删除segments部分时,一切正常。

出现此类错误的原因是什么?我该如何正确地请求这些数据?

1 个答案:

答案 0 :(得分:0)

针对具体报告,如https://developers.google.com/analytics/devguides/reporting/core/v4/advanced#cohort_and_lifetime_value_ltv_dimensions_and_metrics所述:

在您的同类群组报告中,没有维度“ga:segment”用作维度。您应该将自己限制在群组和文档的文档中提到的维度和指标。终身报告。

希望这有帮助。