Tag Archives: Ga

GoogleAnalyticsのAPIをPHPで使ってみる [トラッキングデータ取得編]

前回に引き続き、今回は実際のトラッキングデータを取得してみる。

⇒ライブラリはココ

サンプルのコードを元に書いてみました。

とりあえず、日別の訪問者数、PV数を取得してみる。

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
define('ga_email','***********');
define('ga_password','********');
define('ga_profile_id','********');

require 'gapi.class.php';

$ga = new gapi(ga_email,ga_password);

$ga->requestReportData(ga_profile_id,array('date'),array('pageviews','visits'), 'date');
?>
<table>
<tr>
  <th>DATE</th>
  <th>Pageviews</th>
  <th>Visits</th>
</tr>
<?php
foreach($ga->getResults() as $result):
?>
<tr>
  <td><?php echo $result ?></td>
  <td><?php echo $result->getPageviews() ?></td>
  <td><?php echo $result->getVisits() ?></td>
</tr>
<?php
endforeach
?>
</table>

<table>
<tr>
  <th>Total Results</th>
  <td><?php echo $ga->getTotalResults() ?></td>
</tr>
<tr>
  <th>Total Pageviews</th>
  <td><?php echo $ga->getPageviews() ?>
</tr>
<tr>
  <th>Total Visits</th>
  <td><?php echo $ga->getVisits() ?></td>
</tr>
<tr>
  <th>Results Updated</th>
  <td><?php echo $ga->getUpdated() ?></td>
</tr>
</table>
</body>
</html>

とりあえず、resultをprint_rしてみると、こんな感じ。

gapi Object
(
    [auth_token:private] => XXXXXXXXXXXXX
    [account_entries:private] => Array
        (
        )

    [account_root_parameters:private] => Array
        (
        )

    [report_aggregate_metrics:private] => Array
        (
            [pageviews] => 56230
            [visits] => 14619
        )

    [report_root_parameters:private] => Array
        (
            [updated] => 2010-08-18T01:48:03.913-07:00
            [generator] => Google Analytics
            [generatorVersion] => 1.0
            [totalResults] => 32
            [startIndex] => 1
            [itemsPerPage] => 30
            [profileId] => 17170169
            [webPropertyId] => XXXXXXXX
            [accountName] => XXXXXXXX
            [startDate] => 2010-07-18
            [endDate] => 2010-08-18
        )

    [results:private] => Array
        (
            [0] => gapiReportEntry Object
                (
                    [metrics:private] => Array
                        (
                            [pageviews] => 433
                            [visits] => 106
                        )

                    [dimensions:private] => Array
                        (
                            [date] => 20100814
                        )

                )

            [1] => gapiReportEntry Object
                (
                    [metrics:private] => Array
                        (
                            [pageviews] => 445
                            [visits] => 148
                        )

                    [dimensions:private] => Array
                        (
                            [date] => 20100808
                        )

                )

            [2] => gapiReportEntry Object
                (
                    [metrics:private] => Array
                        (
                            [pageviews] => 470
                            [visits] => 133
                        )

                    [dimensions:private] => Array
                        (
                            [date] => 20100718
                        )

                )
            (以下省略....)
        )
)

表示したい軸・指標はここにあるやつかな。
Dimensions & Metrics Reference

意外と簡単。