Ads Manager Scripts

TheAdsManagerAppclass in Google Ads scripts enables you to manage accounts linked under yourManager Account. You can manageall your advertiser accounts through a single script instead of creating aseparate script for each account.

Retrieve list of accounts

You can retrieve accounts under a manager account using theaccountsmethod, for example:

constaccountSelector=AdsManagerApp.accounts().withCondition('customer_client.descriptive_name = "My Account"');constaccountIterator=accountSelector.get();

There are some restrictions to the accounts that can be retrieved:

  • Manager accounts cannot be retrieved if you have a multi-levelhierarchy. Only the client accounts can be selected.
  • By default, closed, canceled, and suspended accounts are not returned. Youcan override this behavior by callingwithCondition specifying a differentfilter forcustomer_client.status.

Theaccounts call retrieves the list of all client accounts under themanager account hierarchy by default. You can use thewithLimitmethod of theManagedAccountSelectorclass to restrict the number of accounts that your script retrieves. Anotheroption is to select the accounts by their customer IDs using thewithIdsmethod:

// Hyphens in the account ID are optional.constaccountSelector=AdsManagerApp.accounts().withIds(['123-456-7890','234-567-8901','345-678-9012']);

Work on client accounts

Once you've retrieved the client accounts, you can iterate through them usingthe iterator'shasNextandnextmethods. You need to use theselectmethod to switch the execution context to a client account. After you select aclient account, any further API calls apply to the client account until youexplicitly select another account:

// Keep track of the manager account for future reference.constmanagerAccount=AdsApp.currentAccount();// Select your accountsconstaccountIterator=AdsManagerApp.accounts()// ... Write some logic here to select the accounts you want using// withCondition or withIds// Iterate through the list of accountsfor(constaccountofaccountIterator){// Select the client account.AdsManagerApp.select(account);// Select Search and Display campaigns under the client accountconstcampaignIterator=AdsApp.campaigns().get();// Operate on client account...}

Work on accounts in parallel

Google Ads scripts lets you operate on multiple client accounts in parallel, using theexecuteInParallelmethod of theManagedAccountSelectorclass. TheexecuteInParallel method has the following signature:

functionexecuteInParallel(functionName,optionalCallbackFunctionName,optionalInput);

TheexecuteInParallel method executes a function specified byfunctionNameon eachManagedAccountthat theManagedAccountSelectormatches. Once all accounts have been processed, the callback function, ifspecified byoptionalCallbackFunctionName, is executed once, passing a listofExecutionResultobjects as its argument for any further processing. The typical usage is shownbelow:

functionmain(){constaccountSelector=AdsManagerApp.accounts().withLimit(50).withCondition('customer_client.currency_code = "USD"');accountSelector.executeInParallel("processClientAccount","afterProcessAllClientAccounts");}functionprocessClientAccount(){constclientAccount=AdsApp.currentAccount();//Processyourclientaccounthere....//optionally,returnaresult,astext.return"";}functionafterProcessAllClientAccounts(results){for(constresultofresults){//Processtheresultfurther...}}

The function specified byfunctionName can optionally accept a stringargument (optionalInput). This parameter can be used to pass an additionalparameter to all parallel methods called byexecuteInParallel:

functionmain(){constaccountSelector=AdsManagerApp.accounts().withIds([1234567890,3456787890]);constsharedParameter="INSERT_SHARED_PARAMETER_HERE";accountSelector.executeInParallel("processClientAccount",null,sharedParameter);}functionprocessClientAccount(sharedParameter){// Process your client account here....}

If you want to pass a JavaScript configuration object that containsaccount-specific settings, you could first convert it into a string using theJSON.stringifymethod:

functionmain(){...constaccountFlags={'1234567890':{'label':'Brand 1 campaigns',},'3456787890':{'label':'Brand 2 campaigns',}};accountSelector.executeInParallel("processClientAccount",null,JSON.stringify(accountFlags));...}functionprocessClientAccount(sharedParameter){constaccountFlags=JSON.parse(sharedParameter);// Process your client account here....}

The function specified byfunctionName can also return a string instead of anobject throughJSON.stringify:

functionprocessClientAccount(){...constjsonObj={value:10,list:[1,2,3,4,5,6],name:"Joe Smith"};returnJSON.stringify(jsonObj);}

The returned values are passed into the callback function in a list ofExecutionResultobjects. If you returned a JSON string from the function, you could convert itback into a JavaScript object usingJSON.parsemethod:

functioncallbackFunctionName(results){for(vari=0;i <results.length;i++){varresultObj=JSON.parse(results[i].getReturnValue());}}

TheexecuteInParallelmethod operates on a maximum of 50accounts,so you'll have to implement your own restrictions to limit the number ofaccounts that your script retrieves. You can use thewithLimitorwithIdsmethod ofManagedAccountSelectorclass to restrict the number of accounts that your script retrieves.

Execution time limits

Seethis page fordetails on Ads Manager scripts execution time limits.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-14 UTC.