/** * Logs available Ad clients for an account. * * @param {string} accountName The resource name of the account that owns the * collection of ad clients. */functionlistAdClients(accountName){letpageToken;do{constresponse=AdSense.Accounts.Adclients.list(accountName,{pageToken:pageToken});if(!response.adClients){console.log('Noadclientsfoundforthisaccount.');return;}for(constadClientofresponse.adClients){console.log('Foundadclientforproduct"%s"withresourcename"%s".',adClient.productCode,adClient.name);console.log('ReportingdimensionID:%s',adClient.reportingDimensionId??'None');}pageToken=response.nextPageToken;}while(pageToken);}
/** * Lists ad units. * @param {string} adClientName The resource name of the ad client that owns the collection * of ad units. */functionlistAdUnits(adClientName){letpageToken;do{constresponse=AdSense.Accounts.Adclients.Adunits.list(adClientName,{pageSize:50,pageToken:pageToken});if(!response.adUnits){console.log('Noadunitsfoundforthisadclient.');return;}for(constadUnitofresponse.adUnits){console.log('Foundadunitwithresourcename"%s"anddisplayname"%s".',adUnit.name,adUnit.displayName);}pageToken=response.nextPageToken;}while(pageToken);}
/** * Generates a spreadsheet report for a specific ad client in an account. * @param {string} accountName The resource name of the account. * @param {string} adClientReportingDimensionId The reporting dimension ID * of the ad client. */functiongenerateReport(accountName,adClientReportingDimensionId){// Prepare report.consttoday=newDate();constoneWeekAgo=newDate(today.getTime()-7*24*60*60*1000);constreport=AdSense.Accounts.Reports.generate(accountName,{// Specify the desired ad client using a filter.filters:['AD_CLIENT_ID=='+escapeFilterParameter(adClientReportingDimensionId)],metrics:['PAGE_VIEWS','AD_REQUESTS','AD_REQUESTS_COVERAGE','CLICKS','AD_REQUESTS_CTR','COST_PER_CLICK','AD_REQUESTS_RPM','ESTIMATED_EARNINGS'],dimensions:['DATE'],...dateToJson('startDate',oneWeekAgo),...dateToJson('endDate',today),// Sort by ascending date.orderBy:['+DATE']});if(!report.rows){console.log('Norowsreturned.');return;}constspreadsheet=SpreadsheetApp.create('AdSenseReport');constsheet=spreadsheet.getActiveSheet();// Append the headers.sheet.appendRow(report.headers.map((header)=>header.name));// Append the results.sheet.getRange(2,1,report.rows.length,report.headers.length).setValues(report.rows.map((row)=>row.cells.map((cell)=>cell.value)));console.log('Reportspreadsheetcreated:%s',spreadsheet.getUrl());}/** * Escape special characters for a parameter being used in a filter. * @param {string} parameter The parameter to be escaped. * @return {string} The escaped parameter. */functionescapeFilterParameter(parameter){returnparameter.replace('\\','\\\\').replace(',','\\,');}/** * Returns the JSON representation of a Date object (as a google.type.Date). * * @param {string} paramName the name of the date parameter * @param {Date} value the date * @return {object} formatted date */functiondateToJson(paramName,value){return{[paramName+'.year']:value.getFullYear(),[paramName+'.month']:value.getMonth()+1,[paramName+'.day']:value.getDate()};}
[null,null,["最后更新时间 (UTC):2024-12-21。"],[[["The AdSense Management API allows you to programmatically access your AdSense account data within Google Apps Script."],["You can retrieve information about your account structure, including accounts, ad clients, and ad units."],["This API enables you to generate reports on your AdSense performance and export them to a Google Sheet."],["This is an advanced service that needs to be enabled before use, offering functionality similar to the public AdSense Management API."]]],[]]