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

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"
  }
}


查询资源管理器使用相同的ViewID和request参数,并且正在正确获取数据。我缺少什么?

最佳答案

您在第一个viewId对象中缺少ReportRequest键。在单独的对象中单独使用viewId无效。请尝试以下代码:

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

10-04 16:48