その他サービス/GoogleAnalytics/php
提供: 初心者エンジニアの簡易メモ
analyticsをphpで操作
目次
プロジェクト作成
- https://console.developers.google.com/project
- "sample-analytics"などと入れる
認証作成
- 作成したプロジェクトを選択
- APIと認証/認証情報リンクを選択
- 新しいクライアントIDを作成ボタンを押す
- サービスアカウントを選択
- 秘密鍵のjsonファイルを保存
- p12ファイルも保存
analyticsAPIを有効に
- 左メニューの"API"リンクを選択
- "Analytics API"を選択
- 有効にするボタンを押す
analyticsサイト側設定
- 上部の"アナリティクス設定"リンクを押す
- プロパティのユーザ管理を押す
- メールアドレス欄にjsonファイル内のclient_email(1405xxxxxxxx-46f988f8xxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com)文字列を追加
- ビュー設定のビューIDをメモしておく(phpからの呼び出しに使う)
phpライブラリDL
git clone https://github.com/google/google-api-php-client.git
Top10表示
<?php
class AnalyticsApiModel
{
private $_client;
private $_client_id = 'xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
private $_service_account_name = 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
private $_key_file = 'sample-analytics-xxxxxxxxxxx.p12';
private $_view_id = 'ga:xxxxxxxxxx'; //ビューIDを入力
public function __construct()
{
require_once __DIR__ . "/google-api-php-client/src/Google/autoload.php";
$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");
$client->setClientId($this->_client_id);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
$this->_service_account_name,
array('https://www.googleapis.com/auth/analytics'),
@file_get_contents(__DIR__ . '/' . $this->_key_file)
));
$this->_client = $client;
}
public function findAllTop10($time)
{
$analytics = new Google_Service_Analytics($this->_client);
$end_date = date("Y-m-d", $time);
$start_date = date("Y-m-d", $time - 86400);
$result = $analytics->data_ga->get(
$this->_view_id,
$start_date,
$end_date,
'ga:pageviews',
array(
'dimensions' => 'ga:pageTitle,ga:pagePath',
'sort' => '-ga:pageviews',
// 'filters' => 'ga:pagePath=~/tag/',
'max-results' => '10' //件数
)
);
return $result['rows'];
}
}
$api = new AnalyticsApiModel();
$all = $api->findAllTop10(time());
print nl2br(print_r($all,1));
//Array
//(
// [0] => Array
// (
// [0] => テストページ1
// [1] => /test1
// [2] => 1772
// )
// [1] => Array
// (
// [0] => テストページ2
// [1] => /test2
// [2] => 925
// )
リアルタイム数を取得
リアルタイムなコンテンツURLを取得することはできないみたい(2015/6時点)
try {
$optParams = array(
'dimensions' => 'rt:medium');
$results = $analytics->data_realtime->get(
$this->_view_id,
'rt:activeUsers',
$optParams
);
$totals = $results->getTotalsForAllResults();
print nl2br(print_r($totals,1)); // 下記*1に示す
if (count($results->getRows()) > 0) {
foreach ($results->getRows() as $row) {
print nl2br(print_r($row,1)); // 下記*2に示す
}
}
} catch (apiServiceException $e) {
echo $e->getMessage();
}
*1
Array
(
[rt:activeUsers] => 721
)
*2
Array
(
[0] => DIRECT
[1] => 383
)
Array
(
[0] => ORGANIC
[1] => 327
)
Array
(
[0] => REFERRAL
[1] => 1
)
Array
(
[0] => SOCIAL
[1] => 10
)
User does not have sufficient permissions for this profileエラー
view id が間違えてないか確認
参考
http://www.karakaram.com/google-analytics-api-batch
http://syncer.jp/google-analytics-api-tutorial
公式リアルタイムページ https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get?hl=ja
