首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google分析,显示超过一个查询

Google分析,显示超过一个查询
EN

Stack Overflow用户
提问于 2017-04-06 15:58:57
回答 1查看 46关注 0票数 1

我对PHP非常陌生,所以请容忍我:)

对,所以我正在使用谷歌分析,我使用他们的脚本之一来显示一个查询。看上去很好,但我不太确定如何显示更多?

我知道如何得到一个查询,但我在显示它时遇到了困难。我只能显示1。我只有会话,但我想增加更多,例如,反弹率等。

下面是我使用的代码:

代码语言:javascript
复制
  <?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的知识非常有限。

不管怎么都要谢谢您

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-06 16:56:31

不清楚您是需要进行多个查询,还是只需要向现有查询中添加更多的度量标准。例如,可以在同一个查询中查询ga:sessionsga:percentNewSessions

代码语言:javascript
复制
return $analytics->data_ga->get(
    'ga:146790870',
    '2016-11-01',
    'today',
    'ga:sessions, percentNewSessions');

然后,您需要从结果中提取第二个度量:

代码语言:javascript
复制
  $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>";

在了解结果对象的结构之前,先对其进行处理。始终使用参考文档来了解哪些字段可用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43260229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档