שירות AdSense מאפשר לכם להשתמש ב-AdSense Management API ב-Apps Script. ממשק ה-API הזה מאפשר ללקוחות AdSense לקבל מידע על המבנה של החשבון שלהם ולהריץ דוחות על הביצועים שלו.
חומרי עזר
למידע מפורט על השירות הזה, קראו את מאמרי העזרה של AdSense Management API. כמו כל השירותים המתקדמים ב-Apps Script, גם בשירות AdSense נעשה שימוש באותם אובייקטים, שיטות ופרמטרים כמו ב-API הציבורי. מידע נוסף זמין במאמר איך נקבעות חתימות השיטות.
כדי לדווח על בעיות ולקבל תמיכה נוספת, אפשר לפרסם שאלה ב-Stack Overflow באמצעות התג adsense-api.
בדוגמה הזו מפורטים כל החשבונות שזמינים למשתמש. החשבונות מצוינים בתור שמות משאבים, לדוגמה accounts/pub-12345, שאפשר להשתמש בהם בשיטות אחרות, כמו רשימת לקוחות המודעות. שימו לב לשימוש באסימוני דפים כדי לגשת לרשימה המלאה של התוצאות.
/** * Lists available AdSense accounts. */functionlistAccounts(){letpageToken;do{constresponse=AdSense.Accounts.list({pageToken:pageToken});if(!response.accounts){console.log('Noaccountsfound.');return;}for(constaccountofresponse.accounts){console.log('Foundaccountwithresourcename"%s"anddisplayname"%s".',account.name,account.displayName);}pageToken=response.nextPageToken;}while(pageToken);}
הצגת רשימה של לקוחות מודעות
בדוגמה הזו מפורטים כל לקוחות המודעות בחשבון נתון. מציינים את החשבון בתור שם משאב, לדוגמה accounts/pub-12345. אפשר לקבל את שם המשאב של החשבון באמצעות הקוד לדוגמה List accounts.
/** * 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);}
הצגת רשימה של יחידות המודעות
בדוגמה הזו מפורטות כל יחידות המודעות של לקוח מודעות נתון. מציינים את לקוח המודעות בתור שם משאב, למשל accounts/pub-12345/adclients/ca-pub-12345.
אפשר לקבל את שם המשאב של חשבון הלקוח באמצעות הקוד לדוגמה של רשימת חשבונות לקוח של מודעות.
/** * 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);}
יצירת דוח
הדוגמה הזו יוצרת דוח לגבי חשבון AdSense שלכם ומציגה את התוצאות בגיליון אלקטרוני.
/** * 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()};}
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2024-12-21 (שעון UTC)."],[[["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."]]],[]]