2016-06-13 33 views
15

Il codice seguente restituisce un oggetto di Google_Service_AnalyticsReporting_GetReportsResponseAnalytics API e PHP - ottenere report in diversi formati

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

mi chiedo se posso ottenere report in un formato diverso, ad esempio: Array(Dimension,value)

risposta

6

Sì.

L'SDK di Google Reporting v4 per PHP sembra essere stato convertito automaticamente dal codice java, insieme a tutto il solito overkill dell'oggetto.

Per convertire a un array, assumendo che avete chiesto una dimensione e alcune metriche:

$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

Ammettendo per un po 'di necrofilia, questo post è stato l'unico risultato di ricerca di Google per i nomi delle classi folli Google API PHP di 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, 
        ) 
       ); 

questo fatto il lavoro per me.

$ fila dà qualcosa come segue

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) 
Problemi correlati