Google 分析 API 多个查询 - 第二个功能?
Google Analytics API Multiple Queries - Second Function?
这是一个比较新手的问题,请多多包涵...
我已成功授权我的 Google Analytics API 帐户并进行以下调用,它成功打印了跳出率。
我想添加更多查询,例如综合浏览量和会话,我该怎么做?
这些函数让我很困惑,我需要为每个查询创建一个新函数吗?
提前致谢!
汤姆
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,
'today',
'today',
'ga:bouncerate');
}
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 "<p>First view (profile) found: $profileName</p>";
print "<p>Total sessions: $sessions</p>";
} else {
print "<p>No results found.</p>";
}
您的代码的以下部分是实际发出 API 请求的部分:
$analytics->data_ga->get(
'ga:' . $profileId,
'today',
'today',
'ga:bouncerate');
如果要进行其他查询,则必须再次调用 $analytics->data_ga->get()
方法。您不需要为每个查询编写一个新函数(如果您不想),但您需要每次都调用 $analytics->data_ga->get()
。您选择如何重用该位取决于您。
这是一个比较新手的问题,请多多包涵...
我已成功授权我的 Google Analytics API 帐户并进行以下调用,它成功打印了跳出率。
我想添加更多查询,例如综合浏览量和会话,我该怎么做?
这些函数让我很困惑,我需要为每个查询创建一个新函数吗?
提前致谢! 汤姆
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,
'today',
'today',
'ga:bouncerate');
}
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 "<p>First view (profile) found: $profileName</p>";
print "<p>Total sessions: $sessions</p>";
} else {
print "<p>No results found.</p>";
}
您的代码的以下部分是实际发出 API 请求的部分:
$analytics->data_ga->get(
'ga:' . $profileId,
'today',
'today',
'ga:bouncerate');
如果要进行其他查询,则必须再次调用 $analytics->data_ga->get()
方法。您不需要为每个查询编写一个新函数(如果您不想),但您需要每次都调用 $analytics->data_ga->get()
。您选择如何重用该位取决于您。