Analytics API和PHP - 以不同格式获取报告

时间:2022-09-07 15:14:08

The following code returns an object of Google_Service_AnalyticsReporting_GetReportsResponse

以下代码返回Google_Service_AnalyticsReporting_GetReportsResponse的对象

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

I'm wondering if I can get Reports in a different format, example: Array(Dimension,value)

我想知道我是否可以以不同的格式获取报告,例如:Array(Dimension,value)

2 个解决方案

#1


6  

Yes.

The Google Reporting v4 SDK for PHP looks like it has been automatically converted from java code, right along with all the usual object overkill.

适用于PHP的Google Reporting v4 SDK看起来已经从java代码自动转换,以及所有常见的对象矫枉过正。

To convert to an array, assuming you asked for one dimension and some metrics:

要转换为数组,假设您要求一个维度和一些指标:

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

#2


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,
                    )
                );

This done the job for me.

这完成了我的工作。

$row gives something like follows

$ row给出了类似下面的内容

array(5 items)
   0 => array(2 items)
      0 => 'India' (5 chars)
      1 => '1' (1 chars)
   1 => array(2 items)
      0 => '*' (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)

#1


6  

Yes.

The Google Reporting v4 SDK for PHP looks like it has been automatically converted from java code, right along with all the usual object overkill.

适用于PHP的Google Reporting v4 SDK看起来已经从java代码自动转换,以及所有常见的对象矫枉过正。

To convert to an array, assuming you asked for one dimension and some metrics:

要转换为数组,假设您要求一个维度和一些指标:

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

#2


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,
                    )
                );

This done the job for me.

这完成了我的工作。

$row gives something like follows

$ row给出了类似下面的内容

array(5 items)
   0 => array(2 items)
      0 => 'India' (5 chars)
      1 => '1' (1 chars)
   1 => array(2 items)
      0 => '*' (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)