Construct a new negative keyword list and add it to a campaign
function addNegativeKeywordListToCampaign() {
var NEGATIVE_KEYWORD_LIST_NAME = 'INSERT_LIST_NAME_HERE';
var CAMPAIGN_NAME = 'INSERT_CAMPAIGN_NAME_HERE';
var negativeKeywordListOperator =
AdsApp.newNegativeKeywordListBuilder()
.withName(NEGATIVE_KEYWORD_LIST_NAME)
.build();
if (negativeKeywordListOperator.isSuccessful()) {
var negativeKeywordList = negativeKeywordListOperator.getResult();
negativeKeywordList.addNegativeKeywords([
'broad match keyword',
'"phrase match keyword"',
'[exact match keyword]'
]);
var campaign = AdsApp.campaigns()
.withCondition('Name = "' + CAMPAIGN_NAME + '"')
.get();
campaign.addNegativeKeywordList(negativeKeywordList);
} else {
Logger.log('Could not add Negative Keyword List.');
}
}
Remove all the shared negative keywords in an negative keyword list
function removeAllNegativeKeywordsFromList() {
var NEGATIVE_KEYWORD_LIST_NAME = 'INSERT_LIST_NAME_HERE';
var negativeKeywordListIterator =
AdsApp.negativeKeywordLists()
.withCondition('Name = "' + NEGATIVE_KEYWORD_LIST_NAME + '"')
.get();
if (negativeKeywordListIterator.totalNumEntities() == 1) {
var negativeKeywordList = negativeKeywordListIterator.next();
var sharedNegativeKeywordIterator =
negativeKeywordList.negativeKeywords().get();
var sharedNegativeKeywords = [];
while (sharedNegativeKeywordIterator.hasNext()) {
sharedNegativeKeywords.push(sharedNegativeKeywordIterator.next());
}
for (var i = 0; i < sharedNegativeKeywords.length; i++) {
sharedNegativeKeywords[i].remove();
}
}
}