Account Labels

Create an account label

function createAccountLabels() {
  var labelName = 'INSERT_LABEL_NAME_HERE';

  MccApp.createAccountLabel(labelName);
  Logger.log("Label with text = '%s' created.", labelName);
}

Apply an account label to multiple accounts

function applyAccountLabels() {
  var accountIds = ['INSERT_ACCOUNT_ID_HERE', 'INSERT_ACCOUNT_ID_HERE'];
  var labelName = 'INSERT_LABEL_NAME_HERE';

  var accounts = MccApp.accounts().withIds(accountIds).get();
  while (accounts.hasNext()) {
    var account = accounts.next();
    account.applyLabel(labelName);

    Logger.log('Label with text = "%s" applied to customer id %s.',
               labelName, account.getCustomerId());
  }
}

Remove an account label from multiple accounts

function removeLabelFromAccounts() {
  var accountIds = ['INSERT_ACCOUNT_ID_HERE', 'INSERT_ACCOUNT_ID_HERE'];
  var labelName = 'INSERT_LABEL_NAME_HERE';

  var accounts = MccApp.accounts().withIds(accountIds).get();
  while (accounts.hasNext()) {
    var account = accounts.next();
    account.removeLabel(labelName);

    Logger.log('Label with text = "%s" removed from customer id %s.',
               labelName, account.getCustomerId());
  }
}

Select an account by label name

function selectAccountsByLabelName() {
  var labelName = 'INSERT_LABEL_NAME_HERE';

  var accountIterator = MccApp.accounts()
      .withCondition("LabelNames CONTAINS '" + labelName + "'")
      .get();

  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
    var accountName = account.getName() ? account.getName() : '--';
    Logger.log('%s,%s,%s,%s', account.getCustomerId(), accountName,
        account.getTimeZone(), account.getCurrencyCode());
  }
}

Select an account by label ID

function selectAccountsByLabelId() {
  var labelId = 'INSERT_LABEL_ID_HERE';
  var accountIterator = MccApp.accounts()
      .withCondition("LabelIds IN ['" + labelId + "']")
      .get();

  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
    var accountName = account.getName() ? account.getName() : '--';
    Logger.log('%s,%s,%s,%s', account.getCustomerId(), accountName,
        account.getTimeZone(), account.getCurrencyCode());
  }
}

Retrieve all account labels

function getAllAccountLabels() {
  var labelIterator = MccApp.accountLabels().get();
  while (labelIterator.hasNext()) {
    var label = labelIterator.next();

    Logger.log('Label with id = %s and text = %s was found.',
               label.getId().toFixed(0), label.getName());
  }
}

Retrieve an account label by its name

function getLabelByName() {
  var labelName = 'INSERT_LABEL_NAME_HERE';

  var labelIterator = MccApp.accountLabels()
      .withCondition("Name CONTAINS '" + labelName + "'")
      .get();

  while (labelIterator.hasNext()) {
    var label = labelIterator.next();

    Logger.log('Label with id = %s and text = %s was found.',
               label.getId().toFixed(0), label.getName());
  }
}

Retrieve account labels by their IDs

function getLabelById() {
  var labelIterator = MccApp.accountLabels()
      .withIds([12345, 67890]) // Replace with label IDs here
      .get();

  while (labelIterator.hasNext()) {
    var label = labelIterator.next();

    Logger.log("Label with id = %s and text = '%s' was found.",
               label.getId().toFixed(0), label.getName());
  }
}

Send feedback about...

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