google 分析 api 循环
google analytics api loop
在下面的文件中,我得到了最后一天的访问者数量的数据。
现在,我想遍历过去 30 天并获取每一天的数据。
我认为,我需要更改三个部分。
即,$results = getResults($analytics, $profile)
;、printresults($results)
及以下:
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'1daysAgo',
'today',
'ga:sessions');
}
现在我不确定如何完成它。
我在想一些类似的事情:
for($i = 0; $i < 30; $i++{
$results = '$results' . '$i';
function getResults($analytics, $profileId){
return $analytics->data_ga->get(
'ga:' . $profileId,
'$i' . 'daysAgo',
'($i - 1)' . 'daysAgo,
'ga:sessions');
}
printresults('$results' . '$i');
}
但这不起作用。关于如何多次 运行 这个查询有什么建议吗?我想我做的不对。我可能必须创建一个数组,在其中存储 $results
的变量名,然后循环遍历它。但是由于 $results
在几个函数中返回,我将如何在所有这些函数中循环遍历它?
另外,关于这个我还有一个问题。也就是说,如果有访客,$results
只会 return 一些东西。否则,它不可数并且 return 是一个错误。我想抑制这个错误,而不是输出 0
.
这适用于我在另一个文件中编写的函数,但再次不适用于循环。
为了google 分析代码的完整性:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<title>Document</title>
</head>
<body>
<script>
</script>
<?php
// Load the Google API PHP Client Library.
require_once __DIR__ . '/google/vendor/autoload.php';
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
printResults($results);
function getFirstProfileId($analytics) {
// Get the user's first view (profile) ID.
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();
// Get the list of views (profiles) for the authorized user.
$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstPropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
// Return the first view (profile) ID.
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No properties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'1daysAgo',
'today',
'ga:sessions');
}
function printResults($results) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {
// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();
// Get the entry for the first entry in the first row.
$rows = $results->getRows();
$sessions = $rows[0][0];
// Print the results.
print $sessions;
} else {
print "No results found.\n";
}
}
?>
</body>
</html>
除非您有非常具体的原因创建一个 API 循环来分别提取每个数据的日期(例如 to avoid sampling),否则不要这样做:
- 它会无缘无故地产生 processing/API 开销(请参阅下面的替代方法)
- 你增加了 reaching the API limits
的机会
要查询每日数据,只需插入 ga:date
维度即可按天细分数据。
此外,您应该使用 Query Explorer 来确保您的查询在尝试实施之前有效,这将节省您的时间:
最后但同样重要的是,there is a data processing latency which can be up to 24 to 48 hours,因此如果您只查询昨天或今天,您可能看不到任何数据。
在下面的文件中,我得到了最后一天的访问者数量的数据。 现在,我想遍历过去 30 天并获取每一天的数据。
我认为,我需要更改三个部分。
即,$results = getResults($analytics, $profile)
;、printresults($results)
及以下:
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'1daysAgo',
'today',
'ga:sessions');
}
现在我不确定如何完成它。 我在想一些类似的事情:
for($i = 0; $i < 30; $i++{
$results = '$results' . '$i';
function getResults($analytics, $profileId){
return $analytics->data_ga->get(
'ga:' . $profileId,
'$i' . 'daysAgo',
'($i - 1)' . 'daysAgo,
'ga:sessions');
}
printresults('$results' . '$i');
}
但这不起作用。关于如何多次 运行 这个查询有什么建议吗?我想我做的不对。我可能必须创建一个数组,在其中存储 $results
的变量名,然后循环遍历它。但是由于 $results
在几个函数中返回,我将如何在所有这些函数中循环遍历它?
另外,关于这个我还有一个问题。也就是说,如果有访客,$results
只会 return 一些东西。否则,它不可数并且 return 是一个错误。我想抑制这个错误,而不是输出 0
.
这适用于我在另一个文件中编写的函数,但再次不适用于循环。
为了google 分析代码的完整性:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<title>Document</title>
</head>
<body>
<script>
</script>
<?php
// Load the Google API PHP Client Library.
require_once __DIR__ . '/google/vendor/autoload.php';
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
printResults($results);
function getFirstProfileId($analytics) {
// Get the user's first view (profile) ID.
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();
// Get the list of views (profiles) for the authorized user.
$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstPropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
// Return the first view (profile) ID.
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No properties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'1daysAgo',
'today',
'ga:sessions');
}
function printResults($results) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {
// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();
// Get the entry for the first entry in the first row.
$rows = $results->getRows();
$sessions = $rows[0][0];
// Print the results.
print $sessions;
} else {
print "No results found.\n";
}
}
?>
</body>
</html>
除非您有非常具体的原因创建一个 API 循环来分别提取每个数据的日期(例如 to avoid sampling),否则不要这样做:
- 它会无缘无故地产生 processing/API 开销(请参阅下面的替代方法)
- 你增加了 reaching the API limits 的机会
要查询每日数据,只需插入 ga:date
维度即可按天细分数据。
此外,您应该使用 Query Explorer 来确保您的查询在尝试实施之前有效,这将节省您的时间:
最后但同样重要的是,there is a data processing latency which can be up to 24 to 48 hours,因此如果您只查询昨天或今天,您可能看不到任何数据。