1
votes

Below is the page for API v4 to get GA data

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

My request :

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

Response :

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

Query Explorer uses the the same ViewID and request parameter and it is fetching the data properly. What I am missing?

2

2 Answers

2
votes

You are missing the viewId key in your first ReportRequest object. Having viewId standalone in a separate object doesn't work. Try this code instead:

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

You are missing a dimension like ga:date. Here is the list with possible dimensions to use in the GA-API: https://developers.google.com/analytics/devguides/reporting/core/dimsmets