gpt4 book ai didi

php - Google Analytics,显示超过 1 个查询

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

我对 PHP 还很陌生,所以请耐心等待:)

是的,我正在使用谷歌分析,并且我正在使用他们的脚本之一来显示一个查询。看起来一切都不错,但我不太确定如何显示更多?

我知道要获取查询,但在显示它时遇到问题。我只能显示1。我只有 session ,但我想添加更多内容,例如跳出率等。

这是我正在使用的代码:

  <?php

require_once 'google-client/vendor/autoload.php';


session_start();


$client = new Google_Client();
$client->setAuthConfig('google-client/src/Google/client_secret.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

$client->setAccessToken($_SESSION['access_token']);


$analytics = new Google_Service_Analytics($client);


$profile = getFirstProfileId($analytics);

$results = getResults($analytics, $profile);
printResults($results);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/acp/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}


function getFirstProfileId($analytics) {

$accounts = $analytics->management_accounts->listManagementAccounts();

if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();


$properties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);

if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();


$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstPropertyId);

if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();


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) {

return $analytics->data_ga->get(
'ga:146790870',
'2016-11-01',
'today',
'ga:sessions');

return $analytics->data_ga->get(
'ga:146790870',
'2016-11-01',
'today',
'ga:percentNewSessions');
}
function printResults($results) {

if (count($results->getRows()) > 0) {


$profileName = $results->getProfileInfo()->getProfileName();


$rows = $results->getRows();
$sessionstotal = $rows[0][0];


// Print the results.
print "<div class='col s12 m6 l3' style='text-align:center;'>
<div class='card green '>
<div class='card-content white-text'>
<span class='card-title'>Total Sessions</span>
<p style='font-size: 1.8rem; font-weight: bold;'>$sessionstotal</p>
</div>
<div class='card-action green darken-2'>
</div>
</div>
</div>";
} else {
print "<p>No results found.</p>";
}
}
?>

如果有人可以给我任何关于如何改进它的提示,或者该怎么做,请帮助我:) 请记住,我对 PHP 的了解非常有限,因为我是在做项目时学习它的。

还是谢谢你

最佳答案

尚不清楚您是否需要进行多个查询,或者只是需要向现有查询添加更多指标。例如,您可以在同一查询中查询 ga:sessionsga:percentNewSessions

return $analytics->data_ga->get(
'ga:146790870',
'2016-11-01',
'today',
'ga:sessions, percentNewSessions');

然后您需要从结果中提取第二个指标:

  $rows = $results->getRows();
$sessionstotal = $rows[0][0];
$percentNewSessions = $rows[0][1];


// Print the results.
print "<div class='col s12 m6 l3' style='text-align:center;'>
<div class='card green '>
<div class='card-content white-text'>
<span class='card-title'>Total Sessions</span>
<p style='font-size: 1.8rem; font-weight: bold;'>$sessionstotal</p>
<span class='card-title'>Percent New Sessions</span>
<p style='font-size: 1.8rem; font-weight:bold;'>$percentNewSessions</p>
</div>
<div class='card-action green darken-2'>
</div>
</div>
</div>";

尝试一下结果对象,直到您了解其结构。始终使用 Reference docs了解哪些字段可用。

关于php - Google Analytics,显示超过 1 个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43260229/

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