使用 Analytics Admin 服務,在 Google Apps Script 中以程式輔助方式進行。
Analytics Admin 服務可讓您在 Google Apps Script 中使用 Google Analytics Admin API v1。Google Analytics Admin API 提供 Google Analytics 4 (GA4) 設定資料的程式輔助存取權,且僅與 GA4 資源相容。
/** * Logs the Google Analytics accounts accessible by the current user. */functionlistAccounts(){try{accounts=AnalyticsAdmin.Accounts.list();if(!accounts.items||!accounts.items.length){console.log("No accounts found.");return;}for(leti=0;i < accounts.items.length;i++){constaccount=accounts.items[i];console.log('Account: name "%s", displayName "%s".',account.name,account.displayName,);}}catch(e){// TODO (Developer) - Handle exceptionconsole.log("Failed with error: %s",e.error);}}