Pausing Ad Groups from a Google Sheet

355 views
Skip to first unread message

Carter Knight

unread,
Apr 5, 2021, 4:58:17 PM4/5/21
to Google Ads Scripts Forum
Hello,

I was wondering if anyone could please assist me with a script for pausing ad groups based off of a list in a Google Sheet which is formatted with three columns with the headers 'Campaign', 'Ad Group', and 'Status'. When I run this script below, it tells me 'TypeError: Cannot find function pause in object true.-- Campaign: Campaign Name and ad group: Ad Group Name do not exist' even though the Campaign and Ad Groups do exist in the account:


function main() {
  var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName("Stuff to Pause Summary");  
  var data = spreadsheet.getRange("A3:C").getValues(); 
    
  for(var i = 0; i < data.length; i++) {
    
    var row = data[i];
    if(row[0] == '')
      break;
    else {
      try {
        var status = row[2];
        var ag = row[1];
        var campaign = row[0];
        var adGroup = AdsApp.adGroups().withCondition("Name = '" + ag + "'").withCondition("CampaignName = '" + campaign + "'").get().hasNext();
        var pauseAdGroup = adGroup.pause();
        
        Logger.log("We paused the following ad groups: " + pauseAdGroup);
      }
      catch(e) { Logger.log(e + "-- Campaign: %s and ad group: %s do not exist", row[0], row[1])}
    }
  }  
}

Any help would be appreciated, thanks!

Google Ads Scripts Forum Advisor

unread,
Apr 6, 2021, 1:32:01 AM4/6/21
to adwords...@googlegroups.com
Hi Carter,

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

You would have to make use of an AdGroupIterator so you may iterate through entities in your AdGroupSelector. Here's an example on how you can pause an Ad Group. I would also like to inform you that the pause is of type void and would not return anything so putting it in a variable and logging would not do anything. Kindly use the getName and getId methods instead.

Kindly update your script accordingly and let me know how it goes.
 

Thanks,
Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2Eq4LF:ref

Carter Knight

unread,
Apr 13, 2021, 3:45:18 PM4/13/21
to Google Ads Scripts Forum
Thanks for your help, Harry. My updated sheet can now pause a single ad group, but am I able to pause a list of ad groups at the same time with the script? Here's how my script looks to pause one ad group:


function main() {
  var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName("Stuff to Pause Summary");
  var data = spreadsheet.getRange("B4").getValue();
  
  Logger.log(data)

  // pause the ad group
  
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "' + data + '"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    adGroup.pause();
    Logger.log('AdGroup with name = ' + adGroup.getName() +
        ' has paused status : ' + adGroup.isPaused());
  }
}

Google Ads Scripts Forum Advisor

unread,
Apr 13, 2021, 11:13:22 PM4/13/21
to adwords...@googlegroups.com
Hi Carter,

Kindly replace the if statement with a while loop so that you can loop through the AdGroupIterator. In addition, as you have mentioned that you have multiple ad groups that you would like to pause, please take note of our best practice to not leave your selectors in an indeterminate state. When you update a list of entities, make sure your code doesn't break the iterator's selector condition as a side effect which could lead to unexpected behavior.

Carter Knight

unread,
Apr 16, 2021, 1:41:52 PM4/16/21
to Google Ads Scripts Forum
Perfect, thank you!
Reply all
Reply to author
Forward
0 new messages