Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Dịch vụ Analytics cho phép bạn sử dụng API Quản lý và API Báo cáo của Google Analytics trong Apps Script. Các API này giúp người dùng Analytics có thể lấy thông tin về cấu trúc tài khoản và chạy báo cáo về hiệu suất của tài khoản.
Tài liệu tham khảo
Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo cho các API Google Analytics:
Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Analytics sử dụng các đối tượng, phương thức và tham số giống như API công khai. Để biết thêm thông tin, hãy xem phần Cách xác định chữ ký phương thức.
Để báo cáo vấn đề và tìm các dịch vụ hỗ trợ khác, hãy xem các trang hỗ trợ tương ứng:
/** * Lists Analytics accounts. */functionlistAccounts(){try{constaccounts=Analytics.Management.Accounts.list();if(!accounts.items||!accounts.items.length){console.log('Noaccountsfound.');return;}for(leti=0;i < accounts.items.length;i++){constaccount=accounts.items[i];console.log('Account:name"%s",id"%s".',account.name,account.id);// List web properties in the account.listWebProperties(account.id);}}catch(e){// TODO (Developer) - Handle exceptionconsole.log('Failedwitherror:%s',e.error);}}/** * Lists web properites for an Analytics account. * @param {string} accountId The account ID. */functionlistWebProperties(accountId){try{constwebProperties=Analytics.Management.Webproperties.list(accountId);if(!webProperties.items||!webProperties.items.length){console.log('\tNowebpropertiesfound.');return;}for(leti=0;i < webProperties.items.length;i++){constwebProperty=webProperties.items[i];console.log('\tWebProperty:name"%s",id"%s".',webProperty.name,webProperty.id);// List profiles in the web property.listProfiles(accountId,webProperty.id);}}catch(e){// TODO (Developer) - Handle exceptionconsole.log('Failedwitherror:%s',e.error);}}/** * Logs a list of Analytics accounts profiles. * @param {string} accountId The Analytics account ID * @param {string} webPropertyId The web property ID */functionlistProfiles(accountId,webPropertyId){// Note: If you experience "Quota Error: User Rate Limit Exceeded" errors// due to the number of accounts or profiles you have, you may be able to// avoid it by adding a Utilities.sleep(1000) statement here.try{constprofiles=Analytics.Management.Profiles.list(accountId,webPropertyId);if(!profiles.items||!profiles.items.length){console.log('\t\tNowebpropertiesfound.');return;}for(leti=0;i < profiles.items.length;i++){constprofile=profiles.items[i];console.log('\t\tProfile:name"%s",id"%s".',profile.name,profile.id);}}catch(e){// TODO (Developer) - Handle exceptionconsole.log('Failedwitherror:%s',e.error);}}
Chạy báo cáo
Mẫu này chạy một báo cáo để truy xuất 25 từ khoá và nguồn lưu lượng truy cập hàng đầu, đồng thời lưu trữ kết quả trong một bảng tính mới.
/** * Runs a report of an Analytics profile ID. Creates a sheet with the report. * @param {string} profileId The profile ID. */functionrunReport(profileId){consttoday=newDate();constoneWeekAgo=newDate(today.getTime()-7*24*60*60*1000);conststartDate=Utilities.formatDate(oneWeekAgo,Session.getScriptTimeZone(),'yyyy-MM-dd');constendDate=Utilities.formatDate(today,Session.getScriptTimeZone(),'yyyy-MM-dd');consttableId='ga:'+profileId;constmetric='ga:visits';constoptions={'dimensions':'ga:source,ga:keyword','sort':'-ga:visits,ga:source','filters':'ga:medium==organic','max-results':25};constreport=Analytics.Data.Ga.get(tableId,startDate,endDate,metric,options);if(!report.rows){console.log('Norowsreturned.');return;}constspreadsheet=SpreadsheetApp.create('GoogleAnalyticsReport');constsheet=spreadsheet.getActiveSheet();// Append the headers.constheaders=report.columnHeaders.map((columnHeader)=>{returncolumnHeader.name;});sheet.appendRow(headers);// Append the results.sheet.getRange(2,1,report.rows.length,headers.length).setValues(report.rows);console.log('Reportspreadsheetcreated:%s',spreadsheet.getUrl());}
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2024-12-21 UTC."],[[["The Analytics service enables the use of Google Analytics Management and Reporting APIs within Apps Script to access account structure and performance data."],["This advanced service requires prior enabling before utilization and mirrors the functionality of the public Google Analytics APIs."],["Provided sample code demonstrates listing account structures (accounts, web properties, and profiles) and running reports to extract keyword and traffic source data."],["Comprehensive reference documentation and support pages for various Google Analytics APIs are linked for further information and troubleshooting."]]],[]]