Apps Script を使ってスプレッドシートに Google アナリティクスのアカウントを一覧表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* アカウント-プロパティ-ビュー 情報の一覧を取得し、スプレッドシートに印字する | |
*/ | |
function putAccountsList() { | |
var m = Analytics.Management.AccountSummaries.list(); | |
Logger.log(m); | |
// 見出し行 | |
var headerRow = [ | |
'アカウント#ID', | |
'アカウント#名前', | |
'アカウント#ユーザーの権限', | |
'アカウント#スター付き', | |
'アカウント#作成日時', | |
'アカウント#最終更新日時', | |
'プロパティ#ID', | |
'プロパティ#名前', | |
'プロパティ#サイトURL', | |
'プロパティ#ユーザーの権限', | |
'プロパティ#スター付き', | |
'プロパティ#作成日時', | |
'プロパティ#最終更新日時', | |
'ビュー#ID', | |
'ビュー#名前', | |
'ビュー#通貨のタイプ', | |
'ビュー#タイムゾーン', | |
'ビュー#サイトURL', | |
'ビュー#デフォルトページ', | |
'ビュー#ユーザーの権限', | |
'ビュー#スター付き', | |
'ビュー#eコマーストラッキング有効', | |
'ビュー#拡張eコマーストラッキング有効', | |
'ビュー#ロボットフィルタリング有効', | |
'ビュー#作成日時', | |
'ビュー#最終更新日時', | |
]; | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; | |
var accounts = listAccounts(); | |
putMatrix( | |
/* sheet = */ sheet, | |
/* row = */ 1, | |
/* column = */ 1, | |
/* data = */ [].concat([headerRow], accounts) | |
); | |
// Logger.log(accounts); | |
} | |
/* ========================================================================= */ | |
/** | |
* プロパティ情報とそれに紐付くプロパティ情報,ビュー情報を取得する | |
* | |
* @return Array[] | |
*/ | |
function listAccounts() { | |
var accounts = Analytics.Management.Accounts.list(); | |
if (accounts.items) { | |
return concatMap(function(account) { | |
var accountProps = [ | |
account.id, // アカウントID | |
account.name, // 名前 | |
jaPermissions(account.permissions.effective).join(), | |
// このアカウントに対するユーザーのすべての権限 | |
!!account.starred, // スター付きかどうか | |
formatDate(account.created), // 作成日時 | |
formatDate(account.updated), // 最終更新日時 | |
]; | |
// 現在のアカウントに紐付くプロパティ情報を取得してアカウント情報と連結 | |
return listWebProperties(account.id).map(concatBefore(accountProps)); | |
}, accounts.items); | |
} else { | |
return []; | |
} | |
} | |
/** | |
* プロパティ情報とそれに紐付くビュー情報を取得する | |
* | |
* @param string accountId プロパティが属するアカウントのID | |
* @return Array[] | |
*/ | |
function listWebProperties(accountId) { | |
var webProperties = Analytics.Management.Webproperties.list(accountId); | |
if (webProperties.items) { | |
return concatMap(function(property) { | |
var properyProps = [ | |
property.id, // プロパティID | |
property.name, // 名前 | |
property.websiteUrl, // ウェブサイトのURL | |
jaPermissions(property.permissions.effective).join(), | |
// このプロパティに対するユーザーのすべての権限 | |
!!property.starred, // スター付きかどうか | |
formatDate(property.created), // 作成日時 | |
formatDate(property.updated), // 最終更新日時 | |
]; | |
// 現在のプロパティに紐付くビュー情報を取得してプロパティ情報と連結 | |
return listProfiles(accountId, property.id).map(concatBefore(properyProps)); | |
}, webProperties.items); | |
} else { | |
return []; | |
} | |
} | |
/** | |
* ビュー情報を取得する | |
* | |
* @param string accountId ビューが属するアカウントのID | |
* @param string webPropertyId ビューが属するプロパティのID | |
* @return Array[] | |
*/ | |
function listProfiles(accountId, webPropertyId) { | |
Utilities.sleep(200); | |
var profiles = Analytics.Management.Profiles.list(accountId, webPropertyId); | |
if (profiles.items) { | |
return profiles.items.map(function(profile) { | |
return [ | |
profile.id, // ビューID | |
profile.name, // 名前 | |
profile.currency, // 関連付けられた通貨のタイプ | |
profile.timezone, // 設定されているタイムゾーン | |
profile.websiteUrl, // ウェブサイトのURL | |
profile.defaultPage || '-', // デフォルトページ | |
jaPermissions(profile.permissions.effective).join(), | |
// このビュー(旧プロファイル)に対するユーザーのすべての権限 | |
!!profile.eCommerceTracking, // eコマーストラッキングが有効かどうか | |
!!profile.enhancedECommerceTracking, // 拡張eコマーストラッキングが有効かどうか | |
!!profile.botFilteringEnabled, // ロボットのフィルタリングが有効かどうか | |
!!profile.starred, // スター付きかどうか | |
formatDate(profile.created), // 作成日時 | |
formatDate(profile.updated), // 最終更新日時 | |
]; | |
}); | |
} else { | |
return []; | |
} | |
} | |
/* ========================================================================= */ | |
/** | |
* 与えられた配列の前に別の配列を連結する関数を返す | |
* | |
* @param *[] head | |
* @return function(*[]):*[] | |
* | |
* @example | |
* var f = concatBefore(['A', 'B', 'C']) | |
* f(['X', 'Y', 'Z']) | |
* // => ['A', 'B', 'C', 'X', 'Y', 'Z'] | |
*/ | |
function concatBefore(head) { | |
return function(tail) { | |
return [].concat(head, tail); | |
}; | |
} | |
/** | |
* map した後に concat する | |
* | |
* @param function(*):*[] func | |
* @param *[] arr | |
* @return *[] | |
* | |
* @see {@link http://hackage.haskell.org/package/base-4.10.0.0/docs/Prelude.html#v:concatMap} | |
*/ | |
function concatMap(func, arr) { | |
return [].concat.apply([], arr.map(func)); | |
} | |
/** | |
* ISO 8601 形式の日時文字列を Excel 標準の日時文字列に変換 | |
* | |
* @param string dateStr ISO 8601 形式の日時文字列 | |
* @return string Excel 標準の日時文字列 | |
* | |
* @see {@link https://ja.wikipedia.org/wiki/ISO_8601} | |
* | |
* @example | |
* formatDate('1970-01-02T03:04:05Z') | |
* // => '1970/01/02 03:04:05' | |
*/ | |
function formatDate(dateStr) { | |
return Utilities.formatDate(new Date(dateStr), 'UTC', 'yyyy/MM/dd HH:mm:ss'); | |
} | |
/** | |
* ユーザーの権限を日本語表記に翻訳 | |
* | |
* @param string[] permissions 翻訳前のユーザーの権限の配列 | |
* @return string[] 翻訳後(日本語表記)のユーザーの権限の配列 | |
* | |
* @example | |
* jaPermissions(['READ_AND_ANALYZE', 'COLLABORATE']) | |
* // => ['表示と分析', '共有設定'] | |
*/ | |
function jaPermissions(permissions) { | |
dict = { | |
MANAGE_USERS: 'ユーザー管理', | |
EDIT: '編集', | |
COLLABORATE: '共有設定', | |
READ_AND_ANALYZE: '表示と分析', | |
}; | |
return permissions.map(function(p) { return dict[p]; }); | |
} | |
/** | |
* 二重配列を値をスプレッドシートに印字する | |
* | |
* @param Sheet sheet 印字対象のシート | |
* @param int row 印字範囲上端の行番号 | |
* @param int column 印字範囲左端の列番号 | |
* @param *[][] data 印字されるデータ | |
* @return Range データが印字された範囲を示す Range オブジェクト | |
*/ | |
function putMatrix(sheet, row, column, data) { | |
var numRows = data.length; | |
var numColumns = Math.max.apply(null, data.map(function(row) { return row.length; })); | |
return sheet.getRange(row, column, numRows, numColumns).setValues(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment