Script to pause keywords

269 views
Skip to first unread message

Carl Konopka

unread,
Jun 2, 2021, 12:15:44 PM6/2/21
to Google Ads Scripts Forum
I found the following script and changed the name to main, thinking it would enable me to pause keywords.  However, it is not doing anything.  I have a list of keywords I would like to pause.  I'm not sure of a withCondition to use that would pick from a list.  Would this work in Java "Text IN [ 'best movies','keyword2']"?

Thanks,



function main() {
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "Test"')
      .get();
  //Logger.log(adGroupIterator);
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    var keywordIterator = adGroup.keywords()
        .withCondition('Text="best movies"').get();
    //Logger.log(keywordIterator);
    while (keywordIterator.hasNext()) {
      var keyword = keywordIterator.next();
      Logger.log(keyword);
      keyword.pause();
    }
  }
}

Sigurd Fabrin

unread,
Jun 2, 2021, 1:19:28 PM6/2/21
to Google Ads Scripts Forum
You can skip the adGroup iterator and go straight to AdsApp.keywords()

Smth like this:

function main() {
  var keywords = ['kw1', 'kw2', 'kw3', 'etc'];
  var kwIter = AdsApp.keywords()
  .withCondition('Text IN ["' + keywords.join('","') + '"]')
  .get()
  while (kwIter.hasNext()) {
  var kw = kwIter.next()
    kw.pause()
  } 
}

 sigurd

Google Ads Scripts Forum Advisor

unread,
Jun 2, 2021, 10:54:48 PM6/2/21
to adwords...@googlegroups.com
Hi Carl,

Harry here, from the Google Ads Scripts Team. Allow me to provide guidance as well.

The script you have referenced will look at keywords at the AdGroup level in which the specific Ad Group is named 'Test' ('Name = "Test"'). From there, it would retrieve and pause all keywords that are named 'best movies' ('Text="best movies"'). If it did not result into anything, then it would simply mean that there were no keywords found based on the conditions (using the withCondition method).

To answer your question, this condition will work "Text IN [ 'best movies','keyword2']" so you may simply update the KeywordIterator in your script with it. You may also opt to retrieve entities at the Keyword level by using Sigurd's script example. Please see also Keywords and Ad Groups for your reference. 

@Sigurd: As always, thanks for your insights.

Let me know if there's anything else I can assist you with.

Thanks,
Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2Hzeol:ref

Carl Konopka

unread,
Jun 3, 2021, 7:16:51 AM6/3/21
to Google Ads Scripts Forum
Thanks so much for both of your feedback.  To give some background and context:

  • My ads account is currently suspended and I'm trying to get back into compliance by pausing keywords I was provided that are too general.  So, I have a list provided by Google and would expect to find them
  • My mistake on my original code.  I was thinking I was picking campaign as "Test" not AdGroup.
  • I tried Sigurd's code with my list of keywords, but still it did not make any changes
  • What would be the syntax for finding all the keywords that contain 'best movies', so it would also find '2021 best movies"?
  • I have run ad scripts in the past, but not recently.  I was poking around and noticed another script asked me to authorize it, but when I attempted to authorize it did not work.  When I have run both of these scripts they have never asked me to authorize them.
Carl

Sigurd Fabrin

unread,
Jun 3, 2021, 8:40:16 AM6/3/21
to Google Ads Scripts Forum
The IN operator from my code snippet above only matches keywords that are exactly the same as the text you provide.

This code will pause all keywords that contain some text

function main() { 
  var kwIter = AdsApp.keywords()
  .withCondition('Text CONTAINS_IGNORE_CASE "keyword text')
  .get()
  while (kwIter.hasNext()) {
  var kw = kwIter.next()
kw.pause()
  } 
}

Btw: you can not run scripts without authorisation

Carl Konopka

unread,
Jun 3, 2021, 10:47:37 AM6/3/21
to Google Ads Scripts Forum
Sigurd,

Thanks for the information.

Is there a way to use both CONTAINS_IGNORE_CASE and IN at the same time?    I want to both use a list (IN) and use CONTAINS_IGNORE_CASE.

Thanks

Message has been deleted

Sigurd Fabrin

unread,
Jun 3, 2021, 2:00:38 PM6/3/21
to Google Ads Scripts Forum
Sure, just filter with regex, then you can do pretty much whatever - see https://www.w3schools.com/jsref/jsref_obj_regexp.asp & https://regex101.com/

function main() { 
  var regexPattern = /(^chelsea shirt$|^arsenal|liverpool)/gi;
  // pattern above will match: exactly chelsea shirt OR everything that starts with arsenal OR everything that contains liverpool - case insensitive
  var kwIter = AdsApp.keywords()
  .get()
  while (kwIter.hasNext()) {
  var kw = kwIter.next()
  if (kw.getText().match(regexPattern)) {
kw.pause()
  }
 }
}

If it's a large account, consider limit to active keywords from active campaigns and active adGroups

Google Ads Scripts Forum Advisor

unread,
Jun 4, 2021, 1:01:51 AM6/4/21
to adwords...@googlegroups.com
Hi Sigurd,

Thank you for providing your solutions. It is very much appreciated.

@Carl: I can confirm that Sigurd's solution will work for your use case. For context on authorizing scripts, Authorization is done upon a script is created or when you are enabling a disabled one. Please see Authorization guide for more information.

Let us know if you would need further assistance.
Reply all
Reply to author
Forward
0 new messages