A script to pause Keywords in a campaign and enable the same Keywords in another campaign

425 views
Skip to first unread message

Michael Smedegaard

unread,
Mar 3, 2021, 9:16:41 AM3/3/21
to Google Ads Scripts Forum
Hi

I want to create a script that will enable keywords from a particular Google Ads account/campaign and to pause the same keywords from the same account but in af different campaign into MCC level. 

I have found this script which seems to be able to do a bit of the same thing, but not quite:

https://groups.google.com/g/adwords-scripts/c/ccNya8B3P-0/m/Cl7lej5tBAAJ

To give you an example - 
  • Account 1
    • Campaign A
      • AG 1
        • KW1
        • KW2
      • AG 2
        • KW3
        • KW4
    • Campaign B
      • AG 1
        • KW1
        • KW2
      • AG 2
        • KW3
        • KW4
I find keywords in campaign A after performance metrics cliks. Here I would like to pause these keywords but activate them in Campaign B instead.

The structure is the same in Campaign A and Campaign B, so the same keyword will always be in the same campaign / ad group in Campaign A and Campaign B, respectively.

I have already used the Google Sheets Ad-On to pull data in to give me which AGs need to be paused in which accounts. Im just not able to find a script that will help me pause between accounts. 

Any help on this will be much appreciated! 

Sincerely
Mike

Google Ads Scripts Forum Advisor

unread,
Mar 3, 2021, 10:01:02 PM3/3/21
to adwords...@googlegroups.com

Hi Michael,

 

Thanks for reaching out. I am Harry from the Google Ads Scripts Team. Allow me to assist you on this.

 

Implementing a Ads Manager Script should allow you to pause/manage entities between sub accounts of your MCC and since you've mentioned that the keywords would be the same for both of the campaigns (please do correct me if I got this wrong), you may retrieve the ID of the Keyword from Campaign A then, reference the Keyword ID to be paused in Campaign B. Please see Keyword.getID() and Keyword.pause() for your reference.

 

For further assistance, kindly provide your CID and script name so that I would be able to check what you have so far and guide you accordingly. You may send them here or privately via the reply to author option. If this option is not available at your end, you may send it through our email (googleadsscr...@google.com) instead.

 

Thanks,

Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2DKjqk:ref

Google Ads Scripts Forum Advisor

unread,
Mar 4, 2021, 10:58:35 PM3/4/21
to adwords...@googlegroups.com

Hi Michael,

 

Thanks for coming back and providing the details. Please see below a code on how you can retrieve keyword IDs from campaign A then use it to pause keywords in campaign B from another account.
 

// Retrieve Keyword IDs that satisfied metric clicks condition from Campaign A
  var accountIterator = AdsManagerApp.accounts().withIds([123-456-789]).get();
  var keywordsIDs = [];
  
  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
    AdsManagerApp.select(account);
 
    var campaignA = AdsApp.campaigns().withCondition("Name = 'Campaign A'").get().next();
    var keywords = campaignA.keywords().withCondition("Clicks > 5").get();
    
    while(keywords.hasNext()) {
      var keyword = keywords.next();
      keywordsIDs.push(keyword.getId());
    }
  }
  
  // Access campaign B from the other account and then pause keywords using the keywordsIDs retrieved from campaign A
  var accountIterator = AdsManagerApp.accounts().withIds([789-456-123]).get();
   while (accountIterator.hasNext()) {
    var account = accountIterator.next();
    AdsManagerApp.select(account);
 
    var campaignA = AdsApp.campaigns().withCondition("Name = 'Campaign B'").get().next();
    var keywords = campaignA.keywords().withIds(keywordsIDs).get();
    
    while(keywords.hasNext()) {
      var keyword = keywords.next();
      keyword.pause();
    }
  }

 

Let me know how it goes after updating the script for your use case and if there would be any issues.

Michael Smedegaard

unread,
Mar 5, 2021, 8:01:29 AM3/5/21
to Google Ads Scripts Forum

Hi Harry

Thanks for the script. I have a small correction, as it is keywords from Campaign A that must be paused and then activated in Campaign B, but in the same account.

 

I have inserted the following, but get the error (TypeError: Cannot call method "hasNext" of undefined. (file Script.gs, line 12)) Can you help me with this error?

 

// Retrieve Keyword IDs that satisfied metric clicks condition from Campaign A (EXACT)

  var accountIterator = AdsManagerApp.accounts()

  .withIds(['123-456-7890'])

  .get();

  var keywordsIDs = [];

  var Campaign_A = "CampaignName CONTAINS 'Campaign_A'"

  var Campaign_B = "CampaignName CONTAINS 'Campaign_B'"

 

function main() {

 

  while (accountIterator.hasNext()) {

    var account = accountIterator.next();

    AdsManagerApp.select(account);

 

    var campaignA = AdsApp.campaigns()

    .withCondition(Campaign_A).get().next();

  

    var keywords = campaignA.keywords()

    .forDateRange("LAST_MONTH")

    .withCondition("Clicks >= 1")

   // .withCondition("Status = ENABLED")

   // .withCondition("CampaignStatus = ENABLED")

   // .withCondition("AdGroupStatus = ENABLED")

    .withCondition("KeywordMatchType = EXACT")

    .get();

   

    while(keywords.hasNext()) {

      var keyword = keywords.next();

      keywordsIDs.push(keyword.getId());

    }

  }

 

  // Access campaign B from the same account and then pause keywords using the keywordsIDs retrieved from campaign A

  var accountIterator = AdsManagerApp.accounts()

  .withIds(['123-456-7890'])

  .get();

 

   while (accountIterator.hasNext()) {

    var account = accountIterator.next();

    AdsManagerApp.select(account);

 

    var campaignA = AdsApp.campaigns()

    .withCondition(Campaign_B).get().next();

    

    var keywords = campaignA.keywords()

    .withIds(keywordsIDs)

    .withCondition("Status = PAUSED")

    .withCondition("CampaignStatus = ENABLED")

    .withCondition("AdGroupStatus = ENABLED")

    .withCondition("KeywordMatchType = EXACT")

Google Ads Scripts Forum Advisor

unread,
Mar 7, 2021, 10:54:06 PM3/7/21
to adwords...@googlegroups.com

Hi Michael,

 

Thank you for pointing that out and clarifying your requirement. Please see below for the updated code. You would not need to filter the second keyword selector as we have already identified the keyword IDs on the first filtered keywords selector. Kindly take note that you would need to update the account ID and campaign names. Let me know how it goes after trying.

 

var accountIterator = AdsManagerApp.accounts().withIds(['123-456-789']).get();
 
var keywordsIDs = [];
var Campaign_A = "CampaignName CONTAINS 'Campaign_A'"
var Campaign_B = "CampaignName CONTAINS 'Campaign_B'"
 
function main() {
  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
 
    AdsManagerApp.select(account);
 
    var campaignA = AdsApp.campaigns().withCondition(Campaign_A).get().next();
 
    var keywords = campaignA.keywords()
    .forDateRange("LAST_MONTH")
    .withCondition("Clicks >= 1")
    .withCondition("Status = ENABLED")
    .withCondition("CampaignStatus = ENABLED"
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("KeywordMatchType = EXACT")
    .get();
 
    while(keywords.hasNext()) {
      var keyword = keywords.next();
      var adGroupID = keyword.getBaseAdGroup().getId();
      keywordsIDs.push([adGroupID, keyword.getId()]);
    }
       
    var campaignB = AdsApp.campaigns().withCondition(Campaign_B).get().next();
 
    var keywords = campaignB.keywords().withIds(keywordsIDs).get();
    while(keywords.hasNext()) {
      var keyword = keywords.next();
      keyword.pause();
    }
  }
}



Thanks,

Michael Smedegaard

unread,
Mar 8, 2021, 9:36:27 AM3/8/21
to Google Ads Scripts Forum
Hi Harry

When I test the code you sent (with updated account ID) and run a test of the script, nothing happens? The script does not report errors and I have made sure that there are clicks etc on the keywords I test.

How can that be?

A little addition to the script:
When a keyword is found in Campaign_A, it must pause in Campaign_A when the same keyword is activated in Campaign_B.
When the requirements for a keyword in Campaign_B no longer apply, the keyword must be paused in Campaign_B and activated in Campaign_A again

while (keywords.hasNext ()) {
       var keyword = keywords.next ();
       var adGroupID = keyword.getBaseAdGroup (). getId ();
       keywordsIDs.push ([adGroupID, keyword.getId ()]);
    // Pause keywords with satisfied metric from Campaign_A
       keyword.pause ();


Thanks,

Google Ads Scripts Forum Advisor

unread,
Mar 8, 2021, 10:42:32 PM3/8/21
to adwords...@googlegroups.com

Hi Michael,

 

Thanks for coming back. Kindly provide the script name and your CID so that I could check this on our end and assist you further. You may send them here or privately via the reply to author option. If this option is not available at your end, you may send it through our email (googleadsscr...@google.com) instead.

 

Can you also provide context to what you mean by "When the requirements for a keyword in Campaign_B no longer apply" so that I can assist you in this regard?

Google Ads Scripts Forum Advisor

unread,
Mar 9, 2021, 10:30:31 PM3/9/21
to adwords...@googlegroups.com

Hi Michael,

 

Thanks for providing the requested information. I was not aware that there would be multiple campaigns in Campaign A and Campaign B that you referred to. Kindly iterate through campaigns like in this example instead. Please see Scheduling a script section in this link so that you can automatically set your script to execute daily.

 

Also, as it seems that you would manage a large amount of keywords kindly save the keywords in a list before pausing them instead of pausing them inside the keyword iterator. Please see the recommended coding approach for your reference. If you would like to pause keywords in campaign B that are referenced from campaign A and vice versa, then I think you would have to create two separate scripts for this respectively or in the same script, but different code blocks.

Reply all
Reply to author
Forward
0 new messages