I tried to run query on Analytics Reporting using the "google/apiclient" and based on example on Google Analytics Reporting v4 documentation. It is returning records, but always only 10. NextPageToken is null in response. Response has only one report. Request has set PageSize for 100 records. I tried many configuration PageSize: 100/500/1000 and PageToken: 1/100/1000/abc and always return only 10 records. I created the same report on page and it has 1169 pages for 10 records on page (for the same date range). I looked on limit quotas for API and it has default value.
Did i miss something in code or may be there something problem with configuration service account (some limitation for service account)?
private function getReport($analytics, $VIEW_ID) {
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2020-04-01");
$dateRange->setEndDate("2020-04-12");
$quantityCheckedOut = new Google_Service_AnalyticsReporting_Metric();
$quantityCheckedOut->setExpression("ga:quantityCheckedOut");
$quantityCheckedOut->setAlias("itemQuantity");
$dimension1 = new Google_Service_AnalyticsReporting_Dimension();
$dimension1->setName("ga:productSku");
$pivots = new Google_Service_AnalyticsReporting_Pivot();
$pivots->setDimensions([$dimension1]);
$pivots->setMetrics($quantityCheckedOut);
$order = new Google_Service_AnalyticsReporting_OrderBy();
$order->setFieldName("ga:quantityCheckedOut");
$order->setSortOrder("ASCENDING");
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setPageSize("100");
$request->setHideValueRanges(true);
$request->setMetrics([$quantityCheckedOut]);
$request->setPivots($pivots);
$request->setOrderBys([$order]);
$request->setSamplingLevel("LARGE");
return $request;
}
private function initializeAnalytics()
{
$KEY_FILE_LOCATION = $this->initFolder . '/google.json';
$filesystemAdapter = new Local($this->cacheFolder);
$filesystem = new Filesystem($filesystemAdapter);
$client = new Google_Client();
$client->setApplicationName("App Name");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setIncludeGrantedScopes(true);
$cache = new FilesystemCachePool($filesystem);
$client->setCache($cache);
$client->addScope(Google_Service_AnalyticsReporting::ANALYTICS_READONLY);
$analytics = new Google_Service_AnalyticsReporting($client);
return $analytics;
}
private function printResults($reports)
{
$nextToken = 0;
/** @var Google_Service_AnalyticsReporting_Report $report */
foreach ($reports->getReports() as $report) {
$nextToken = $report->getNextPageToken();
}
return $nextToken
}
public function run(){
/** @var Google_Service_AnalyticsReporting $analytics */
$analytics = $this->initializeAnalytics();
$pageToken = 1;
$i = 0;
$request = $this->getReport($analytics, "VIEW_IDxxxx");
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
while($i < 1) {
$request->setPageToken((string)$i);
$body->setReportRequests($request);
$response = $analytics->reports->batchGet($body);
$pageToken = $this->printResults($response);
$i++;
}
}