Labels

Get all labels from a user's account

function getAllLabels() {
  var labelIterator = AdWordsApp.labels().get();

  while (labelIterator.hasNext()) {
    var label = labelIterator.next();
    Logger.log(label.getName());
  }
}

Get a label by name

function getLabelsByName() {
  var labelIterator = AdWordsApp.labels()
      .withCondition('Name = "INSERT_LABEL_NAME_HERE"')
      .get();
  if (labelIterator.hasNext()) {
    var label = labelIterator.next();
    Logger.log('Name: ' + label.getName());
    Logger.log('Description: ' + label.getDescription());
    Logger.log('Color: ' + label.getColor());
    Logger.log('Number of campaigns: ' +
        label.campaigns().get().totalNumEntities());
    Logger.log('Number of ad groups: ' +
        label.adGroups().get().totalNumEntities());
    Logger.log('Number of ads: ' + label.ads().get().totalNumEntities());
    Logger.log('Number of keywords: ' +
        label.keywords().get().totalNumEntities());
  }
}

Apply a label to a campaign

function applyLabel() {
  // Retrieve a campaign, and apply a label to it. Applying labels to other
  // object types are similar.
  var campaignIterator = AdWordsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.applyLabel('Test');
  }
}

Remove a label from a campaign

function removeLabel() {
   var campaignIterator = AdWordsApp.campaigns()
       .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
       .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.removeLabel('Test');
  }
}

Remove a label from the user's account

function removeLabel() {
  var labelIterator = AdWordsApp.labels()
      .withCondition('Name = "INSERT_LABEL_NAME_HERE"')
      .get();
  if (labelIterator.hasNext()) {
    label = labelIterator.next();
    label.remove();
  }
}

Send feedback about...

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