无法在GA-v4 API资源管理器上查询

时间:2018-01-09 15:46:25

标签: google-analytics google-apis-explorer

以下是获取GA数据的API v4页面

https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet

我的要求:

 {
  "reportRequests": [
    {
      "dateRanges": [
        {
          "startDate": "7daysAgo",
          "endDate": "yesterday"
        }
      ],
      "metrics": [
        {
          "expression": "ga:users"
        }
      ]
    },
    {
      "viewId": "ga:123456"
    }
  ]
}

回复:

{
  "error": {
    "code": 400,
    "message": "Parameter viewId must be set.",
    "status": "INVALID_ARGUMENT"
  }
}

Query Explorer使用相同的ViewID和request参数,它正在正确获取数据。我缺少什么?

2 个答案:

答案 0 :(得分:1)

您错过了第一个viewId对象中的ReportRequest密钥。将viewId独立放在单独的对象中并不起作用。请尝试使用此代码:

{
  "reportRequests": [
    {
      "viewId": "ga:123456",  //viewId defined within the ReportRequest object itself
      "dateRanges": [
        {
          "startDate": "7daysAgo",
          "endDate": "yesterday"
        }
      ],
      "metrics": [
        {
          "expression": "ga:users"
        }
      ]
    }
  ]
}

答案 1 :(得分:0)

您缺少像ga:date这样的维度。以下是可在GA-API中使用的维度的列表:https://developers.google.com/analytics/devguides/reporting/core/dimsmets