PHP阵列和Google Analytics(分析)V3 API [英] PHP Array and Google Analytics V3 API

查看:66
本文介绍了PHP阵列和Google Analytics(分析)V3 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Analytics(分析)V3 PHP OAuht API.在Google Analytics(分析)API示例中使用Simple.php时,数据以PHP数组的形式返回.我正在使用以下电话获得对某些特定数据的更详细的答案.效果很好.

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

数组的输出是

       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.

现在是问题所在.我花了最后一周的时间试图将其解析为HTML表或任何看起来很漂亮的东西.我已经接近了,但是这个多维数组似乎超出了我的能力.我还尝试使解决方案足够灵活,以便在添加度量或维度的情况下也能处理更多度量或维度.我是自学成才的PHP,所以也许我缺少一个或两个函数可以简化此过程.再次感谢您提供任何提示和提示,以使这项工作有效.

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.

推荐答案

问题出在表主体行的foreach中.您似乎错过了所有行.将其包装在另一个循环中,以在tds集周围打印出tr.

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> 

这篇关于PHP阵列和Google Analytics(分析)V3 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆