Check if Label Exists

419 views
Skip to first unread message

Feras Khalbuss

unread,
May 24, 2019, 12:32:21 PM5/24/19
to Google Ads Scripts Forum
Hi everyone,

I hope all is well. This script may overlap with earlier threads slightly but my biggest question is if this script can be modified to check if the label exists. If not then create it. The issue I see is that if the labels are created and already exist for a large account that could increase the run time of the script significantly. 

The script below creates expanded text ads with a label for more context. 

Thanks in advance for your help. 


function main() {
  
  var spreadsheet = SpreadsheetApp.openByUrl('REDACTED);
  var sheet = spreadsheet.getActiveSheet();
  var data = sheet.getRange("A:J").getValues();
  for (i in data) {
    if (i == 0) {
      continue;
    }
    var [adGroupId, h1, h2, h3, d1, d2, p1, p2, url, labela] = data[i];
    if (adGroupId == ''){
      break;
    }
    else{       
      var adGroupIterator = AdsApp.adGroups().withIds([adGroupId]).get();
      if(adGroupIterator.hasNext()){
        var adGroup = adGroupIterator.next();
        var adOperation = adGroup.newAd().expandedTextAdBuilder()
        .withHeadlinePart1(h1)
        .withHeadlinePart2(h2)
  .withHeadlinePart3(h3)
        .withDescription1(d1)
  .withDescription2(d2)
        .withPath1(p1)
        .withPath2(p2)
        .withFinalUrl(url)
        .build();

var ad = adOperation.getResult();
AdsApp.createLabel(labela)
ad.applyLabel(labela);
        
             }
    }
  }
}

Google Ads Scripts Forum Advisor Prod

unread,
May 27, 2019, 2:32:43 AM5/27/19
to adwords...@googlegroups.com

Hi Feras,

Thanks for raising your concern.

Could you confirm if your goal is to create a script that will only check whether specific label is already existing in your account? If yes, you may try to implement the sample script in this link as it will only retrieve the labels that satisfied the condition set on the label selector. You may also try to utilize the totalNumEntities() method as it will return the total number of labels that satisfies the filter set on the selector.

Let me know if you have questions/clarifications.

Regards,
Ejay
Google Ads Scripts Team



ref:_00D1U1174p._5001UAqXWe:ref

mouhsine ahmed

unread,
May 28, 2019, 7:07:07 AM5/28/19
to Google Ads Scripts Forum
Message has been deleted

Robert Zimmermann

unread,
Jun 6, 2019, 6:44:12 AM6/6/19
to Google Ads Scripts Forum
Hi Feras,

I've added this part at the beginning of my Script and it works fine for me:

var labelIterator = AdsApp.labels().withCondition("Name CONTAINS 'LabelName'").get();
      if (labelIterator.hasNext()){}
      else{
          AdsApp.createLabel('LabelName')
          }

I use it to see if a label already exist before labeling campaigns with it.

Best regards

Robert
Reply all
Reply to author
Forward
0 new messages