How to get the next 10,000 data from google analytics api using php?

戏子无情 提交于 2021-01-28 08:06:15

问题


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 );
}
}
?>

回答1:


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.



来源:https://stackoverflow.com/questions/46191768/how-to-get-the-next-10-000-data-from-google-analytics-api-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!