Google 広告スクリプトの AdsManagerApp クラスを使用すると、クライアント センター(MCC)アカウントにリンクされているアカウントを管理できます。アカウントごとに個別のスクリプトを作成するのではなく、1 つのスクリプトですべての広告主アカウントを管理できます。
アカウントのリストを取得する
MCC アカウントの配下にあるアカウントを取得するには、accounts メソッドを使用します。たとえば、次のようになります。
const accountSelector = AdsManagerApp.accounts()
.withCondition('customer_client.descriptive_name = "My Account"');
const accountIterator = accountSelector.get();
復元できるアカウントにはいくつかの制限があります。
- 階層が複数レベルになっている場合、クライアント センター(MCC)アカウントを取得することはできません。選択できるのはクライアント アカウントのみです。
- デフォルトでは、閉鎖、キャンセル、停止されたアカウントは返されません。この動作をオーバーライドするには、
withConditionを呼び出してcustomer_client.statusに別のフィルタを指定します。
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 Search and Display 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 メソッドを使用して、スクリプトが取得するアカウントの数を制限できます。
実行時間の制限
広告管理ツール スクリプトの実行時間制限について詳しくは、制限事項のドキュメントをご覧ください。