label condition on campaign level for adgroup selector

415 views
Skip to first unread message

Holger Schulz

unread,
May 20, 2022, 7:00:20 AM5/20/22
to Google Ads Scripts Forum
Hi,

great that you can add a label condition on campaign level e.g. for an AdGroup selectors now.
But:-)
I can not belive that this is really that complicated.

I am looking for something like:
AdsApp.adGroups().withCondition("campaign.labels.name CONTAINS ALL ('my_label_name')")

I found this solution but I hope for a hint for a better solution:-)

function main() {
  var account = AdsManagerApp.accounts().withIds(["123-456-7890"]).get().next();
  AdsManagerApp.select(account);

  var sLabelCampaign = "label_name_used_for_campaigns";
  var sLabelAdGroup = " label_name_used_for_adgroups";

  var selAdGroups = AdsApp.adGroups().withCondition('campaign.status = "ENABLED"').withCondition("campaign.serving_status = 'SERVING'");
  withCondidtionLabel(selAdGroups, sLabelCampaign, sLabelAdGroup);
 
  var iter = selAdGroups.get();
  while(iter.hasNext()) {
    var el = iter.next();
    Log.debug(el.getCampaign().getName()," -> ", el.getName());
   
  }
}

function withCondidtionLabel(_selector, _sLabelCampaign, _sLabelAdGroup) {
  // ##what I would like to do:
  // _selector.withCondition("campaign.labels.name CONTAINS ALL ('"+sLabelCampaign+"')");
  // _selector.withCondition("adgroup.labels.name CONTAINS ALL ('"+sLabelAdGroup+"')");
  // or for the current selector level: _selector.withCondition("labels.name CONTAINS ALL ('"+sLabelAdGroup+"')");
 
  // ## work around I found
  var sAccountNo = AdsApp.currentAccount().getCustomerId().replaceAll("-", "");
  var sLabelCampaignId = AdsApp.labels().withCondition("LabelName CONTAINS '"+_sLabelCampaign+"'").get().next().getId();  
  _selector.withCondition("campaign.labels CONTAINS ALL ('customers/"+sAccountNo+"/labels/"+sLabelCampaignId+"')")
  _selector.withCondition("LabelNames CONTAINS '"+_sLabelAdGroup+"'");
}


Any idea for an more elegant solution?

Thanks
       Holger

Google Ads Scripts Forum Advisor

unread,
May 24, 2022, 2:16:56 AM5/24/22
to adwords...@googlegroups.com
Hi Holger,

Thank you for reaching out to us.

With regard to your concern, filtering labels on specific AdsApp entity (e.g  campaign selector) should be a list of label resource names. Your implementation approach should be fine. What you can also consider is you may generate your entity selector with labels by using AdsApp.​LabelSelector first. Kindly refer to the sample function below:
 
var CONFIG = {
  //Campaign labels to filter | Leave blank to select all campaigns
  CAMPAIGN_LABEL: "",
};

function getCampaignIterator () {

  if (CONFIG.CAMPAIGN_LABEL == "") {

      return AdsApp.campaigns().get();

  } else {

      var labelSelector = AdsApp.labels()
          .withCondition("label.name = '" + CONFIG.CAMPAIGN_LABEL + "'")
      ;

      var ids = [];

      var labelIterator = labelSelector.get();
      while (labelIterator.hasNext()) {
          var label = labelIterator.next();
          
          var campaignIterator = label.campaigns().get();
      
          while (campaignIterator.hasNext()) {

          var campaign = campaignIterator.next();
      
              ids.push(campaign.getId());
      
          } 
      }

      return AdsApp.campaigns().withIds(ids).get();

  }

}

function main () {

var campaignIterator = getCampaignIterator();
while (campaignIterator.hasNext()) {

      var campaign = campaignIterator.next();

      Logger.log(campaign.getName());


    }

}

Regards,
Google Logo
Teejay Wennie
Google Ads Scripts Team
 
 

ref:_00D1U1174p._5004Q2bBewN:ref

Holger Schulz

unread,
May 24, 2022, 6:12:53 AM5/24/22
to Google Ads Scripts Forum
Thanks

         Holger

Google Ads Scripts Forum Advisor

unread,
May 26, 2022, 2:57:51 AM5/26/22
to adwords...@googlegroups.com
Hello,

Let us know if you have any other concerns related to Google Ads Scripts. We'll be happy to assist you.

Ritesh Kumar Jha

unread,
Jun 6, 2022, 2:03:36 PM6/6/22
to Google Ads Scripts Forum on behalf of adsscripts
Yes,
We are trying to add a performance report script to my Google Ad account but we are unable to run this script sucessfully

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/ZOfK8000000000000000000000000000000000000000000000RCHA0800NBOlAQYDTTG9umJ6bBvhaA%40sfdc.net.

Google Ads Scripts Forum Advisor

unread,
Jun 7, 2022, 12:59:45 AM6/7/22
to adwords...@googlegroups.com

Hello,

Thank you for looping in. For our team to assist you, can you please provide us with the following details below via `Reply privately to author` option so that we can conduct an investigation into your issue?

  • Google Ads account ID / CID
  • Script name
  • Screenshot of the error
  • Shareable link of the involved spreadsheet, if applicable.

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2bBewN:ref
Reply all
Reply to author
Forward
0 new messages