สคริปต์ Ads Manager

คลาส AdsManagerApp ในสคริปต์ Google Ads ช่วยให้คุณจัดการบัญชีที่ลิงก์ภายใต้บัญชีดูแลจัดการได้ คุณจัดการบัญชีผู้ลงโฆษณาทั้งหมดได้ผ่านสคริปต์เดียวแทนที่จะสร้างสคริปต์แยกต่างหากสำหรับแต่ละบัญชี

ดึงข้อมูลรายการบัญชี

คุณสามารถดึงข้อมูลบัญชีภายใต้บัญชีดูแลจัดการได้โดยใช้เมธอด accounts เช่น

const accountSelector = AdsManagerApp.accounts()     .withCondition('customer_client.descriptive_name = "My Account"');  const accountIterator = accountSelector.get(); 

บัญชีที่กู้คืนได้มีข้อจำกัดบางประการดังนี้

  • คุณจะเรียกข้อมูลบัญชีดูแลจัดการไม่ได้หากมีลำดับชั้นแบบหลายระดับ เลือกได้เฉพาะบัญชีของลูกค้า
  • โดยค่าเริ่มต้น ระบบจะไม่แสดงบัญชีที่ปิด ยกเลิก และถูกระงับ คุณ สามารถลบล้างลักษณะการทำงานนี้ได้โดยการเรียกใช้ withCondition ระบุตัวกรองอื่น สำหรับ customer_client.status

accounts จะเรียกรายการบัญชีลูกค้าทั้งหมดภายใต้ลำดับชั้นบัญชีดูแลจัดการโดยค่าเริ่มต้น คุณสามารถใช้วิธี withLimit ของคลาส ManagedAccountSelector เพื่อจำกัดจำนวนบัญชีที่สคริปต์ดึงข้อมูลได้ อีกทางเลือกหนึ่งคือการเลือกบัญชีตามรหัสลูกค้าโดยใช้วิธี withIds ต่อไปนี้

// Hyphens in the account ID are optional. const accountSelector = AdsManagerApp.accounts()     .withIds(['123-456-7890', '234-567-8901', '345-678-9012']); 

ทำงานในบัญชีลูกค้า

เมื่อเรียกข้อมูลบัญชีลูกค้าแล้ว คุณจะวนซ้ำผ่านบัญชีเหล่านั้นได้โดยใช้วิธีการ ของตัววนซ้ำ hasNext และ next คุณต้องใช้วิธี select เพื่อเปลี่ยนบริบทการดำเนินการเป็นบัญชีลูกค้า หลังจากเลือกบัญชีลูกค้าแล้ว การเรียก API เพิ่มเติมจะมีผลกับบัญชีลูกค้าจนกว่าคุณจะเลือกบัญชีอื่นอย่างชัดเจน

// Keep track of the manager account for future reference. const managerAccount = AdsApp.currentAccount();  // Select your accounts const accountIterator = AdsManagerApp.accounts() // ... Write some logic here to select the accounts you want using // withCondition or withIds  // Iterate through the list of accounts for (const account of accountIterator) {   // Select the client account.   AdsManagerApp.select(account);    // Select Search and Display campaigns under the client account   const campaignIterator = AdsApp.campaigns().get();    // Operate on client account   ... } 

ทำงานในบัญชีแบบคู่ขนาน

สคริปต์ Google Ads ช่วยให้คุณดำเนินการในบัญชีลูกค้าหลายบัญชีได้แบบคู่ขนานโดยใช้วิธี executeInParallel ของคลาส ManagedAccountSelector เมธอด executeInParallel มีลายเซ็นต่อไปนี้

function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput); 

เมธอด executeInParallel จะเรียกใช้ฟังก์ชันที่ระบุโดย functionName ในแต่ละ ManagedAccount ที่ ManagedAccountSelector ตรงกัน เมื่อประมวลผลบัญชีทั้งหมดแล้ว ระบบจะเรียกใช้ฟังก์ชันเรียกกลับ (หากoptionalCallbackFunctionNameระบุไว้) 1 ครั้ง โดยส่งรายการออบเจ็กต์ ExecutionResult เป็นอาร์กิวเมนต์สำหรับการประมวลผลเพิ่มเติม การใช้งานทั่วไปแสดงอยู่ ที่นี่

function main() {   const accountSelector = AdsManagerApp.accounts()       .withLimit(50)       .withCondition('customer_client.currency_code = "USD"');    accountSelector.executeInParallel("processClientAccount", "afterProcessAllClientAccounts"); }  function processClientAccount() {   const clientAccount = AdsApp.currentAccount();    // Process your client account here.   ...    // optionally, return a result, as text.   return ""; }  function afterProcessAllClientAccounts(results) {   for (const result of results) {     // Process the result further     ...   } } 

ฟังก์ชันที่ระบุโดย functionName สามารถรับอาร์กิวเมนต์สตริง (optionalInput) ได้โดยไม่บังคับ พารามิเตอร์นี้ใช้เพื่อส่งพารามิเตอร์เพิ่มเติมไปยังเมธอดแบบขนานทั้งหมดที่เรียกใช้โดย executeInParallel ได้

function main() {   const accountSelector = AdsManagerApp.accounts().withIds([1234567890, 3456787890]);   const sharedParameter = "INSERT_SHARED_PARAMETER_HERE";   accountSelector.executeInParallel("processClientAccount", null, sharedParameter); }  function processClientAccount(sharedParameter) {   // Process your client account here.   ... } 

หากต้องการส่งออบเจ็กต์การกำหนดค่า JavaScript ที่มีการตั้งค่าเฉพาะบัญชี คุณสามารถแปลงเป็นสตริงก่อนได้โดยใช้เมธอด JSON.stringify

function main() {   ...   const accountFlags = {     '1234567890': {        'label': 'Brand 1 campaigns',      },     '3456787890': {        'label': 'Brand 2 campaigns',      }   };   accountSelector.executeInParallel("processClientAccount", null,       JSON.stringify(accountFlags));   ... }  function processClientAccount(sharedParameter) {   const accountFlags = JSON.parse(sharedParameter);   // Process your client account here.   ... } 

ฟังก์ชันที่ระบุโดย functionName ยังแสดงผลสตริงแทนออบเจ็กต์ผ่าน JSON.stringify ได้ด้วย

function processClientAccount() {   ...   const jsonObj = {value: 10, list: [1,2,3,4,5,6], name: "Joe Smith"};   return JSON.stringify(jsonObj); } 

ระบบจะส่งค่าที่แสดงผลไปยังฟังก์ชันเรียกกลับในรายการออบเจ็กต์ ExecutionResult หากแสดงสตริง JSON จากฟังก์ชัน คุณจะแปลงกลับเป็นออบเจ็กต์ JavaScript ได้โดยใช้เมธอด JSON.parse

function callbackFunctionName(results) {   for (var i = 0; i < results.length; i++) {     var resultObj = JSON.parse(results[i].getReturnValue());   } } 

เมธอด executeInParallel จะทำงานกับ accounts สูงสุด 50 รายการ ดังนั้นคุณจะต้องใช้ข้อจำกัดของคุณเองเพื่อจำกัดจำนวนบัญชี ที่สคริปต์ดึงข้อมูล คุณสามารถใช้เมธอด withLimit หรือ withIds ของคลาส ManagedAccountSelector เพื่อจํากัดจํานวนบัญชีที่สคริปต์ดึงข้อมูล

ขีดจำกัดเวลาดำเนินการ

ดูรายละเอียดเกี่ยวกับขีดจํากัดเวลาในการเรียกใช้สคริปต์ของ Ads Manager ได้ที่เอกสารประกอบเกี่ยวกับขีดจํากัด