Ad Groups

Add an ad group

function addAdGroup() {
  var campaignIterator = AdWordsApp.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 = AdWordsApp.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() {
  // AdWordsApp.adGroups() will return all ad groups that are not removed by
  // default.
  var adGroupIterator = AdWordsApp.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 = AdWordsApp.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 = AdWordsApp.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 = AdWordsApp.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 = AdWordsApp.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());
  }
}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.