Skip to content

Instantly share code, notes, and snippets.

@ddaws
Last active August 27, 2020 23:21
Show Gist options
  • Save ddaws/db363d6a5966c1c8080388972298862a to your computer and use it in GitHub Desktop.
Save ddaws/db363d6a5966c1c8080388972298862a 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();
}
}
@sshcli
Copy link

sshcli commented Aug 27, 2020

@ddaws can you add the extra lines to automatically create the placement exclusion list?

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