Skip to content

Instantly share code, notes, and snippets.

@williamhrs
Created October 13, 2016 18:00
Show Gist options
  • Select an option

  • Save williamhrs/7919e42ead8eca82badd04e9199ac395 to your computer and use it in GitHub Desktop.

Select an option

Save williamhrs/7919e42ead8eca82badd04e9199ac395 to your computer and use it in GitHub Desktop.
Adwords Script to remove BAD TLDS
/**
* Removes placements ending in the tlds xyz, tk, and download.
* @author Dawson Reid
* @author Andrew Breen
*/
// 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();
}
}
@LRENZ
Copy link

LRENZ commented Oct 11, 2017

hi, I am testing this script, figure out a problem,that is ,when run this script ,it will contain some different url. For example: TLDS = '.in' , but it will also return the domian just like xxx.info, because AWQL use CANTAIN to search the placement URL. So, Is it any possible to use some method like ENDSWITH to return Placement URL? thanks for your help

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