拡張サービスの「Google Analytics API」を使って、Google アナリティクスから指定した年月の上位アクセスページ情報を取得してみます。
※ 下記コードを実行する前に「[Google Apps Script]拡張サービスを使用する。」を参考に、Google Analytics APIを有効にしておく必要があります。
※ コード中の「プロファイルID」は下記Webページにある手順で取得することができます。
・Google AnalyticsのプロファイルIDをカンタンに確認する方法
http://www.teradas.net/archives/11637/
・Google AnalyticsのプロファイルIDはビュー IDに変わっています。
http://arayu.jp/google-profile-id.html
function myFunction() {
getList(2014, 4, '(プロファイルID)'); //201年4月の情報取得
}
function getList(y, m, profileId){
var startDate = new Date(y, m - 1, 1);
var endDate = new Date(y, m, 0);
startDate = Utilities.formatDate(startDate, Session.getScriptTimeZone(), 'yyyy-MM-dd');
endDate = Utilities.formatDate(endDate, Session.getScriptTimeZone(), 'yyyy-MM-dd');
var metrics = 'ga:pageviews';
var options = {
dimensions: 'ga:pagePath,ga:pageTitle',
sort: '-ga:pageviews'
}
var report = Analytics.Data.Ga.get('ga:' + profileId, startDate, endDate, metrics, options);
if(report.rows){
for(var i = 0; i < 30; i++){
Logger.log('[' + i + ']path:' + report.rows[i][0] + ',title:' + report.rows[i][1] + ',pageviews:' + report.rows[i][2]);
}
}
}
初めてGoogle Apps Scriptからアナリティクスのデータを取得してみましたが、思った以上に簡単に処理できました。
“その月の人気ページ一覧”の作成など、Google Apps Scriptを使って色々出来そうです。
■ 参考Webページ
- Dimensions & Metrics Reference
- https://developers.google.com/analytics/devguides/reporting/core/dimsmets
- Automated Access to Google Analytics Data in Google Sheets
- https://developers.google.com/analytics/solutions/articles/reporting-apps-script?hl=ja
- formatDate(date, timeZone, format)
- https://developers.google.com/apps-script/reference/utilities/utilities?hl=ja#formatDate%28Date,String,String%29
- getScriptTimeZone()
- https://developers.google.com/apps-script/reference/base/session?hl=ja#getScriptTimeZone%28%29
















この記事へのコメントはありません。