【Movable Type】Google アナリティクス APIを利用したランキング表示

Movable Type と Google アナリティクス API で記事の閲覧回数を取得したランキングを作成しました!

週間ランキング上位3件

1週間のアクセスを集計したランキング部分のキャプチャです。

<?php
$cgi = 'サイトのドメイン/mt/mt-data-api.cgi'; // mt-data-api.cgiのURL
$domain = 'サイトのドメイン'; // サイトのドメイン
$user = 'ユーザー名'; // MTのユーザー名
$pass = 'パス'; // MTのパスワード
$siteId = 2;

$startDate = date('Y-m-d', strtotime("- 6 days")); // 開始日
$endDate = date('Y-m-d'); // 終了日
$path = '/blog/'; // パスを設定しない場合は'/'を設定
$limit = 3; // 最大取得ページ数

// authentication
$url = $cgi . '/v2/authentication';
$data = array(
'username' => $user,
'password' => $pass,
'clientId' => 'foo',
'remember' => 1,
);
$options = array('http' =>
array(
'method' => 'POST',
'content' => http_build_query($data),
),
);
@$response = file_get_contents($url, false, stream_context_create($options));
if (!$response) {
echo "authentication failed.";
exit();
}
$json = json_decode($response);
$accessToken = $json->accessToken;

// pageviews
$url = $cgi . '/v2/sites/' . $siteId . '/stats/path/pageviews?path=' . $path . '&startDate=' . $startDate . '&endDate=' . $endDate . '&limit=' . $limit;
$options = array('http' =>
array(
'method' => 'GET',
'header' => 'X-MT-Authorization: MTAuth accessToken=' . $accessToken,
),
);
$response = file_get_contents($url, false, stream_context_create($options));

if (!$response) {
echo "pageviews failed.";
exit();
}
$json = json_decode($response);
$size = count($json->items);

// output
echo '<ol class="Ranking">';
for ($i = 0; $i < $size; $i++) {
$item = $json->items[$i];
echo '<li><a href="' . $domain . $item->path . '">' . ($i+1) . "位:" . $item->title . '(' . $item->pageviews . '回の閲覧)</a></li>';
}
echo '</ol>';
?>

最初、「Google アナリティクス APIを利用するのに必要なPerlモジュールのうちいくつかがありません: Crypt::SSLeay 」
とエラーが出ましたが、
http://www.movabletype.jp/documentation/mt6/installation/system-option.html

Crypt::SSLeay
AOL や Yahoo! などの SSL を利用する OpenID のコメント投稿者を認証する場合に必要です。または、Google Analytics を利用する場合に必要となります

これらの Perl モジュールは、Movable Type チェック用 CGI の mt-check.cgi を実行することで、インストールされているかどうか確認できます。mt-check.cgi については『サーバーのシステム情報の確認』の項を参照ください。

Crypt::SSLeay をサーバにインストールすることで解決しました。

最初に参考にしたのは↓こちらのページでしたが、
【参考】Google Analytics API v3をPHPから利用する方法 「PHP から API を叩く」

途中、より自由度のありそうな↓こちらの方法に変更しました。
【参考】Movable TypeのDataAPIでアクセスランキングを表示する方法

ここまでで、記事のランキング表示は出来たのですが、
「カスタムフィールド要素も出せるようにしたい!」と↓こちらの方法にも挑戦しましたが…
【参考】Movable TypeのDataAPIで特定のカスタムフィールドを取得する方法

カスタムフィールド部分がうまく出せないままです。
カスタムフィールドの画像とか出したいなぁ。

5 thoughts on “【Movable Type】Google アナリティクス APIを利用したランキング表示

  1. Hello! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to prevent hackers?

    • ofuta says:

      Thank you for your comment!

      There are a lot of excellent plug-in to WordPress.
      Add to !

      Or …
      Migration to Movable Type!
      Movable Type is past log remain in static HTML.
      So ,
      And survive when there is trouble .

  2. I just want to mention I am just very new to weblog and definitely loved your web blog. Almost certainly I’m likely to bookmark your blog . You amazingly come with awesome posts. Many thanks for sharing with us your web site.

  3. manicure says:

    Howdy great website! Does running a blog such as this take a
    great deal of work? I’ve very little understanding
    of coding however I was hoping to start my own blog
    in the near future. Anyways, should you have any suggestions or tips for
    new blog owners please share. I understand this
    is off topic however I simply had to ask. Cheers!

explanation へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください