Ads Manager 脚本

借助 Google Ads 脚本中的 AdsManagerApp 类,您可以管理在 经理账号下关联的账号。您可以通过单个脚本管理所有广告客户账号,而无需为每个账号创建单独的脚本。

检索账号列表

您可以使用 accounts 方法检索经理账号下的账号,例如:

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

const accountIterator = accountSelector.get();

可以检索的账号有一些限制:

  • 如果您有多级层次结构,则无法检索经理账号。只能选择客户账号。
  • 默认情况下,系统不会返回已关闭、已取消和已暂停的账号。您可以通过调用 withCondition 并为 customer_client.status 指定不同的过滤条件来替换此行为。

默认情况下,accounts 调用会检索经理账号层次结构下的所有客户账号的列表。您可以使用 withLimit 方法来限制脚本检索的账号数量。ManagedAccountSelector另一种 方法是使用 withIds 方法按客户 ID 选择账号:

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

使用客户账号

检索到客户账号后,您可以使用 迭代器的 hasNextnext 方法遍历这些账号。您需要使用 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 方法并行处理多个客户账号。ManagedAccountSelectorexecuteInParallel 方法具有以下签名:

function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput);

executeInParallel 方法会在 functionName ManagedAccountSelector 匹配的每个 ManagedAccount 上执行由 指定的函数。处理完所有账号后,如果 optionalCallbackFunctionName指定了回调函数,则该函数会执行一次,并将列表 的 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 方法最多可处理 50 个 accounts, 因此您必须自行实现限制,以限制脚本检索的账号数量。您可以使用 withLimitwithIds 方法来限制脚本检索的账号数量。ManagedAccountSelector

执行时间限制

如需详细了解 Ads Manager 脚本执行时间限制,请参阅限制文档