Googleアナリティクスが提供しているAPIを使って、人気記事ランキングを設置するプログラムです。
このプログラムには、
Google Analytics API class for PHP
を使います。
基本的な使い方の解説は、
http://www.koikikukan.com/archives/2012/05/10-015555.php
で確認してください。
ただ、どうも、「Google Analytics API class for PHP」がバージョンアップしたのか、解説とは異なる部分が多いようです。
ですので、現時点で動くプログラムを作りましたので、参考にして下さい。
ステップ1
アクセスランキングを設置するフォルダー内に Google Analytics API class for PHP の中にある
googleanalytics.class.php
をアップロードして下さい。
ステップ2
下記のコードを記載したPHPファイルを、例えば、
get_data.php
などとして、googleanalytics.class.php のあるフォルダーへアップロードして下さい。
赤字は、自分の環境に合わせて変更して下さい。
<?php
// include the Google Analytics PHP class
include “googleanalytics.class.php”;
try {
// create an instance of the GoogleAnalytics class using your own Google {email} and {password}
$ga = new GoogleAnalytics(‘hoge@hoge.jp’,’hogehoge’);
// set the Google Analytics profile you want to access – format is ‘ga:123456’;
$ga->setProfile(‘ga:1234’);
//あなたのサイトのドメイン。最後に/は不要。
$domain = “http://www.hoge.jp”;
//一週間
$from_day = date(‘Y-m-d’,strtotime(“0 day”));
$to_day = date(‘Y-m-d’,strtotime(“-6 day”));
get_data($from_day,$to_day);
make_file(“w”,$log_line);
//1ヶ月
$from_day = date(‘Y-m-d’,strtotime(“0 day”));
$to_day = date(‘Y-m-d’,strtotime(“-29 day”));
get_data($from_day,$to_day);
make_file(“m”,$log_line);
print(“ok”);exit;
} catch (Exception $e) {
print ‘Error: ‘ . $e->getMessage();
}
function make_file($f_name,$write_data){
umask(0);
$file= $f_name . “.cgi”;
$file=trim($file);
$file_dat=fopen($file,”w+”);
flock($file_dat, LOCK_EX);
fputs($file_dat, $write_data);
flock($file_dat, LOCK_UN);
chmod($file,0666);
}
function get_data($from_d,$to_d){
global $log_line,$ga,$domain;
// set the date range we want for the report – format is YYYY-MM-DD
$ga->setDateRange($to_d,$from_d);
// get the report for date and country filtered by Australia, showing pageviews and visits
$report = $ga->getReport(
array(
‘dimensions’ => urlencode(‘ga:pagePath,ga:pageTitle’),
‘metrics’=>urlencode(‘ga:uniquePageviews’),
‘filters’=>urlencode(‘ga:pagePath=~^/modules/’),
‘sort’ => urlencode(‘-ga:uniquePageviews’),
‘max-results’ => urlencode(20),
)
);
$log_line = null;
foreach ($report as $key => $value) {
list($url,$title) = explode(“~~”, $key);
$url = $domain . $url;
$log_line .= “$url<>$title<>” . $value[‘ga:uniquePageviews’] . “\n”;
}
}
?>
ステップ3
get_data.php を実行すると、
w.cgi : 7日間のユニークユーザーアクセス数のランキング
m.cgi : 30日間のユニークユーザーアクセス数のランキング
が作られます。
それを読み込んで画面上へ上位10件を表示させるコード(7日間累計の場合)は、下記の通りです。w.php などとしてサイトへアップして下さい。
<?php
//京都
$file_name = “w.cgi”;
$get_contents = @file_get_contents($file_name);
$logs = array();
$logs = explode(“\n”,$get_contents);
$num = 0;
foreach($logs as $value){
$this_data = array();
$this_data = explode(“<>”,$value);
$title = mb_substr($this_data[1],0,30);
if($title !== $this_data[1]){
$title = $title . “…”;
}
$p_data .=<<<EOD
<li>
<a href=”$this_data[0]” title=”$this_data[1]”>$title</a>
</li>
EOD;
$num++;
if($num>=10){break;}
}
print<<<EOD
<div>
<ul>
$p_data
</ul>
</div>
EOD;
?>
ステップ4
get_data.php は、定期的に実行しなければ、w.cgiやm.cgiは更新されません。
cron に登録して、定期的に自動実行するようにして下さい。
以上です。