アクセス解析Piwikのプラグインを作ってみる
説明が英語なので骨が折れるが、まずは以下のページのとおりに開発環境を構築する。
で、上記URLのかなり下の方にあるとおり、consoleコマンドでプラグインのひな形を作成する。
./console generate:plugin --name="MyPlugin"
プラグインの説明を入力しろと言われるので適当に入れて進む。
Enter a plugin description: My Awesome Plugin Enter a plugin version number (default to 0.1.0): Plugin MyPlugin 0.1.0 generated. Our developer guides will help you developing this plugin, check out http://developer.piwik.org/guides To see a list of available generators execute ./console list generate Enjoy!
続いて以下のコマンドでプラグインを有効状態にする。
./console plugin:activate MyPlugin
さらにレポートを生成するコマンド実行。
./console generate:report
プラグインの名前を入れて、と出るので、先に自分が作った名前を入力する。
Enter the name of your plugin: MyPlugin Enter the name of your report, for instance "Browser Families": My first plugin Enter a documentation that describes the data of your report (you can leave it empty and define it later): Enter the report dimension, for instance "Browser" (you can leave it either empty or use an existing one): You should now implement the method called "getMyfirstplugin()" in API.php Enjoy!
で、pluginsディレクトリにMyPluginディレクトリが出来ていて、その中にAPI.phpができている。
なんと!その中に今コマンドで指定したgetMyfirstpluginメソッドが出来ているではあーりませんか。
/** * Another example method that returns a data table. * @param int $idSite * @param string $period * @param string $date * @param bool|string $segment * @return DataTable */ public function getMyfirstplugin($idSite, $period, $date, $segment = false) { $table = new DataTable(); $table->addRowFromArray(array(Row::COLUMNS => array('nb_visits' => 5))); return $table; }
この中の $table->addRowFromArray(...
部分はダミーなので、ここに自分でAPI読み込みを書けばOK。
$table = \Piwik\API\Request::processRequest('VisitorInterest.getNumberOfVisitsPerVisitDuration', array( 'idSite' => $idSite, 'period' => $period, 'date' => $date, ));
最後に、自動で生成された Reports/GetMyfirstplugin.php を編集する。
protected function init()
の中の以下のコメントアウトを外す。
// $this->metrics = array('nb_visits', 'nb_hits'); // $this->subcategoryId = 'PomPlugin_Myfirstplugin';
ブラウザから開いて動作確認する。
- 作者: 谷藤賢一,徳丸浩(協力)
- 出版社/メーカー: リックテレコム
- 発売日: 2017/02/18
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る