สคริปต์ 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 จะดึงข้อมูลรายการบัญชีลูกค้าทั้งหมดที่อยู่ภายใต้ลําดับชั้นบัญชีดูแลจัดการโดยค่าเริ่มต้น คุณสามารถใช้วิธีของคลาส ManagedAccountSelector เพื่อจํากัดจํานวนบัญชีที่สคริปต์ดึงข้อมูลwithLimit อีกตัวเลือกหนึ่งคือเลือกบัญชีตามรหัสลูกค้าโดยใช้วิธี 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 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);
}

ระบบจะส่งค่าที่แสดงผลไปยังฟังก์ชัน Callback ในรายการออบเจ็กต์ 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

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

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