如何使用php从谷歌分析API获取下一个10,000数据?

时间:2017-09-13 07:46:26

标签: php google-analytics google-api google-analytics-api google-api-php-client

美好的一天,

有没有办法从谷歌分析API获取下一个10,000个数据? 在获得前10,000个数据后,我想获得下一组数据。有办法实现吗?我正在使用谷歌分析api php客户端库。

这是我的代码:

 <?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 个答案:

答案 0 :(得分:2)

这将获得前10页的数据。我将它限制在10以防止它吃掉我的配额。如果你删除它,它将继续阻止获取所有行并占用你的配额。

注意:只有在您有一份报告时才会起作用。如果您有多个报告,那么您将不得不做一些花哨的事情来将正确的pageToken应用于每个报告并删除完整的报告。此时,没有报告ID或方式将您发送到报告的内容与报告数组中的位置匹配。

$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++;
    }

我在其上添加了一个教程Google Analytics V4 pagination,我可以在GitHub找到我使用的完整代码,注意这个代码是从我的示例项目中删除的。