毎月初めに(勝手に)公開している仕事先のサイトの訪問数による色んなシェア(※参照※)、カスタムレポート作ってそこから数値をペタペタコピペしてたけど、暇つぶしがてらGoogle Analyticsにアクセスして集計してブログに投稿するためのhtmlにしたテキストをメール送信するGoogle Apps Scriptを書いて、毎月3日の適当な時間帯のトリガーで設定することにしてみた。
ソースコードをさらしてみる
コンストラクタは”レポートのタイトル”、”レポートの開始日”、”レポートの終了日”を引数にとる、GAからデータを取得させる関数、メール送信用に文字列を作らせる関数を持ったMonthlyShareというクラス(らしきもの)を準備。Main.gsのmain関数で必要なレポート分だけインスタンスを作って本文を作成させてメールする。
って感じ。
Google Analyticsからデータを取得するAnalytics.Data.Ga.get(ids, start-date, end-date, metrics, optionalArgs)
の”ids”にあたるtable idだかprofile IDだかとメール送信先のメールアドレスは「プロジェクトのプロパティ」のユーザープロパティに入れておいた。
UserProperties.getProperty( 'プロパティ名' )
※Google Apps Scriptの準備の仕方とかは調べたほうが詳しく書かれているのがいっぱいあるので、特に記述しない…。
/*****
* MonthlyShare Class
* @settings { 'month_first', 'month_last', 'report_title', 'report_max' }
*****/
var MonthlyShare = function( settings ) {
var a;
// month_first( 1st of last month )
a = new Date( this.today.getYear(), this.today.getMonth() - 1, 1 )
, this.month_first = settings.month_first || Utilities.formatDate( a, 'JST', 'yyyy-MM-dd' )
;
// month_last( last day of last manths )
a = new Date( this.today.getYear(), this.today.getMonth(), 0 )
, this.month_last = settings.month_last || Utilities.formatDate( a, 'JST', 'yyyy-MM-dd' )
;
// report_title
this.report_title = settings.report_title || this.report_title;
// report_max
this.report_max = settings.report_max || this.report_max;
};
MonthlyShare.prototype = {
ga_profile_ids: UserProperties.getProperty( 'ga_profile_ids' )
, today: new Date()
, month_first: ''
, month_last: ''
, report_title: 'Monthly Share Report'
, report_max: 5
, report_gadata: {}
, report_mailtext: ''
, ga_metrix: ''
, ga_options: {}
, gaData: function( met, opt ) {
this.ga_metrix = met
, this.ga_options = opt;
/**
Analytics.Data.Ga.get(ids, start-date, end-date, metrics, optionalArgs)
**/
return this.report_gadata = Analytics.Data.Ga.get( this.ga_profile_ids, this.month_first, this.month_last, met, opt );
}
, dimensionMix: function( d ) {
var a = new Array();
for( var i = 0, m = d.length, b = 0, s = ''; i < m; i++, s = '' ){
c = d[ i ].length - 1;
for( var j = 0; j < c; j++ ){
if( !!s )
s += '+';
s += d[ i ][ j ];
}
a.push( [ s, d[ i ][ c ] ] );
}
return a;
}
, analysisShare: function( d ) {
var a = new Array()
, ttl_val = this.report_gadata.getTotalsForAllResults()[ 'ga:visits' ]
;
for( var i = 0, m = d.length, b = 0; i < m; i++ ){
b = Utilities.formatString( '%.2f', Math.round( d[ i ][ 1 ] / ttl_val * 10000 ) / 100 ) + '%';
a.push( [ d[ i ][ 0 ], b ] );
}
return a;
}
, createMailText: function() {
var a = '', rows, b;
rows = this.report_gadata.getRows();
if( rows.lenght <= 0 )
return '';
b = rows.slice( 0, this.report_max - 1 );
if( rows[ 0 ].length > 2 ){
b = this.dimensionMix( rows )
}else{
b = rows;
}
b = this.analysisShare( b );
a += '
- ' + this.report_title + '
';
for( var i = 0; i < this.report_max; i++ ){
a += '- ' + b[ i ][ 0 ] + '
- ' + b[ i ][ 1 ] + '
';
}
a += '
';
return this.mailText( a );
}
, mailText: function( s ) {
if( !!s )
this.report_mailtext = s
return this.report_mailtext;
}
};
function main() {
var mail_text = ''
, options = {}
, month_first
, month_last
;
/*
// for Test
var today = new Date();
month_first = Utilities.formatDate( new Date( today.getYear(), today.getMonth() - 1, 1 ), 'JST', 'yyyy-MM-dd' );
month_last = Utilities.formatDate( new Date( today.getYear(), today.getMonth(), 0 ), 'JST', 'yyyy-MM-dd' );
;
*/
// ----------------
var ms0 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': '全デバイス・OSシェア'
} );
options = {
'dimensions': 'ga:operatingSystem'
, 'sort': '-ga:visits'
};
ms0.gaData( 'ga:visits', options );
mail_text += ms0.createMailText();
// ----------------
var ms1 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': '全デバイス・ブラウザシェア'
} );
options = {
'dimensions': 'ga:browser'
, 'sort': '-ga:visits'
};
ms1.gaData( 'ga:visits', options );
mail_text += ms1.createMailText();
// ----------------
var ms2 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': '全デバイス・ブラウザ+バージョンシェア'
} );
options = {
'dimensions': 'ga:browser,ga:browserVersion'
, 'sort': '-ga:visits'
};
ms2.gaData( 'ga:visits', options );
mail_text += ms2.createMailText();
// ----------------
var ms3 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': '全デバイス・OS+ブラウザシェア'
} );
options = {
'dimensions': 'ga:operatingSystem,ga:browser'
, 'sort': '-ga:visits'
};
ms3.gaData( 'ga:visits', options );
mail_text += ms3.createMailText();
// ----------------
var ms4 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': 'PC系縛り・ブラウザ+バージョンシェア'
} );
options = {
'dimensions': 'ga:browser,ga:browserVersion'
, 'sort': '-ga:visits'
, 'filters': 'ga:deviceCategory==desktop'
};
ms4.gaData( 'ga:visits', options );
mail_text += ms4.createMailText();
// ----------------
var ms5 = new MonthlyShare( {
'month_first': month_first
, 'month_last': month_last
, 'report_title': 'スマホ系縛り・OS+バージョンシェア'
} );
options = {
'dimensions': 'ga:operatingSystem,ga:operatingSystemVersion'
, 'sort': '-ga:visits'
, 'filters': 'ga:deviceCategory==mobile,ga:deviceCategory==tablet'
};
ms5.gaData( 'ga:visits', options );
mail_text += ms5.createMailText();
// ----------------
var report_month = Utilities.formatDate( new Date( ms5.month_first.replace( /\-/g, '/' ) ), 'JST', 'yyyy/MMM' );
MailApp.sendEmail( {
'to': UserProperties.getProperty( 'mail_to' )
, 'subject': '[ ' + report_month + ' ]Automatically Monthly Report'
, 'body': mail_text
} )
// Logger.log( mail_text );
};
一番よく見た公式サイトのリファレンスとか
Analytics Service
Dimensions & Metrics Reference
Properties Services
Utilities Service
Mail Service
何か間違えていたら指摘していただけると…
そして、誰かの何かの参考になれば…