Label

Get all labels
Add a label
Apply a label to a campaign
Apply a label to an ad group
Apply a label to an ad

Get all labels


function getAllLabels() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const labels = Search.LabelService.get({
    accountId: accountId,
  }).rval;
  if (labels.totalNumEntries == 0) {
    Logger.log('Target LABEL does not exist.');
    return;
  }
  for (let i = 0; i < labels.values.length; i++){
    let label = labels.values[i].label;
    Logger.log('labelId-> ' + label.labelId + ', labelName-> ' + label.labelName);
  }
}

Add a label


function addLabel() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const labelArray = [
    {
      labelName: 'labelAAA',
    },
    {
      labelName: 'labelBBB',
    },
    {
      labelName: 'labelCCC',
    }
  ];//Multiple selections are possible
  const labels = Search.LabelService.add({
    accountId: accountId,
    operand: labelArray,
  }).rval;
  for (let i = 0; i < labels.values.length; i++){
    if (labels.values[i].operationSucceeded) {
      let label = labels.values[i].label;
      Logger.log('labelId-> ' + label.labelId + ', labelName-> ' + label.labelName + ' has added.');
    } else {
      Logger.log('labelName-> ' + labelArray[i].labelName + ' could not be added.');
    }
  }
}

Apply a label to a campaign


function applyLabelToCampaign() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignLabelArray = [
    {
      campaignId: 1111111,
      labelId: 22222,
    },
    {
      campaignId: 3333333,
      labelId: 44444,
    },
    {
      campaignId: 5555555,
      labelId: 66666,
    }
  ];//Multiple selections are possible
  const campaignLabels = Search.CampaignLabelService.add({
    accountId: accountId,
    operand: campaignLabelArray,
  }).rval;
  for (let i = 0; i < campaignLabels.values.length; i++){
    if (campaignLabels.values[i].operationSucceeded) {
      let campaignLabel = campaignLabels.values[i].campaignLabel;
      Logger.log('labelId->' + campaignLabel.labelId + ' have linked to campaignId-> ' + campaignLabel.campaignId);
    } else {
      Logger.log('labelId->' + campaignLabelArray[i].labelId + ' could not be linked to campaignId-> ' + campaignLabelArray[i].campaignId);
    }
  }
}

Apply a label to an ad group


function applyLabelToAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupLabelArray = [
    {
      adGroupId: 111111111,
      campaignId: 2222222,
      labelId: 33333,
    },
    {
      adGroupId: 444444444,
      campaignId: 5555555,
      labelId: 66666,
    },
    {
      adGroupId: 777777777,
      campaignId: 8888888,
      labelId: 99999,
    }
  ];//Multiple selections are possible
  const adGroupLabels = Search.AdGroupLabelService.add({
    accountId: accountId,
    operand: adGroupLabelArray,
  }).rval;
  for (let i = 0; i < adGroupLabels.values.length; i++){
    if (adGroupLabels.values[i].operationSucceeded) {
      let adGroupLabel = adGroupLabels.values[i].adGroupLabel;
      Logger.log('labelId-> ' + adGroupLabel.labelId + ' have linked to adGroupId-> ' + adGroupLabel.adGroupId);
    } else {
      Logger.log('labelId-> ' + adGroupLabelArray[i].labelId + ' could not be linked to adGroupId-> ' + adGroupLabelArray[i].adGroupId);
    }
  }
}

function applyLabelToAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdLabelArray = [
    {
      adGroupId: 111111111,
      adId: 2222222222,
      campaignId: 3333333,
      labelId: 44444,
    },
    {
      adGroupId: 555555555,
      adId: 6666666666,
      campaignId: 7777777,
      labelId: 88888,
    }
  ];//Multiple selections are possible
  const adGroupAdLabels = Search.AdGroupAdLabelService.add({
    accountId: accountId,
    operand: adGroupAdLabelArray,
  }).rval;
  for (let i = 0; i < adGroupAdLabels.values.length; i++){
    if (adGroupAdLabels.values[i].operationSucceeded) {
      let adGroupAdLabel = adGroupAdLabels.values[i].adGroupAdLabel;
      Logger.log('labelId-> ' + adGroupAdLabel.labelId + ' have linked to adGroupId-> '
        + adGroupAdLabel.adGroupId + ', adId-> ' + adGroupAdLabel.adId);
    } else {
      Logger.log('labelId-> ' + adGroupAdLabelArray[i].labelId + ' could not be linked to adGroupId-> '
        + adGroupAdLabelArray[i].adGroupId + ', adId-> ' + adGroupAdLabelArray[i].adId);
    }
  }
}