谷歌分析,显示多个查询 [英] Google analytics, display more than 1 query

查看:102
本文介绍了谷歌分析,显示多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP非常陌生,请耐心等待:)

对,我使用google analytics,我正在使用其中一个脚本来显示一个查询。它看起来不错,但我不确定如何显示更多?



我知道得到一个查询,但我无法显示它。我只能显示1.
我只有会话,但我想添加更多例如跳出率等。

以下是我正在使用的代码:

<?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 =新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));



函数getFirstProfileId($ analytics){

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


$ properties = $ analytics-> management_webproperties
- > listManagementWebproperties($ firstAccountId);
$ b $ 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 {
抛出新的异常('找不到此用户的视图(配置文件)'。
}
} else {
抛出新的异常('没有找到该用户的属性。');
}
} else {
抛出新的异常('没有为这个用户找到帐户');



function getResults($ analytics,$ profileId){

return $ analytics-> data_ga-> get(
'ga:146790870',
'2016-11-01',
'今天',
'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< 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.8 rem; font-weight:bold;'> $ sessionstotal< / p>
< / div>
< div class ='card-action green darken-2'>
< / div>
< / div>
< / div>;
} else {
print< p>找不到结果。< / p>;
}
}
?>

如果有人可以给我提示如何改善它,或做什么,请帮助我:)请记住我对PHP的知识非常有限,因为我在项目中学习它。



无论如何感谢您

解决方案

目前尚不清楚您是否需要进行多个查询,或者只需要将更多指标添加到现有查询中。例如,您可以在同一查询中查询 ga:sessions ga: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< 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.8 rem; font-weight:bold;'> $ sessionstotal< / p>
< span class ='card-title'>百分比新会话数< / span>
< p style =' font-weight:bold;'> $ percentNewSessions< / p>
< / div>
< div class ='card-action green darken-2'> ;
< / div>
< / div>
< / div>;

玩弄结果对象,直到您了解它的结构。始终使用参考文档了解可用字段。

I'm pretty new to PHP so bear with me please :)

Right so I am working with google analytics and I am using one of their scripts to display one query. It looks all good but I'm not really sure how to display more?

I know to get a query, but I have trouble displaying it. I can only display 1. I only have sessions but I want to add more for example , bounce rate etc.

Here is the code I am using:

  <?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>";
    }
  }
  ?>

If someone can give me any tips on how to improve it, or what to do please help me:) Please bear in mind I have very limited knowledge on PHP as I'm learning it while doing projects.

thank you anyway

解决方案

It is not clear if you need to make multiple queries or if you simply need to add more metrics to your existing query. For example you can query for ga:sessions and ga:percentNewSessions in the same query.

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

Then you would need to extract the second metric from the results:

  $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>";

Play around with the results object until you get a sense of its structure. Always use the Reference docs to understand what fields are available.

这篇关于谷歌分析,显示多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆