‘PHP’ カテゴリーのアーカイブ

EC-CUBEをインストールしたサーバのURLを変更した場合

2010年9月7日 火曜日

インストールディレクトリ下のdata/install.phpの下記の行を変更する。

    define (‘SITE_URL’, ‘http://www.test-site.com/’);
    define (‘SSL_URL’, ‘http://www.test-site.com/’);

Google Analytics APIをPHPから使ってページランキングを表示する

2010年7月10日 土曜日

code.google.comからGAPIというライブラリをダウンロードする。

curlのモジュールが有効になっていることが必要。下記はGAPIに付属のサンプルコード。実行するとブラウザごとのページビュー、訪問数をページビューの少ない順に出力する。実行するにはdefine文に定義されているemailアドレス、パスワード、プロファイルIDを実際に使用できる者に変更するに必要がある。プロファイルIDはGoogle Analytics管理画面のトップページでプロファイルの編集を実行して表示した画面の左上に表示されている数字。

<?php
define(‘ga_email’,'youremail@email.com’);
define(‘ga_password’,'your password’);
define(‘ga_profile_id’,'your profile id’);

require ‘gapi.class.php’;

$ga = new gapi(ga_email,ga_password);

$ga->requestReportData(ga_profile_id,array(‘browser’,'browserVersion’),array(‘pageviews’,'visits’));
?>
<table>
<tr>
  <th>Browser &amp; Browser Version</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>

requestReportDataメソッドのパラメータは

requestReportData($report_id, $dimensions, $metrics, $sort_metric=null, $filter=null, $start_date=null, $end_date=null, $start_index=1, $max_results=30)

となっていて、Google Analytics APIのFeedリクエストで指定できるパラメータが渡せるようになっている。複数指定できるパラメータは配列として渡す。たとえば上位アクセスランキングのリストがほしい場合は上記メソッドを変更して、

$ga->requestReportData(ga_profile_id,array(‘pageTitle’,'pagePath’),array(‘pageviews’,'visits’),
 ’-pageviews’, null, null, null, 1, 10);

とすることで取得できる。

表示部分は

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

<?php
foreach($ga->getResults() as $result):
?>
<tr>
  <td><a href=’<?php echo $result->getPagepath() ?>’><?php echo $result->getPagetitle() ?></a></td>
  <td><?php echo $result->getPageviews() ?></td>
  <td><?php echo $result->getVisits() ?></td>
</tr>
<?php
endforeach
?>

のように変更する。