借助 Google Ads 脚本中的 AdsManagerApp 类,您可以管理与您的经理账号相关联的账号。您可以通过单个脚本管理所有广告客户账号,而无需为每个账号创建单独的脚本。
检索账号列表
您可以使用 accounts 方法检索经理账号下的账号,例如:
const accountSelector = AdsManagerApp.accounts()
.withCondition('customer_client.descriptive_name = "My Account"');
const accountIterator = accountSelector.get();
可检索的账号存在一些限制:
- 如果您有多级层次结构,则无法检索经理账号。只能选择客户账号。
- 默认情况下,系统不会返回已关闭、已撤销和已中止的账号。您可以通过调用
withCondition并为customer_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']);
处理客户账号
检索到客户账号后,您可以使用迭代器的 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 脚本,您可以使用 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 类的 withLimit 或 withIds 方法来限制脚本检索的账号数量。
执行时间限制
如需详细了解 Ads Manager 脚本执行时间限制,请参阅限制文档。