Ads Manager 脚本

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

检索账号列表

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

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

const accountIterator = accountSelector.get();

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

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

默认情况下,accounts 调用会检索经理账号层次结构下的所有客户账号列表。您可以使用 ManagedAccountSelector 类的 withLimit 方法限制脚本检索到的账号数量。另一种方法是使用 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 campaigns under the client account
  const campaignIterator = AdsApp.campaigns().get();

  // Operate on client account
  ...
}

并行处理账号

借助 Google Ads 脚本,您可以使用 ManagedAccountSelector 类的 executeInParallel 方法并行操作多个客户账号。executeInParallel 方法具有以下签名:

function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput);

executeInParallel 方法会对 ManagedAccountSelector 匹配的每个 ManagedAccount 执行 functionName 指定的函数。处理完所有账号后,系统会执行回调函数(如果由 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 字符串,则可以使用 JSON.parse 方法将其转换回 JavaScript 对象:

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

executeInParallel 方法最多可处理 50 个 accounts,因此您必须实现自己的限制,以限制脚本检索的账号数量。您可以使用 ManagedAccountSelector 类的 withLimitwithIds 方法来限制脚本检索的账号数量。

执行时间限制

如需详细了解 Google Ads 经理脚本的执行时间限制,请参阅此页面