Google 広告スクリプトの AdsManagerApp
クラスを使用すると、MCC アカウントにリンクされているアカウントを管理できます。アカウントごとに個別のスクリプトを作成する代わりに、1 つのスクリプトですべての広告主アカウントを管理することができます。
アカウントのリストを取得する
accounts
メソッドを使用して、MCC アカウントの下位のアカウントを取得できます。次に例を示します。
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
メソッドを使用して JSON 文字列を 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
メソッドを使用して、スクリプトが取得するアカウントの数を制限できます。
実行時間の制限
Google アド マネージャー スクリプトの実行時間の上限について詳しくは、こちらのページをご覧ください。