PHP 数组和 Google Analytics V3 API

发布于 2025-01-06 01:16:57 字数 4368 浏览 0 评论 0原文

我正在使用 Google Analytics V3 PHP Oauht API。在 Google Analytics API 示例中使用 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); 

数组的输出

       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,所以也许我缺少一两个可以使这变得更容易的函数。再次感谢您提供的任何提示和想法技巧,以使这项工作顺利进行。


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2025-01-13 01:16:57

为了让您按自己的方式使用

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

,对于行使用类似的内容

$data [rows][0][1]

您将必须通过增量获取变量,例如:

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

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

这应该让您开始构建表格。祝你好运!

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!

你的笑 2025-01-13 01:16:57

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

这是我用来打印分析数据表的:

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

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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文