This is the legacy documentation for Google Ads scripts. Go to the current docs.

Ad Groups

Stay organized with collections Save and categorize content based on your preferences.

Add an ad group

function addAdGroup() {
  var campaignIterator = AdsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var adGroupOperation = campaign.newAdGroupBuilder()
        .withName('INSERT_ADGROUP_NAME_HERE')
        .withCpc(1.2)
        .build();
  }
}

Update an ad group

function updateAdGroup() {
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    adGroup.bidding().setCpc(1.2);
    // update other properties as required here
  }
}

Get all ad groups

function getAlladGroups() {
  // AdsApp.adGroups() will return all ad groups that are not removed by
  // default.
  var adGroupIterator = AdsApp.adGroups().get();
  Logger.log('Total adGroups found : ' + adGroupIterator.totalNumEntities());
  while (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    Logger.log('AdGroup Name: ' + adGroup.getName());
  }
}

Get an ad group by name

function getAdGroupByName() {
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    Logger.log('AdGroup Name: ' + adGroup.getName());
    Logger.log('Enabled: ' + adGroup.isEnabled());
  }
}

Get an ad group's stats

function getadGroupstats() {
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    // You can also request reports for pre-defined date ranges. See
    // https://developers.google.com/adwords/api/docs/guides/awql,
    // DateRangeLiteral section for possible values.
    var stats = adGroup.getStatsFor('LAST_MONTH');
    Logger.log(adGroup.getName() + ', ' + stats.getClicks() + ', ' +
        stats.getImpressions());
  }
}

Pause an ad group

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

Get an ad group's device bid modifiers

function getAdGroupBidModifiers() {
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    Logger.log('AdGroup name: ' + adGroup.getName());
    Logger.log('Mobile bid modifier: ' +
        adGroup.devices().getMobileBidModifier());
    Logger.log('Tablet bid modifier: ' +
        adGroup.devices().getTabletBidModifier());
    Logger.log('Desktop bid modifier: ' +
        adGroup.devices().getDesktopBidModifier());
  }
}