How to remove negative keywords from list before adding new?

133 views
Skip to first unread message

Sigurd

unread,
Nov 28, 2019, 1:00:14 PM11/28/19
to Google Ads Scripts Forum
Hi, 

How can I remove existing negative keywords from a negative keyword list before adding new? 

It seems I can only add new keywords to a list.

My code

 var ss = SpreadsheetApp.openByUrl(url)
 var sheet = ss.getSheetByName(sheet);
 var data = sheet.getDataRange().getValues();
 var negativeKeywords = data.filter(String)
      
 var listIter = AdsApp.negativeKeywordLists()
 .withCondition('Name = "' + listName + '"')
 .get();
      while (listIter.hasNext()) {        
        var list = listIter.next();

        // Here I would like to clear the list
        
         for (i=0;i<negativeKeywords.length;i++) {
        list.addNegativeKeywords(negativeKeywords[i])
      }
      }

I guess I could delete it and build it from scratch but there must be a better way


Thanks

/sigurd

Google Ads Scripts Forum Advisor

unread,
Nov 29, 2019, 1:37:52 AM11/29/19
to adwords...@googlegroups.com
Hi Sigurd,

Thank you for reaching out.

For the negative keywords that belongs to a negative keyword list, you can use the AdsApp.​SharedNegativeKeyword instead for removing this entity and the specific method that you can use is remove(). You may refer to this sample code to have an idea on how to implement it on Google Ads scripts.

Furthermore, for your convenience, we have a readily available script (Master Negative List solution script) that you can use. It reads negative criteria from a Google Spreadsheet and creates a shared negative criteria list in the Google Ads account and syncs (remove or add shared negative keywords) the list with the criteria from the spreadsheet. Please read the provided link to read more about this solution script to determine if it meets your requirement.

Let me know if you have further questions.

Regards,
Ejay
Google Ads Scripts Team

ref:_00D1U1174p._5001UODTcJ:ref

Sigurd

unread,
Nov 29, 2019, 6:22:01 AM11/29/19
to Google Ads Scripts Forum
Thanks Ejay. It now works beautifully :)


Here's an updated version of my code

var ss = SpreadsheetApp.openByUrl(url)
 var sheet = ss.getSheetByName(sheet);
 var data = sheet.getDataRange().getValues();
 var negativeKeywords = data.filter(String)
      
 var listIter = AdsApp.negativeKeywordLists()
 .withCondition('Name = "' + listName + '"')
 .get();
    if (listIter.totalNumEntities() == 1) {
       var list = listIter.next();
       var existingKeywordsIter = list.negativeKeywords().get();
       var existingKeywords = [];
      while (existingKeywordsIter.hasNext()) {
         existingKeywords.push(existingKeywordsIter.next());
       }
          for (a=0;a<existingKeywords.length;a++) {
            existingKeywords[a].remove();        
       }        
          for (b=0;b<negativeKeywords.length;b++) {
            list.addNegativeKeywords(negativeKeywords[b])
      }
    }

Google Ads Scripts Forum Advisor

unread,
Dec 1, 2019, 8:32:59 PM12/1/19
to adwords...@googlegroups.com
Hi Sigurd,

Thanks for letting me know the status of your script. I am happy also that you script is now working.

Feel free to post your concerns to this forum and our team will be glad to assist you.
Reply all
Reply to author
Forward
0 new messages