php - Google analytics, display more than 1 query -


i'm pretty new php bear me please :)

right working google analytics , using 1 of scripts display 1 query. looks i'm not sure how display more?

i know query, have trouble displaying it. can display 1. have sessions want add more example , bounce rate etc.

here code 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 user.');         }       } else {         throw new exception('no properties found user.');       }     } else {       throw new exception('no accounts found 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 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 can give me tips on how improve it, or please me:) please bear in mind have limited knowledge on php i'm learning while doing projects.

thank anyway

it not clear if need make multiple queries or if need add more metrics existing query. example can query ga:sessions , ga:percentnewsessions in same query.

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

then need extract second metric results:

  $rows = $results->getrows();   $sessionstotal = $rows[0][0];   $percentnewsessions = $rows[0][1];     // print 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 results object until sense of structure. use reference docs understand fields available.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -