AI-generated Key Takeaways
-
These Google Ads scripts provide functions to manage negative keyword lists, including retrieving a list by name, adding a list to a campaign, and removing all negative keywords from a list.
-
The
getNegativeKeywordList
function retrieves an existing negative keyword list using its name and throws an error if not found. -
The
addNegativeKeywordListToCampaign
function associates an existing negative keyword list with a specific campaign, also throwing an error if either the list or campaign is not found. -
The
removeAllNegativeKeywordsFromList
function removes all negative keywords within a specified negative keyword list, throwing an error if the list is not found.
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(); } }