Google 広告スクリプトの
AdsManagerApp
クラスを使用すると、
MCC アカウントにリンクされているアカウントを管理できます。アカウントごとに個別のスクリプトを作成するのではなく、1 つのスクリプトで広告主様のアカウントをすべて管理できます。
アカウントのリストを取得する
MCC アカウントに属するアカウントは、
accounts
メソッドを使用して取得できます。例:
const accountSelector = AdsManagerApp.accounts()
.withCondition('customer_client.descriptive_name = "My Account"');
const accountIterator = accountSelector.get();
取得できるアカウントにはいくつかの制限があります。
- 階層が複数レベルになっている場合、MCC アカウントは取得できません。選択できるのはクライアント アカウントのみです。
- デフォルトでは、閉鎖、キャンセル、停止されたアカウントは返されません。この動作をオーバーライドするには、
customer_client.statusに別のフィルタを指定してwithConditionを呼び出します。
accounts 呼び出しでは、デフォルトで MCC アカウント階層に属するすべてのクライアント アカウントのリストが取得されます。
ManagedAccountSelector
クラスの
withLimit
メソッドを使用すると、スクリプトが取得するアカウントの数を制限できます。また、
`withIds`
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 広告スクリプトでは、
executeInParallel
メソッドを使用して、複数のクライアント アカウントを並行して操作できます。ManagedAccountSelectorexecuteInParallel メソッドのシグネチャは次のとおりです。
function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput);
executeInParallel メソッドは、functionName
が一致する各
ManagedAccountSelector
ManagedAccount
で
によって指定された関数を実行します。すべてのアカウントが処理されると、コールバック関数(
optionalCallbackFunctionNameで指定されている場合)が 1 回実行され、以降の処理のために
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());
}
}
The
executeInParallel
メソッドは最大 50
accountsで動作するため、
スクリプトが取得するアカウントの数を制限するには、独自の制限を実装する必要があります。スクリプトが取得するアカウントの数を制限するには、
withLimit
または
withIds
クラスの
ManagedAccountSelector
を使用します。
実行時間の制限
広告管理スクリプトの実行時間の制限について詳しくは、制限事項のドキュメント をご覧ください。