Google 広告スクリプトの AdsManagerApp
クラスを使用すると、MCC アカウントにリンクされているアカウントを管理できます。アカウントごとに個別のスクリプトを作成する代わりに、1 つのスクリプトですべての広告主アカウントを管理できます。
アカウントのリストを取得する
マネージャー アカウントの下にあるアカウントを取得するには、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
メソッドを使用してお客様 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 campaigns under the client account
const campaignIterator = AdsApp.campaigns().get();
// Operate on client account
...
}
アカウントを並行して操作する
Google 広告スクリプトでは、ManagedAccountSelector
クラスの executeInParallel
メソッドを使用して、複数のクライアント アカウントを並行して操作できます。executeInParallel
メソッドのシグネチャは次のとおりです。
function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput);
executeInParallel
メソッドは、ManagedAccountSelector
が一致する各 ManagedAccount
に対して、functionName
で指定された関数を実行します。すべてのアカウントが処理されると、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 文字列を返した場合は、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
メソッドを使用して、スクリプトが取得するアカウントの数を制限できます。
実行時間の制限
アド マネージャー スクリプトの実行時間の上限について詳しくは、こちらのページをご覧ください。