PHP Array and Google Analytics V3 API

2019-04-13 23:40发布

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.

$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.


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条回答
萌系小妹纸
2楼-- · 2019-04-14 00:03

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!

查看更多
再贱就再见
3楼-- · 2019-04-14 00:26

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.

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> 
查看更多
登录 后发表回答