Search code examples
google-analytics-api

How to get datewise breakdown of Users data from Google Analytics Reporting v4 REST API?


I'm using the below code.

{ reportRequests: [
                             {
                               viewId: 'XXXXXXXX',
                                 dateRanges:[ 
                                   {
                                       startDate:'2018-01-01',
                                       endDate:'2018-01-31'
                                   }
                                   ],
                               metrics: [
                                 {
                                   expression: 'ga:users'
                                 }                                   
                               ]

                             }
                           ]}

I'm getting the response with total values. I need to get the data broken down date wise. I tried adding dimensions date, But I'm getting an error. What is the right way to get datewise data?


Solution

  • You will need to add the date dimension to your request in order to get your results with the dates.

    {
      "reportRequests":[
      {
        "viewId":"XXXX",
        "dateRanges":[
          {
            "startDate":"2018-01-01",
            "endDate":"2018-01-31"
          }],
        "metrics":[
          {
            "expression":"ga:users"
          }],
        "dimensions": [
          {
            "name":"ga:date"
          }]
        }]
    }
    

    samples