gpt4 book ai didi

php - 如何使用 php 从 google Analytics api 获取接下来的 10,000 条数据?

转载 作者:行者123 更新时间:2023-12-02 04:30:19 24 4
gpt4 key购买 nike

美好的一天,

有没有办法从 Google Analytics API 获取接下来的 10,000 个数据?我想在获得前 10,000 个数据后获得下一组数据。有办法实现吗?我正在使用 google Analytics 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我使用的完整代码可以在 GitHub 上找到请注意,此代码是从我的示例项目中删除的。

关于php - 如何使用 php 从 google Analytics api 获取接下来的 10,000 条数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46191768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com