2016-06-13 79 views
15

下面的代碼返回的Google_Service_AnalyticsReporting_GetReportsResponseAnalytics(分析)API和PHP - 獲取報告不同格式

$body = new Google_Service_AnalyticsReporting_GetReportsRequest(); 
$body->setReportRequests($aRequests); 
return $this->oAnalytics->reports->batchGet($body); 

對象我不知道如果我能得到不同格式的報告,例如: Array(Dimension,value)

回答

6

是。

的谷歌報告V4 SDK的PHP看起來像它已經自動從Java代碼轉換,權連同所有常見的對象矯枉過正。

要轉換到一個數組,假設你問一個維度,有些指標:

$data = [] 
foreach ($this->oAnalytics->reports->batchGet($body)->getReports()[0]->getData()->getRows() as $row) { 
    $key = $row->dimensions[0]; // chose a unique value or dimension that works for your app, or remove and use $data[] = $value; below 
    $values = array_merge($row->metrics, $row->dimensions);  
    $data[$key] = $value; 
} 
+1

承認的一點姦屍的,這篇文章是爲谷歌API的PHP SDK的瘋狂類名的只有谷歌的搜索結果。 – cmc

1
$client = new \Google_Client(); 
$cred = new \Google_Auth_AssertionCredentials(
      $serviceAccountEmail, 
      array(\Google_Service_Analytics::ANALYTICS_READONLY), 
      $key 
     ); 
$client->setAssertionCredentials($cred); 

if ($client->getAuth()->isAccessTokenExpired()) { 
      $client->getAuth()->refreshTokenWithAssertion($cred); 
} 
$analytics = new \Google_Service_Analytics($client); 
$row = $analytics->data_ga->get(
        'ga:' . $profileId, 
        $startdate, 
        $enddate, 
        $metrics, 
        array(
         'dimensions' => $dimension, 
         'sort'  => $metrics, 
         'max-results' => 20, 
        ) 
       ); 

這樣做的工作對我來說。

$行給出類似如下

array(5 items) 
    0 => array(2 items) 
     0 => 'India' (5 chars) 
     1 => '1' (1 chars) 
    1 => array(2 items) 
     0 => 'Taiwan' (6 chars) 
     1 => '1' (1 chars) 
    2 => array(2 items) 
     0 => 'United States' (13 chars) 
     1 => '1' (1 chars) 
    3 => array(2 items) 
     0 => '(not set)' (9 chars) 
     1 => '4' (1 chars) 
    4 => array(2 items) 
     0 => 'United Kingdom' (14 chars) 
     1 => '82' (2 chars) 
相關問題