สคริปต์ 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 ตรงกัน เมื่อประมวลผลบัญชีทั้งหมดแล้ว ฟังก์ชัน Callback ซึ่งถ้า ระบุโดย 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 คลาสเพื่อจำกัดจำนวนบัญชีที่สคริปต์ดึงข้อมูล

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

ดูรายละเอียดเกี่ยวกับขีดจำกัดเวลาดำเนินการของ Google Ads Scripts ได้ในเอกสารประกอบเกี่ยวกับขีดจำกัด