google-ads-script

Google AdsApp campaign selector filter by label name in the new experience


Next month the new experience for Google Ads Script will be enabled and I have to adapt my script to work with it.

In my script I look for campaigns with an specific label name:

  var selector = AdsApp
    .campaigns()
    .withCondition("LabelNames CONTAINS_ALL ['my-label']")

With the new experience it seems that the unique way to get all the campaigns filtered by a label has to be done using the label's resource name.

withCondition("campaign.labels CONTAINS ANY ('customers/1234567890/labels/123','customers/1234567890/labels/456')"). The value is a list of label resource names.

As I don't know that value, the unique way I found is to iterate the enabled campaigns, get the labels and check if the label I want exists. If exists I continue executing the code.

Is there a way I can get the campaigns filtered by the label name instead of the resource name?


Solution

  • It seems that is not possible so the unique way I found is getting the Label resource name first and then retrieve the campaigns.

    let label;
    const labelIterator = AdsApp.labels().withCondition('label.name = "my_label"').get();
    while (labelIterator.hasNext()) {
      label = labelIterator.next();
    }
    
    const campaignIterator = AdsApp.campaigns()
      .withCondition('campaign.status = ENABLED')
      .withCondition(`campaign.labels CONTAINS ALL ('${label.getResourceName()}')`)
      .get();
    

    This way I only perform two request and I get the campaigns that I need.