How to use nextPageToken in Google Analytics API using PHP


Have a good day,

I really hope to get the next set of data after getting the first 10,000 Google Analytics data. After further research, there's one thing called nextpagetoken, and I'm embarrassed to get the next set of data. I tried it but I kept getting an error or I did not have any data. Does anyone have the same problem before? Help me with this (code example very appreciated). Thank you very much.

The answer

You must obtain the followingPageToken value from each response and configure all the following requests:

 $client = new \Google_Client(); /* ... */ $analytics = new \Google_Service_AnalyticsReporting($client); $request = new \Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($view); $request->setMetrics($metrics); $request->setDateRanges($dateRange); $request->setPageSize(10000); /* ... */ $body = new \Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests(array($request)); do { $response = $analytics->reports->batchGet($body, $params); /* ... Processing $response ... */ $request->setPageToken($response[0]->getNextPageToken()); } while ($response[0]->getNextPageToken() != '');