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.
...
}
通过 JSON.stringify
,functionName
指定的函数也可以返回字符串而不是对象:
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
方法来限制脚本检索的账号数量。
执行时间限制
如需详细了解广告管理工具脚本的执行时间限制,请参阅限制文档。