PHP阵列和Google Analytics V3 API

时间:2021-06-19 15:16:00

I am using the Google Analytics V3 PHP OAuht API. When using Simple.php in Google Analytics API Example the data are returned as PHP arrays. I am using the following call to get a more detailed answer to some specific data. It works fine.

我正在使用Google Analytics V3 PHP OAuht API。在Google AnalyticsAPI中使用Simple.php示例时,数据将作为PHP数组返回。我正在使用以下调用来获取某些特定数据的更详细答案。它工作正常。

$ids = "ga:xxxxxx";
$start_date = "2011-01-01";
$end_date = "2011-11-30";
$metrics = "ga:visits,ga:pageviews";
$dimensions = "ga:browser";
$optParams = array('dimensions' => $dimensions);
$data = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams); 

Output of the Array is

数组的输出是

       Data

Array
(
    [kind] => analytics#gaData
    [id] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxxxx&dimensions=ga:browser&metrics=ga:visits,ga:pageviews&start-date=2011-01-01&end-date=2011-11-30&start-index=1&max-results=1000
    [query] => Array
        (
            [start-date] => 2011-01-01
            [end-date] => 2011-11-30
            [ids] => ga:xxxxxxxx
            [dimensions] => ga:browser
            [metrics] => Array
                (
                    [0] => ga:visits
                    [1] => ga:pageviews
                )

            [start-index] => 1
            [max-results] => 1000
        )

    [itemsPerPage] => 1000
    [totalResults] => 220
    [selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxx&dimensions=ga:browser&metrics=ga:visits,ga:pageviews&start-date=2011-01-01&end-date=2011-11-30&start-index=1&max-results=1000
    [profileInfo] => Array
        (
            [profileId] => xxxxx
            [accountId] => xxxxx
            [webPropertyId] => UA-xxxxxx-x
            [internalWebPropertyId] => xxxxxxxxxx
            [profileName] => xxxxx.com
            [tableId] => ga:xxxxxxxx
        )

    [containsSampledData] => 
    [columnHeaders] => Array
        (
            [0] => Array
                (
                    [name] => ga:browser
                    [columnType] => DIMENSION
                    [dataType] => STRING
                )

            [1] => Array
                (
                    [name] => ga:visits
                    [columnType] => METRIC
                    [dataType] => INTEGER
                )

            [2] => Array
                (
                    [name] => ga:pageviews
                    [columnType] => METRIC
                    [dataType] => INTEGER
                )

        )

    [totalsForAllResults] => Array
        (
            [ga:visits] => 36197
            [ga:pageviews] => 123000
        )

    [rows] => Array
        (
            [0] => Array
                (
                    [0] => (not set)
                    [1] => 459
                    [2] => 1237
                )

            [1] => Array
                (
                    [0] => 12345
                    [1] => 3
                    [2] => 3
                )

            [2] => Array
                (
                    [0] => 440955
                    [1] => 1
                    [2] => 1
                )

            [3] => Array
                (
                    [0] => Alexa Toolbar
                    [1] => 1
                    [2] => 1
                )

            [4] => Array
                (
                    [0] => Android Browser
                    [1] => 4177
                    [2] => 9896
                )

    ....

    The [Rows] Array has 219 entries.

Now the problem. I have spent the last week trying to parse this into an HTML table or anything that looks presentable. I have come close, but it seems this multi-dimensional array is beyond what I am able to handle. I am also trying to keep the solution flexible enough to handle more metrics or dimensions if they are added as well. I am self-taught PHP, so maybe I am missing a function or two that could make this easier. Thanks again for any hints, tips of ideas to make this work.

现在的问题。我花了最后一周试图将其解析为HTML表格或任何看起来很漂亮的东西。我已经接近了,但似乎这个多维数组超出了我能够处理的范围。我还试图使解决方案保持足够的灵活性,以便在添加它们时处理更多指标或维度。我是自学成才的PHP,所以也许我错过了一两个可以使这更容易的功能。再次感谢任何提示,使这项工作的想法提示。


I got a bit further, but I does not fully format the way I want...maybe someone can see where I went wrong

我得到了更多,但我并没有完全按照我想要的方式格式化......也许有人可以看到我哪里出错了

$output = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams);
echo'<table style="text-align: left; width: 100%;" border="1" cellpadding="2"
cellspacing="2">
<tbody><tr>';
foreach ($output['columnHeaders'] as $header) {
print "<td>";
printf('%25s', $header['name']);
print "</td>";
}
print "</tr>";
foreach ($output['rows'] as $row) {
print "<td>";
foreach ($row as $column)
printf('%25s', $column);
print "</td>";
}
print "\n";
echo'
</tbody>
</table>';

I still can't seem to get the rows to display right.

我似乎仍然无法显示正确的行。

2 个解决方案

#1


2  

To get you on your way use

为了让你继续使用

$data [rows][$i][$j][columnHeaders] [0][name]
$data [rows][$i][$j][columnHeaders] [1][name]

and for rows use something like

对于行使用类似的东西

$data [rows][0][1]

You will have to get the variable via increment with something like:

您将不得不通过增量获取变量,例如:

var =(count($data [columnHeaders]));

   for ($i='0'; $i<=$var; $i++) {
    echo '.$data [columnHeaders] [$i][name].';}

This should get you on your way towards building your table. Good Luck!

这应该可以帮助您建立自己的桌子。祝你好运!

#2


2  

The issue is in your foreach for the table body rows. You appear to have missed the rows out. Wrap it in another loop to print out tr around the set of tds.

这个问题在你的foreach表格行中。你好像错过了这些行。将它包装在另一个循环中以打印出tds周围的tr。

This is what I used for printing out a table of analytics data:

这是我用于打印分析数据表的内容:

$data = $analytics->data_ga->get('ga:' . $profileId, $dateFrom, $dateTo, $metrics, array('dimensions' => $dimensions));

<table>
        <thead>
            <tr>
                <?php 
                    foreach ($data->columnHeaders as $header) {
                        $headerName = ucwords(preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', str_replace('ga:', '', $header->name)));
                        print '<th>';
                        printf('%s', $headerName);
                        print '</th>';
                    }
                ?>
            </tr>
        </thead>
        <tbody>
        <?php 
            foreach ($data->rows as $row) {
                print '<tr>';
                foreach ($row as $cell) {
                    print '<td>';
                    printf('%s', $cell);
                    print '</td>';
                }
                print '</tr>';
            }
        ?>
    </tbody>
 </table> 

#1


2  

To get you on your way use

为了让你继续使用

$data [rows][$i][$j][columnHeaders] [0][name]
$data [rows][$i][$j][columnHeaders] [1][name]

and for rows use something like

对于行使用类似的东西

$data [rows][0][1]

You will have to get the variable via increment with something like:

您将不得不通过增量获取变量,例如:

var =(count($data [columnHeaders]));

   for ($i='0'; $i<=$var; $i++) {
    echo '.$data [columnHeaders] [$i][name].';}

This should get you on your way towards building your table. Good Luck!

这应该可以帮助您建立自己的桌子。祝你好运!

#2


2  

The issue is in your foreach for the table body rows. You appear to have missed the rows out. Wrap it in another loop to print out tr around the set of tds.

这个问题在你的foreach表格行中。你好像错过了这些行。将它包装在另一个循环中以打印出tds周围的tr。

This is what I used for printing out a table of analytics data:

这是我用于打印分析数据表的内容:

$data = $analytics->data_ga->get('ga:' . $profileId, $dateFrom, $dateTo, $metrics, array('dimensions' => $dimensions));

<table>
        <thead>
            <tr>
                <?php 
                    foreach ($data->columnHeaders as $header) {
                        $headerName = ucwords(preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', str_replace('ga:', '', $header->name)));
                        print '<th>';
                        printf('%s', $headerName);
                        print '</th>';
                    }
                ?>
            </tr>
        </thead>
        <tbody>
        <?php 
            foreach ($data->rows as $row) {
                print '<tr>';
                foreach ($row as $cell) {
                    print '<td>';
                    printf('%s', $cell);
                    print '</td>';
                }
                print '</tr>';
            }
        ?>
    </tbody>
 </table>