I've included Google API PHP SDK to my website, connected to Analytics API and could get the result of sessions for last 30 days.
This is my code:
// Replace with your view ID, for example XXXX.
$VIEW_ID = "193259946";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("30daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
This is what I get, it's ok:
sessions: 23
So my question is: How to get the response in format day => value?
I.E. ['01.01.2019' => 3, '02.01.2019' => 5]
PS. My website is(will be) hosted on the server without domain name, but with IP, so I couldn't get a API client ID. I', using JSON key to connect to API server.