美好的一天,
我真的想在从Google Analytics获取前10,000个数据后获取下一组数据。经过进一步的研究,有一个名为nextpagetoken的东西,这是我很难得到下一组数据。我已经尝试过,但一直都是错误或者没有数据。以前有人有同样的问题吗?请帮助我(示例代码非常感谢)。非常感谢你。
这是我的代码:
<?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 );
}
function printResults($reports) {
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ )
{
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
echo '<table border="1">';
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
echo '<tr>';
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++)
{
echo '<td>' . ": " . $dimensions[$i] . "\n" .'</td>';
}
for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
echo '<td>' . ": " . $values[$k] . "\n" .'</td>' ;
}
echo '</tr>';
}
}
echo '</table>';
}
}
?>
答案 0 :(得分:1)
您必须从每个响应中获取nextPageToken
值并设置为每个下一个请求:
$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() != '');