如何使用 php 从 google analytics api 获取接下来的 10,000 条数据?
How to get the next 10,000 data from google analytics api using php?
美好的一天,
有没有办法从 google 分析 API 中获取接下来的 10,000 条数据?
我想在获得前 10,000 之后获得下一组数据。有没有办法做到这一点?我正在使用 google 分析 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 );
}
}
?>
这将获取前 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 and the full code i used can be found on GitHub注意这段代码是从我的示例项目中删除的。
美好的一天,
有没有办法从 google 分析 API 中获取接下来的 10,000 条数据? 我想在获得前 10,000 之后获得下一组数据。有没有办法做到这一点?我正在使用 google 分析 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 );
}
}
?>
这将获取前 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 and the full code i used can be found on GitHub注意这段代码是从我的示例项目中删除的。