Skip to content

Instantly share code, notes, and snippets.

@ddaws
Last active October 16, 2023 06:37
Show Gist options
  • Save ddaws/c163b15bef37fc0eae3433d2124b5868 to your computer and use it in GitHub Desktop.
Save ddaws/c163b15bef37fc0eae3433d2124b5868 to your computer and use it in GitHub Desktop.
Executes AdWords scripts across 50+ accounts in parallel and sequentially
// the script will run across all accounts with exactly this label. If you want the script to run
// accross all accounts just remove this all together
var SCRIPT_LABEL = 'Your Script!';
function run() {
// your script goes here
}
// this will execute your script sequentially accounts and is only used for accounts in excess of 50
function executeInSequence(sequentialIds, executeSequentiallyFunc) {
sequentialIds.forEach(function (accountId) {
var account = MccApp.accounts().withIds([accountId]).get().next();
MccApp.select(account);
executeSequentiallyFunc();
});
}
// out custom main function responsible for executing the run function
function main() {
try {
var accountSelector = MccApp.accounts().orderBy('Name');
if (SCRIPT_LABEL) {
accountSelector = accountSelector.withCondition("LabelNames CONTAINS '" + SCRIPT_LABEL + "'");
}
var accountIterator = accountSelector.get();
var accountIds = [];
while (accountIterator.hasNext()) {
var account = accountIterator.next();
accountIds.push(account.getCustomerId());
}
var parallelIds = accountIds.slice(0, 50);
var sequentialIds = accountIds.slice(50);
MccApp.accounts()
.withIds(parallelIds)
.executeInParallel('run');
if (sequentialIds.length > 0) {
executeInSequence(sequentialIds, run);
}
}
catch (exception) {
Logger.log('Running on non-MCC account.');
run();
}
}
@k1rushqa
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment