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

Labels

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

Get all labels from a user's account

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

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

Get a label by name

function getLabelsByName() {
  var labelIterator = AdsApp.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 = AdsApp.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 = AdsApp.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 = AdsApp.labels()
      .withCondition('Name = "INSERT_LABEL_NAME_HERE"')
      .get();
  if (labelIterator.hasNext()) {
    label = labelIterator.next();
    label.remove();
  }
}