Skip to content

Instantly share code, notes, and snippets.

@sshcli
Forked from ddaws/excludePlacements.js
Created August 27, 2020 23:21
Show Gist options
  • Save sshcli/11a870df8ebdc724c41777dc4d204a74 to your computer and use it in GitHub Desktop.
Save sshcli/11a870df8ebdc724c41777dc4d204a74 to your computer and use it in GitHub Desktop.
AdWords Negative Match Placements Term
/**
* Removes placements ending in the tlds xyz, tk, and download.
* @author Dawson R
* @author Andrew B
*/
// Top Level Domains to exclude
var TLDs = '.xyz, .tk, .download';
// -------------------------------------------------------
function removePlacementByDomain (domain) {
var placementSelector = AdWordsApp.display().placements()
.withCondition("PlacementUrl CONTAINS '" + domain + "'")
.withCondition("CampaignStatus != REMOVED");
var placementIterator = placementSelector.get();
while (placementIterator.hasNext()) {
var placement = placementIterator.next();
var placementUrl = placement.getUrl();
//Logger.log(placementUrl);
var campaign = placement.getCampaign();
var excludeOperation = campaign.display().newPlacementBuilder().withUrl(placementUrl).exclude();
if (!excludeOperation.isSuccessful()) {
Logger.log("Could not exclude : " + placementUrl);
}
}
}
function run () {
TLDs.split(',').map(function (tld) {
return tld.trim();
}).forEach(function (domain) {
removePlacementByDomain(domain);
});
}
function executeInSequence (sequentialIds, executeSequentiallyFunc) {
Logger.log('Executing in sequence : ' + sequentialIds);
sequentialIds.forEach(function (accountId) {
var account = MccApp.accounts().withIds([accountId]).get().next();
MccApp.select(account);
executeSequentiallyFunc();
});
}
function main () {
try {
var accountIterator = MccApp.accounts().orderBy('Name').get();
Logger.log('Total number of accounts under MCC : ' + accountIterator.totalNumEntities());
var accountIds = [];
while (accountIterator.hasNext()) {
var account = accountIterator.next();
accountIds.push(account.getCustomerId());
}
var parallelIds = accountIds.slice(0, 50);
var sequentialIds = accountIds.slice(50);
// execute accross accounts
MccApp.accounts()
.withIds(parallelIds)
.executeInParallel('run');
if (sequentialIds.length > 0) {
executeInSequence(sequentialIds, run);
}
} catch (exception) {
// not an Mcc
Logger.log('Running on non-MCC account.');
run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment