GAPI - Google Analytics API PHP Interface とは
以前、無料のアクセス解析ツールで有名な「Google Analytics」が、その解析データを自由に取得し利用できるAPI「Google Analytics Data Export API」をリリースしたと記事にしました。
このAPIをphpで簡単に利用できないか調べてたら、googleが「GAPI」というPHPライブラリを提供することがわかりました。
url: http://code.google.com/p/gapi-google-analytics-php-interface/
からダウンロードできます。

GAPI now has Google Analytics filter support
GAPI is now at version 1.3 - This version contains fixes for the handling of very large metric values represented in scientific notation. Thanks to austinrehab for raising this issue.
Development is complete on the Google Analytics filter control. You can now filter your results using a simple GAPI filter string, for example:
$filter = 'country == United States && browser == Firefox || browser == Chrome';
You can create simple query strings that represent the logic Google Analytics requires, but it is abstracted enough to be more readable and easier to work with.
Download the latest gapi.class.php and try out the filter control with the example.filter.php. Read more about the GAPI Filter Control.
訳:
今のバージョンは1.3です。このバージョンは、「決められた表記により、とても大きな統計値を扱うえるようにする」修正を含んでいます。この問題をあげてくれた「Austin Rehab」さんに感謝します。開発は「Google Analytics フィルターコントロール」に関しては完了しています。あなたは今、単に「GAPIフィルター文字列」を使うことで、結果をフィルターすることができます。例えば、
「$filter = 'country == United States && browser == Firefox || browser == Chrome'」
のように。あなたは、単に「Google Analytics」が解読できる決まった形式のクエリ文字列を作成すればよい。そのクエリ文字列は、判読しやすく簡単に動作するさせるれるよう抽象化されています。最新の「gapi.class.php」をダウンロードしてください。そしてサンプルを使って「フィルターコントロール」を試してください。また、「GAPI Filter Control」についてもっと読んで知ってください。
Features:
* Supports CURL and fopen HTTP access methods, with autodetection
* PHP arrays for Google Analytics metrics and dimensions
* Account data object mapping - get methods for parameters
* Report data object mapping - get methods for metrics and parameters
* Easy filtering, use a GAPI query language for Google Analytics filters
* Full PHP5 Object Oriented code, ready for use in your PHP application
訳:
要点:
・自動認証で、cURLおよびHTTPアクセス方法を利用しています。
・Google Analyticsの統計量や範囲はPHP配列に格納されます。
・Account data オブジェクト関数 - パラメータを取得する方法
・Report data オブジェクト関数 - 統計量や範囲を取得する方法
・Google AnalyticsのフィルターさせるGAPI query言語は簡単に使用できます。
・完全なPHP5オブジェクト指向コードです。PHPアプリケーションで使用するには準備してください。
$ga = new gapi('email@yourdomain.com','password');
$ga->requestReportData(145141242,array('browser','browserVersion'),
array('pageviews','visits'));
foreach($ga->getResults() as $result)
{
echo '<strong>'.$result.'</strong><br />';
echo 'Pageviews: ' . $result->getPageviews() . ' ';
echo 'Visits: ' . $result->getVisits() . '<br />';
}
echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';This project was inspired by the use of Doctrine and Propel ORM interfaces for PHP. Dealing with complex data should be easy!
訳:
このプロジェクトは、PHPの「Doctrine」や「Propel」のORMインターフェースを使用目的で起こりました。複雑なデータに対する扱いでも簡単であるべきです!
Access metrics and dimensions using magic get methods
With GAPI, when data is returned from Google it is automatically converted into a native PHP object, with an interface to allow the 'get' the value of any dimesion or metric.
For example, if you request the metric 'uniquePageviews' and the dimesion 'pagePath' you can do the following:
foreach($ga->getResults() as $result)
{
echo $result->getUniquePageviews();
echo $result->getPagePath();
}
訳:
簡単に統計値や範囲を取得できるメソッド群(関数群)
GAPIでは、GOOGLEから戻ってきたデータは、元のPHPオブジェクトに自動的に変換されます。GAPIは、どんな統計値や範囲も取得できるインターフェースです。例えば、「唯一のページビュー数」と「ページURL情報」を要求したいならば、次のようにすれば可能です。
foreach($ga->getResults() as $result)
{
echo $result->getUniquePageviews();
echo $result->getPagePath();
}

