1

美好的一天,如何從谷歌分析API使用PHP獲取下一個10,000數據?

有沒有辦法從谷歌分析API獲取下一個10,000數據? 我想在獲得第一個10,000後獲取下一組數據。有沒有辦法做到這一點?我正在使用谷歌分析API客戶端庫。

這裏是我的代碼:

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

您是否檢查了https://developers.google.com/api-client-library/php/guide/pagination? –

+0

@JordiNebot謝謝你的建議。我會試着把這個放在我的代碼上。我希望這是答案。 – Ben

+0

@JordiNebot我試過了,錯誤是「調用成員函數getNextPageToken()null」 – Ben

回答

1

這是會得到前10頁的數據。我將它限制在10以防止它進食我的配額。如果你刪除它,它將繼續突破所有的行並吃掉你的配額。

注意:如果您有單個報告,這隻會起作用。如果你有多個報告,那麼你將不得不做一些奇特的事情來爲每個報告應用正確的pageToken並刪除已完成的報告。此時沒有報告標識或方式來匹配您發送到報告的報告,而不是報告陣列中它的位置。

$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注意此代碼從我的樣本項目被撕開被發現。

+1

@達爾姆,謝謝你的教程。這真的是一個非常大的幫助。它現在有效。我得到了正確的數據並將這些數據插入到我的數據庫中。 – Ben

+0

請記住接受答案。當你接受堆棧溢出時,你也會得到積分 – DaImTo

相關問題