Negative Keyword Lists
Stay organized with collections
Save and categorize content based on your preferences.
Get a negative keyword list by name
function getNegativeKeywordList(name) {
const negativeKeywordLists = AdsApp.negativeKeywordLists()
.withCondition(`shared_set.name = "${name}"`)
.get();
if (!negativeKeywordLists.hasNext()) {
throw new Error(`Cannot find negative keyword list with name "${name}"`);
}
return negativeKeywordLists.next();
}
Construct a new negative keyword list and add it to a campaign
function addNegativeKeywordListToCampaign(campaignName, negativeKeywordListName) {
const negativeKeywordLists = AdsApp.negativeKeywordLists()
.withCondition(`shared_set.name = "${negativeKeywordListName}"`)
.get();
if (!negativeKeywordLists.hasNext()) {
throw new Error(`Cannot find negative keyword list with name "${negativeKeywordListName}"`);
}
const negativeKeywordList = negativeKeywordLists.next();
const campaigns = AdsApp.campaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
if (!campaigns.hasNext()) {
throw new Error(`Cannot find campaign with the name "${campaignName}"`);
}
const campaign = campaigns.next();
campaign.addNegativeKeywordList(negativeKeywordList);
}
Remove all the shared negative keywords in an negative keyword list
function removeAllNegativeKeywordsFromList(name) {
const negativeKeywordLists = AdsApp.negativeKeywordLists()
.withCondition(`shared_set.name = "${name}"`)
.get();
if (!negativeKeywordLists.hasNext()) {
throw new Error(`Cannot find negative keyword list with name "${name}"`);
}
const negativeKeywordList = negativeKeywordLists.next();
for (const negativeKeyword of negativeKeywordList.negativeKeywords()) {
negativeKeyword.remove();
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-03-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-03-14 UTC."],[],[]]