2017-04-06 11 views
1

を表示私はPHPにかなり新しいですので、私と一緒にクマ右ので、私はGoogleの分析で働いていると私は1つのクエリを表示するには、そのスクリプトのいずれかを使用していますのGoogle Analyticsは、1つの以上のクエリ

:)をしてください。それはすべての良い見えますが、私は実際にもっと表示する方法がわからないのですか?

私はクエリを取得することを知っていますが、表示に問題があります。私は表示のみ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 = 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に関する非常に限られた知識を持っていることに留意してください。

では、複数のクエリを作成する必要がある場合や、単に既存のクエリに複数のメトリックを追加する必要がある場合、それは明らかではないが、とにかく

答えて

0

をお願いします。たとえば、同じクエリで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を使用してください。

+0

ありがとうございました!それはうまくいった、私はそれが簡単なngl、ちょっと単純な修正であるちょっと頭脳のようなtbh cozだと信じていない。しかし、ありがとう! –

+0

@MateuszKolackiは答えを受け入れ、問題を解決済みとマークします。ここではhttps://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workの方法があります。そうでない場合は、問題が解決されなかったと考えて他の人が回答を提出することがあります。 –

関連する問題