Search

GAPI 1.3について

2011/6/12

GAPI 1.3

License: GNU GPL v3

GAPIは、Google Analytics Data Export APIのアカウントフィードやデータフィードをPHPで簡単に扱うことができるクラスライブラリです。 ディメンション、指標は配列形式で指定します。 使用可能なディメンションと指標はディメンションと指標のリファレンスにあります。

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

GAPIはCURLを使用するため、php.iniの620行目あたりにあるCURLを有効にしておく必要があります(先頭のコメントアウトを外す)。 またローカル環境では動作しませんので、サーバーにアップして確認となります。

; Windows Extensions
extension=php_curl.dll

定数

定数説明
ga_email必須 Google Analyticsにログインする時のメールアドレス指定。
ga_password必須 Google Analyticsにログインする時のパスワードを指定。
ga_profile_id任意 プロファイルID(Analyticsアカウントのプロファイルに割り当てられた固有番号)を指定。 プロファイルIDは、Google Analyticsでマイレポート画面を表示した時のURLの末尾にあるIDになります。※UA-*****ではありません。
例)https://www.google.com/analytics/reporting/?reset=1&id=12345

パラメータ

属性説明
report_id必須 レポートの出力対象となるプロファイルID。
定数「ga_profile_id」を指定。
dimensions必須 Google Analyticsのディメンションを配列で指定。
metrics必須 Google Analyticsの指標を配列で指定。
sort_metric任意 ディメンションや指標のソート方法を指定。
デフォルトは昇順。先頭に「-」を付けると昇順になります。
filter任意 結果をフィルタリングする条件を指定。
参照:Using the GAPI filter control
start_date任意 レポート期間の開始日を指定。
YYYY-MM-DD形式。
end_date任意 レポート期間の終了日を指定。
YYYY-MM-DD形式。
start_index任意 結果フィードの開始インデックスを指定。
デフォルトは1。指定可能な値は1~。
※0など無効な値を指定するとエラーになります。
※max_resultsとセットで指定することで、任意の範囲の結果フィードを取得できます。
max_results任意 結果フィードの最大取得数を指定。
省略した場合は、1,000件。
指定可能な値は、1~10,000。

レポート出力

2011/6/12

Google Analytics Data Export API のデータ フィードを使用して、指定したプロファイルのデータを取得します。

過去1週間の人気エントリーを取得

過去1週間にアクセス数が多かったエントリーを10件取得して表示します。 開始日・終了日の日付設定には、strtotime()を使用しています。

ディメンションga:pageTitlega:pagePath
指標ga:pageviews
フィルターcountry == Japan && browser == Internet Explorer
example.report_popular.php
<!DOCTYPE html>
<html lang="ja">
<head> 
    <meta charset="utf-8"> 
    <title>GAPI-1.3: report</title>
    <style>body { font-size:small; } table { border-collapse:collapse; width:100%; } th,td { padding:3px 5px; } .num { text-align:right; }</style>
</head>
<body>
<?
/* Google Analyticsのログイン情報 */
define('ga_email','{メールアドレス}');
define('ga_password','{パスワード}');
/* プロファイルID */
define('ga_profile_id','{プロファイルID}');

/* ディメンション */
$dimensions=array('pageTitle','pagePath');
/* 指標 */
$metrics=array('pageviews','visits');
/* 結果のソート順と方向 */
$sort_metric='-pageviews';
/* フィルター */
$filter="";
/* 開始日・終了日(過去1週間) */
$start_date=date('Y-m-d'strtotime('-7 day'));
$end_date=date('Y-m-d'strtotime('-1 day'));
/* 開始インデックス */
$start_index=1;
/* 結果フィードの最大取得数 */
$max_results=10;

/* クラス読み込み */
require 'gapi.class.php';

/* 認証 */
$ga = new gapi(ga_email,ga_password);

/* データ取得 */
$ga->requestReportData(
    ga_profile_id,    /* プロファイルID */
    $dimensions,    /* ディメンション */
    $metrics,        /* 指標 */
    $sort_metric,    /* 結果のソート順と方向 */
    $filter,        /* フィルタ */
    $start_date,    /* 開始日 */
    $end_date,        /* 終了日 */
    $start_index,    /* 開始インデックス */
    $max_results    /* 結果の最大取得数 */
);
?>
<h3>人気エントリーTOP10</h3>
<p><?=$start_date;?> ~ <?=$end_date;?>(過去1週間)</p>
<table border="1">
    <tr><th>No.</th><th>コンテンツタイトル</th><th>ページビュー</th></tr>
<?
$i=1;
foreach($ga->getResults() as $result):
?>
        <tr>
            <td><?=$i;?></td>
            <td><a href='<?=$result->getPagepath();?>'><?=str_replace("|PHP & JavaScript Room","",$result->getPagetitle());?></a></td>
            <td class="num"><?=$result->getPageviews();?></td>
        </tr>
<?
    $i++;
endforeach
?>
</table>
</body>
</html>

ユニークページビューを取得

昨日のユニークページビューを最大1000件取得して表示します。 開始日・終了日の日付設定には、strtotime()を使用しています。

example.report_popular.php
<!DOCTYPE html>
<html lang="ja">
<head> 
    <meta charset="utf-8"> 
    <title>GAPI-1.3: report</title>
    <style>body { font-size:small; } table { border-collapse:collapse; width:100%; } th,td { padding:3px 5px; } .num { text-align:right; }</style>
</head>
<body>
<?
/* Google Analyticsのログイン情報 */
define('ga_email','{メールアドレス}');
define('ga_password','{パスワード}');
/* プロファイルID */
define('ga_profile_id','{プロファイルID}');

/* クラス読み込み */
require 'gapi.class.php';

/* 認証 */
$ga=new gapi(ga_email,ga_password);

/* ディメンション */
$dimensions=array('pageTitle','pagePath');
/* 指標 */
$metrics=array('uniquePageviews');
/* 結果のソート順と方向 */
$sort_metric='-uniquePageviews';
/* フィルター */
$filter="";
/* 開始日・終了日(昨日) */
$start_date=date('Y-m-d'strtotime('-1 day'));
$end_date=date('Y-m-d'strtotime('-1 day'));
/* 開始インデックス */
$start_index=1;
/* 結果の最大取得数 */
$max_results=null;

/* データ取得 */
$ga->requestReportData(
    ga_profile_id,    /* プロファイルID */
    $dimensions,    /* ディメンション */
    $metrics,        /* 指標 */
    $sort_metric,    /* 結果のソート順と方向 */
    $filter,        /* フィルタ */
    $start_date,    /* 開始日 */
    $end_date,        /* 終了日 */
    $start_index,    /* 開始インデックス */
    $max_results    /* 結果の最大取得数 */
);
?>
<h3><?=date('Y年m月d日'strtotime('-1 day'));?>のユニークユーザー数</h3>
<p>更新日時:<?=date("Y-m-d H:i:s",strtotime($ga->getUpdated())); ?></p>
<p>結果取得数:<?=$ga->getTotalResults(); ?></p>
<table border="1">
    <tr>
        <th nowrap>No</th>
        <th nowrap>パス</th>
        <th nowrap>ユニークページビュー</th>
    </tr>
<?
$i=1;
foreach($ga->getResults() as $result):
?>
    <tr>
        <td class="num" nowrap><?=$i;?></td>
        <td><a href="http://phpjavascriptroom.com<?=$result->getPagePath();?>" target="_blank"><?=str_replace("|PHP & JavaScript Room","",$result->getPagetitle());?></a></td>
        <td class="num"><?=$result->getUniquePageviews();?></td>
    </tr>
<?
    $i++;
endforeach
?>
    <tr>
        <th nowrap>合計</th>
        <td nowrap> </td>
        <td class="num"><?=$ga->getUniquePageviews(); ?></td>
    </tr>
</table>

フィルタリングしたレポートを出力

2011/6/12

フィルターを使用して、指定した条件にマッチするデータのみを取得することが可能です。

IEからのアクセス状況を取得

下記のサンプルではブラウザ毎のアクセス状況の内、日本かつInternet Explorerからのアクセス状況だけを取得して表示します。

ディメンションga:browserga:browserVersion
指標ga:pageviews
フィルターcountry == Japan && browser == Internet Explorer
examplefilter_browser.php
<!DOCTYPE html>
<html lang="ja">
<head> 
    <meta charset="utf-8"> 
    <title>GAPI-1.3: filter</title>
    <style>body { font-size:small; } table { border-collapse:collapse; width:100%; } th,td { padding:3px 5px; } .num { text-align:right; }</style>
</head>
<body>
<?
/* Google Analyticsのログイン情報 */
define('ga_email','{メールアドレス}');
define('ga_password','{パスワード}');
/* プロファイルID */
define('ga_profile_id','{プロファイルID}');

/* クラス読み込み */
require 'gapi.class.php';

/* 認証 */
$ga=new gapi(ga_email,ga_password);

/* ディメンション */
$dimensions=array('browser','browserVersion');
/* 指標 */
$metrics=array('pageviews','visits');
/* 結果のソート順と方向 */
$sort_metric='-visits';
/* フィルター */
$filter='country == Japan && browser == Internet Explorer';
/* 開始日・終了日(昨日)*/
$start_date=date('Y-m-d'strtotime('-1 day'));
$end_date=date('Y-m-d'strtotime('-1 day'));
/* 開始インデックス */
$start_index=1;
/* 結果の最大取得数 */
$max_results=100;

/* データ取得 */
$ga->requestReportData(
    ga_profile_id,    /* プロファイルID */
    $dimensions,    /* ディメンション */
    $metrics,        /* 指標 */
    $sort_metric,    /* 結果のソート順と方向 */
    $filter,        /* フィルタ */
    $start_date,    /* 開始日 */
    $end_date,        /* 終了日 */
    $start_index,    /* 開始インデックス */
    $max_results    /* 結果の最大取得数 */
);
?>
<h3><?=date('Y年m月d日'strtotime('-1 day'));?>のIEユーザー数</h3>
<p>更新日時:<?=date("Y-m-d H:i:s",strtotime($ga->getUpdated())); ?></p>
<p>結果取得数:<?=$ga->getTotalResults(); ?></p>
<table border="1">
    <tr>
        <th>ブラウザ&ブラウザ バージョン</th>
        <th>ページビュー</th>
        <th>訪問数</th>
    </tr>
<?
foreach($ga->getResults() as $result):
?>
    <tr>
        <td><?=$result;?></td>
        <td class="num"><?=$result->getPageviews();?></td>
        <td class="num"><?=$result->getVisits();?></td>
    </tr>
<?
endforeach
?>
    <tr>
        <th>合計</th>
        <td class="num"><?=$ga->getPageviews() ?>
        <td class="num"><?=$ga->getVisits(); ?></td>
    </tr>
</table>

スマートフォンからのアクセス状況を取得

下記のサンプルでは昨日のスマートフォン(iPhone/iPod touch/iPad/Android)からのアクセス状況を全件取得して表示します。

ディメンションga:operatingSystemga:operatingSystemVersion
指標ga:pageviews
フィルターoperatingSystem==Android || operatingSystem==iPhone || operatingSystem==iPod || operatingSystem==iPad
filter_os.php
<!DOCTYPE html>
<html lang="ja">
<head> 
    <meta charset="utf-8"> 
    <title>GAPI-1.3: filter</title>
    <style>body { font-size:small; } table { border-collapse:collapse; width:100%; } th,td { padding:3px 5px; } .num { text-align:right; }</style>
</head>
<body>
<?
/* Google Analyticsのログイン情報 */
define('ga_email','{メールアドレス}');
define('ga_password','{パスワード}');
/* プロファイルID */
define('ga_profile_id','{プロファイルID}');

/* クラス読み込み */
require 'gapi.class.php';

/* 認証 */
$ga=new gapi(ga_email,ga_password);

/* ディメンション */
$dimensions=array('operatingSystem','operatingSystemVersion');
/* 指標 */
$metrics=array('pageviews','visits');
/* 結果のソート順と方向 */
$sort_metric='-visits';
/* フィルター */
$filter='operatingSystem==Android || operatingSystem==iPhone || operatingSystem==iPod || operatingSystem==iPad';
/* 開始日・終了日(昨日)*/
$start_date=date('Y-m-d'strtotime('-1 day'));
$end_date=date('Y-m-d'strtotime('-1 day'));
/* 開始インデックス */
$start_index=1;
/* 結果の最大取得数 */
$max_results=null;

/* データ取得 */
$ga->requestReportData(
    ga_profile_id,    /* プロファイルID */
    $dimensions,    /* ディメンション */
    $metrics,        /* 指標 */
    $sort_metric,    /* 結果のソート順と方向 */
    $filter,        /* フィルタ */
    $start_date,    /* 開始日 */
    $end_date,        /* 終了日 */
    $start_index,    /* 開始インデックス */
    $max_results    /* 結果の最大取得数 */
);
?>
<h3><?=date('Y年m月d日'strtotime('-1 day'));?> - スマートフォンからのアクセス状況</h3>
<p>更新日時:<?=date("Y-m-d H:i:s",strtotime($ga->getUpdated())); ?></p>
<p>結果取得数:<?=$ga->getTotalResults(); ?></p>
<table border="1">
    <tr>
        <th>OS</th>
        <th>ページビュー</th>
        <th>訪問</th>
    </tr>
<?
foreach($ga->getResults() as $result):
?>
    <tr>
        <td><?=$result;?></td>
        <td class="num"><?=$result->getPageviews();?></td>
        <td class="num"><?=$result->getVisits();?></td>
    </tr>
<?
endforeach
?>
    <tr>
        <th>合計</th>
        <td class="num"><?=$ga->getPageviews() ?>
        <td class="num"><?=$ga->getVisits(); ?></td>
    </tr>
</table>

アクセスのあったAndroid端末のFlashバージョン番号を取得

下記のサンプルでは今までにアクセスのあったAndroid端末のFlashバージョン番号を番号順に表示します。

ディメンションga:operatingSystemga:operatingSystemVersionga:flashVersion
指標ga:pageviews
フィルターoperatingSystem==Android && flashVersion!='(not set)'
filter_android.php
<!DOCTYPE html>
<html lang="ja">
<head> 
    <meta charset="utf-8"> 
    <title>GAPI-1.3: filter</title>
    <style>body { font-size:small; } table { border-collapse:collapse; width:100%; } th,td { padding:3px 5px; } .num { text-align:right; }</style>
</head>
<body>
<?
/* Google Analyticsのログイン情報 */
define('ga_email','{メールアドレス}');
define('ga_password','{パスワード}');
/* プロファイルID */
define('ga_profile_id','{プロファイルID}');

/* クラス読み込み */
require 'gapi.class.php';

/* 認証 */
$ga=new gapi(ga_email,ga_password);

/* ディメンション */
$dimensions=array('operatingSystem','operatingSystemVersion','flashVersion');
/* 指標 */
$metrics=array('pageviews','visits');
/* 結果のソート順と方向 */
$sort_metric='-flashVersion';
/* フィルター */
$filter='operatingSystem==Android && flashVersion!="(not set)"';
/* 開始日・終了日(昨日)*/
$start_date="";
$end_date="";
/* 開始インデックス */
$start_index=1;
/* 結果の最大取得数 */
$max_results=null;

/* データ取得 */
$ga->requestReportData(
    ga_profile_id,    /* プロファイルID */
    $dimensions,    /* ディメンション */
    $metrics,        /* 指標 */
    $sort_metric,    /* 結果のソート順と方向 */
    $filter,        /* フィルタ */
    $start_date,    /* 開始日 */
    $end_date,        /* 終了日 */
    $start_index,    /* 開始インデックス */
    $max_results    /* 結果の最大取得数 */
);
?>
<h3>アクセスのあったAndroid端末のFlashバージョン番号を取得</h3>
<p>更新日時:<?=date("Y-m-d H:i:s",strtotime($ga->getUpdated())); ?></p>
<p>結果取得数:<?=$ga->getTotalResults(); ?></p>
<table border="1">
    <tr>
        <th>AndroidOS</th>
        <th>FlashVersion</th>
        <th>ページビュー</th>
    </tr>
<?
foreach($ga->getResults() as $result):
?>
    <tr>
        <td><?=$result;?></td>
        <td class="num"><?=$result->getFlashVersion();?></td>
        <td class="num"><?=$result->getPageviews();?></td>
    </tr>
<?
endforeach
?>
    <tr>
        <th>合計</th>
        <td class="num"> </td>
        <td class="num"><?=$ga->getPageviews() ?>
    </tr>
</table>

関連コンテンツ

Q. このサイトの情報はお役に立ちましたでしょうか?

投票する 投票結果を見る

管理人に【web拍手】を送るweb拍手(1行メッセージも送れます♪)

pagetop

polarized women