/*********************** * データ取得処理 ***********************/ function main_ga4() { var now = new Date(); var monthStartDate = new Date(now.getFullYear(), now.getMonth(), 1); // 月初 var monthEndDate = new Date(now.getFullYear(), now.getMonth()+1, -1); // 月末 var month_start_day = Utilities.formatDate(monthStartDate, 'Asia/Tokyo', 'yyyy-MM-dd'); var month_end_day = Utilities.formatDate(monthEndDate, 'Asia/Tokyo', 'yyyy-MM-dd'); var ga4_list = get_ga4_property_id(); try { // 各GA4プロパティごとにイベント数を取得する for(var k = 0; k < ga4_list.length ; k++){ // 指標 metric_list = ["eventCount"]; metrics = []; for(var x = 0; x < metric_list.length; x++){ let metricx = AnalyticsData.newMetric(); metricx.name = metric_list[x]; metrics.push(metricx); } // ディメンション dimension_list = []; dimensions = []; for(var x = 0; x < dimension_list.length; x++){ let metricx = AnalyticsData.newDimension(); metricx.name = dimension_list[x]; dimensions.push(metricx); } // データの日付範囲 const dateRange = AnalyticsData.newDateRange(); dateRange.startDate = month_start_day; dateRange.endDate = month_end_day; // 上で作った設定値をリクエストパラメータに設定 const request = AnalyticsData.newRunReportRequest(); request.dimensions = dimensions; request.metrics = metrics; request.dateRanges = dateRange; // API 実行 var report = AnalyticsData.Properties.runReport(request,'properties/'+ga4_list[k].property_id); if (!report.rows) { //Logger.log('No rows returned.'); } else { ga4_list[k].total_event_count = report.rows[0].metricValues[0].value // 合計イベント数 } } writeReport(ga4_list); } catch (e) { // TODO (Developer) - Handle exception Logger.log(`Failed with error: ${e}`); } } /******************************************* * 指定したGAアカウントでのGA4プロパティID一覧の取得 * * ▼ analyticsadmin.accounts.list * https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/list?hl=ja * ▼ analyticsadmin.properties.list * https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/list?hl=ja *******************************************/ // GAのアカウントとGA4のプロパティ一覧の取得 function get_ga4_property_id(){ const accounts = AnalyticsAdmin.AccountSummaries.list(); // GA4のアカウントとプロパティ情報 var ga4_list = []; for(i = 0; i < accounts.accountSummaries.length; i++){ // account var account_name = accounts.accountSummaries[i].displayName var account_id = accounts.accountSummaries[i].name.split("/")[1]; // property for(j = 0; j < accounts.accountSummaries[i].propertySummaries.length ; j++){ var property_name = accounts.accountSummaries[i].propertySummaries[j].displayName; var property_id = accounts.accountSummaries[i].propertySummaries[j].property.split("/")[1]; ga4_list.push(new GA4(account_id, account_name, property_id, property_name, null)); } } return ga4_list; } // https://qiita.com/aqril_1132/items/100562e4bc39e8e9191f // これが欲しかったのよ。。。https://developers.google.com/apps-script/advanced/analyticsdata?hl=ja // https://developers.google.com/apps-script/advanced/analytics?hl=ja /***************************************** * GA4のオブジェクトインスタンスを作成(今後Viewとかに何か使えればなー。数値出すとか) *****************************************/ class GA4{ constructor(account_id, account_name, property_id, property_name){ this.account_id = account_id; this.account_name = account_name; this.property_id = property_id; this.property_name = property_name; //this.subproperty_cnt = null; this.subproperty_cnt = 0; this.total_event_count = 0; } } /*********************** * スプレッドシート書き込み ***********************/ function writeReport(ga4) { var today = new Date(); var year = today.getFullYear(); var month = today.getMonth() + 1; const sheet_name = year+"年"+month+"月"; try{ const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name); // 書き込み処理 const header = [["アカウントID", "アカウント名", "GA4プロパティID", "GA4プロパティ名", "サブプロパティ数", "合計イベント数"]]; // なんか2重にすればできるらしい。2重配列 sheet.getRange(1, 1, 1, 6).setValues(header); // 下がって右いって(ここ起点)、下がって右いって for(i = 0; i < ga4.length ; i++){ sheet.getRange(i+2, 1, 1, 6).setValues( [[ga4[i].account_id, ga4[i].account_name, ga4[i].property_id, ga4[i].property_name, ga4[i].subproperty_cnt, ga4[i].total_event_count]] ); } Logger.log(`Report spreadsheet created: ${sheet.getSheetName()}`); } catch (e) { // TODO (Developer) - Handle exception Logger.log(`Failed to write report with error: ${e}`); } }
GA4でプロパティごとに合計イベント数を取得する