how do I check if a keyword already exists before building it

212 views
Skip to first unread message

Aleksandar V

unread,
Nov 16, 2017, 10:15:00 AM11/16/17
to AdWords Scripts Forum
I created a script that switches all broad match keywords and malformed modified broad match keywords into proper modified broad match keywords: https://gist.github.com/alewolf/e6fcecfba548c765a4ad52d46abcd81e

There is one issue though. I can't find a way to avoid building new keywords if a correct one already exists. Fortunately the original keyword doesn't get overwritten. But when I try to apply a label in a subsequent function I get errors.


Example:

We have an old keyword '+example' with a label applied from an earlier run.

We process a malformed keyword '++example' which results in the same as the old one '+example'. Unfortunately I can't omit building '+example' again because the old one is not part of my selection. For performance reasons I've excluded all old keywords that already have been processed, so I don't have an array to check against. 

Interestingly nothing happens when i build the new keyword '+example' again. No error. And the old keyword '+example' doesn't get overwritten (which is good, we don't lose stats). 

But when my function then tries to apply the label to the newly created keyword I get an error, as the script reports that the label already exists for that keyword. 

The only solution I could think of to work around this is the following, but it seems very inefficient. 

Each time I clean a new keyword I run a keywordSelector to find out if that new keyword already exists in the same ad group. 


var keywordSelector = AdWordsApp
     
.keywords()
     
.withCondition("Text = '+example'");

 
var keywordIterator = keywordSelector.get();
 
if (keywordIterator.hasNext()) {
   
// we now know that this keyword already exists and act accordingly

 
}

When we know that the keyword already exists, we abort and continue with the next one. 

Again, this feels very inefficient when we have several hundred new keywords to process. Is there a better way?

Anthony Madrigal

unread,
Nov 16, 2017, 3:32:27 PM11/16/17
to AdWords Scripts Forum
Hi,

Unfortunately, it appears the only way I could think of doing this would be using keywordIterator.hasNext that you are using. You can take a look at some of our Best Practices for some ideas to optimize your code.

Regards,
Anthony
AdWords Scripts Team
Message has been deleted

Aleksandar V

unread,
Nov 17, 2017, 5:30:45 AM11/17/17
to AdWords Scripts Forum

Thanks !
Reply all
Reply to author
Forward
0 new messages