1

Good day,

Is there a way to get the next 10,000 data from google analytics API? I would want to get the next set of data after getting the first 10,000. Is there a way to accomplish that? I am using google analytics api php client libraries.

Here is my code:

 <?php
 $analytics = initializeAnalytics();     
 $response = getReport($analytics);  
 printResults($response);

function initializeAnalytics()
{
    $KEY_FILE_LOCATION = __DIR__ . 'MyFileDirectory';
    // Create and configure a new client object.
    $client = new Google_Client();
    $client->setApplicationName("Hello Analytics Reporting");
    $client->setAuthConfig($KEY_FILE_LOCATION);
    $client-
>setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    $analytics = new Google_Service_AnalyticsReporting($client);
    return $analytics;
 }

function getReport($analytics) {
    // Replace with your view ID, for example XXXX.
    $VIEW_ID = "MyViewId";

    // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("2017-04-01");
    $dateRange->setEndDate("2017-06-19");

    // Create the Metrics object.
    $totalEvents = new Google_Service_AnalyticsReporting_Metric();
    $totalEvents->setExpression("ga:totalEvents");
    $totalEvents->setAlias("totalEvents");

    //Create the Dimensions object.
    $clientId = new Google_Service_AnalyticsReporting_Dimension();
    $clientId->setName("ga:dimension4");
    $sessionId = new Google_Service_AnalyticsReporting_Dimension();
    $sessionId->setName("ga:dimension5");
    $eventLabel = new Google_Service_AnalyticsReporting_Dimension();
    $eventLabel->setName("ga:eventLabel");
    $timestamp = new Google_Service_AnalyticsReporting_Dimension();
    $timestamp->setName("ga:dimension3");

    // Create the ReportRequest object.
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId($VIEW_ID);
    //set number of rows
    $request->setPageSize(10000);
    $request->setDateRanges($dateRange);
    $request->setMetrics(array($totalEvents));
    $request->setDimensions(array($clientId,$sessionId,$eventLabel, 
    $timestamp));   
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests( array( $request) );
    return $analytics->reports->batchGet( $body );
}
}
?>
12
  • @JordiNebot Thank you for the suggestion. I will try to put this on my code. I hope this is the answer.
    – Ben
    Sep 13, 2017 at 7:56
  • @JordiNebot I tried it and the error is "Call to a member function getNextPageToken() on null"
    – Ben
    Sep 13, 2017 at 8:22
  • @ben your going to have to do a var dump of the response and figure out where nextpagetoken is and use that. Analyics v4 is different then the other APIs the standard pagination methods are not going to work. I had to re-write it myself for C# Sep 18, 2017 at 6:53
  • @DaImTo did you use PHP before? Let me try what you suggested. Have you successfully tried it on C#?
    – Ben
    Sep 18, 2017 at 8:18

1 Answer 1

3

This is going to get the first 10 pages of data. I limited it to 10 to prevent it from eating my quota. If you remove that it will continue to chug away getting all the rows and eating up your quota.

NOTE: This is only going to work if you have a single report. If you have more then one report then you are going to have to do some fancy stuff to apply the correct pageToken to each report and remove the reports which are complete. At this time there is no report Id or way of matching up what you sent to the report that is returned other than its place in the reports array.

$service = new Google_Service_AnalyticsReporting($client); 

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2016-01-01");
$dateRange->setEndDate("2017-06-30");

// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");

//Create the Dimensions object.
$date = new Google_Service_AnalyticsReporting_Dimension();
$date->setName("ga:date");
$pagePath = new Google_Service_AnalyticsReporting_Dimension();
$pagePath->setName("ga:pagePath");

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("81692014");
$request->setPageSize("10000");  
$request->setDateRanges($dateRange);
$request->setDimensions(array($date,$pagePath));
$request->setMetrics(array($sessions));
$request->setMetrics(array($sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$data =  $service->reports->batchGet( $body );


// Remove count if you really want everything.
$cnt = 0; 
while ($data->reports[0]->nextPageToken > 0 && $cnt < 10) {
    // There are more rows for this report.
    $body->reportRequests[0]->setPageToken($data->reports[0]->nextPageToken);
    $data =  $service->reports->batchGet( $body );
    $cnt++;
    }

I put a tutorial up on it Google Analytics V4 pagination and the full code i used can be found on GitHub note this code was ripped from my sample project.

2
  • 1
    @DalmTo, thank you for the tutorial. It was really a very big help. It works now. I got the right data and inserted those data on my database.
    – Ben
    Sep 20, 2017 at 8:59
  • Please remember to accept the answer. You will also get points when you accept welcome to stack overflow Sep 20, 2017 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.