// google analytics api reference: https://developers.google.com/apps-script/advanced/analytics function runReport(profileId, startDate, endDate) { var tableId = 'ga:' + profileId; var metric = 'ga:pageviews'; var options = {}; var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric, options); return report; // 以下Spread Sheetに出力 if (report.rows) { var spreadsheet = SpreadsheetApp.create('Google Analytics Report'); var sheet = spreadsheet.getActiveSheet(); // Append the headers. var headers = report.columnHeaders.map(function(columnHeader) { return columnHeader.name; }); sheet.appendRow(headers); // Append the results. sheet.getRange(2, 1, report.rows.length, headers.length) .setValues(report.rows); Logger.log('Report spreadsheetを作りました: %s', spreadsheet.getUrl()); } else { Logger.log('結果を取得できませんでした.'); } } function listProfiles(accountId, webPropertyId) { var profiles = Analytics.Management.Profiles.list(accountId, webPropertyId); var result = []; if (profiles.items && profiles.items.length) { for (var i = 0; i < profiles.items.length; i++) { var profile = profiles.items[i]; Logger.log('\t\tProfile: name "%s", id "%s".', profile.name, profile.id); // runReport(profile.id); result.push(profile); } } else { Logger.log('\t\tNo web properties found.'); } return result; } function listWebProperties(accountId) { var webProperties = Analytics.Management.Webproperties.list(accountId); var result = []; if (webProperties.items && webProperties.items.length) { for (var i = 0; i < webProperties.items.length; i++) { var webProperty = webProperties.items[i]; Logger.log('\tプロパティ: 名前 "%s", id "%s".', webProperty.name, webProperty.id); // listProfiles(accountId, webProperty.id); result.push(webProperty); } } else { Logger.log('\tプロパティが見つかりません。'); } return result; } function listAccounts() { var accounts = Analytics.Management.Accounts.list(); var result = []; if (accounts.items && accounts.items.length) { for (var i = 0; i < accounts.items.length; i++) { var account = accounts.items[i]; Logger.log('アカウント: 名前 "%s", id "%s".', account.name, account.id); // listWebProperties(account.id); result.push(account); } } else { Logger.log('アカウントが見つかりませんでした。'); } // アカウントは一つしかないので最初の要素だけを返す return result[0]; } function googleAnalyticsNotifier() { // GASに登録しているapi tokenを取得(ファイル>プロジェクトのプロパティ>スクリプトのプロパティ var token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN'); // Slack Appインスタンスの取得 var slackApp = SlackApp.create(token); var today = new Date(); var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000); var yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000); var startDate = Utilities.formatDate(yesterday, Session.getTimeZone(), 'yyyy-MM-dd'); // var startDate = Utilities.formatDate(oneWeekAgo, Session.getTimeZone(), 'yyyy-MM-dd'); var endDate = Utilities.formatDate(today, Session.getTimeZone(), 'yyyy-MM-dd'); var channelId = "#notifications"; //チャンネル名 var options = { username: "Google Analytics", // 投稿するbot名 icon_url: "https://developers.google.com/analytics/images/terms/logo_lockup_analytics_icon_vertical_black_2x.png?hl=ja" }; var account = listAccounts(); var webProperties = listWebProperties(account.id); for (var i = 0; i < webProperties.length; i++) { var profiles = listProfiles(account.id, webProperties[i].id); for (var i = 0; i < profiles.length; i++) { var report = runReport(profiles[i].id, startDate, endDate); var message = startDate + 'の *' + webProperties[i].name + '[' + webProperties[i].websiteUrl + ']* のPV数は、 `' + report.totalsForAllResults['ga:pageviews'] + '` です。'; // var message = startDate + '〜' + endDate + 'の *' + profiles[i].name + '* のPV数は、 `' + report.totalsForAllResults['ga:pageviews'] + '` です。'; slackApp.postMessage(channelId, message, options); } } }