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 方法来限制脚本检索的帐号数量。

执行时间限制

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