How to apply labels to campaign without "Ba: Expected one label named 'test' but found 0"

57 views
Skip to first unread message

Mirko Norz

unread,
Oct 6, 2023, 11:36:31 AM10/6/23
to Google Ads Scripts Forum
I am attempting to apply custom labels to campaigns using Google Ads scripts. However, I consistently encounter the following error:

"Ba: Expected one label named 'test' but found 0"

I would greatly appreciate your guidance and support in resolving this issue. If there are any specific steps or best practices related to applying custom labels to campaigns via Google Ads scripts, I would welcome your expert advice.

Sigurd Fabrin

unread,
Oct 10, 2023, 4:27:02 AM10/10/23
to Google Ads Scripts Forum
It sounds like you're trying to add a label that doesn't exist, while running the script in preview.

Smth like this approach should work 
function main() {  
  const campaignName = 'Name or partial name of campaign';
  const labelName = 'Name of label';
  if (!AdsApp.labels() // check if label exist
      .withCondition(`LabelName = "${labelName}"`)
      .get()
      .hasNext()) {
    AdsApp.createLabel(labelName); // if it doesn't exist, create it
    console.log(`Label "${labelName}" created`)
  }
  else {console.log(`Label already exists, let\'s reuse it`)}
  const campaignTypes = [ // missing discovery campaign ..
    AdsApp.campaigns(),
    AdsApp.shoppingCampaigns(),
    AdsApp.videoCampaigns(),
    AdsApp.performanceMaxCampaigns()
  ];
  console.log(`Label applied to campaign(s):`);
  for (i=0;i<campaignTypes.length;i++) {
    const campaignIter = campaignTypes[i]
    .withCondition(`campaign.name LIKE "%${campaignName}%"`)
    .get();
    while (campaignIter.hasNext()) {
      const campaign = campaignIter.next();
      try {
      campaign.applyLabel(labelName); // apply label to campaign
      console.log(campaign.getName());
      }
      catch(e) {
        if (e.toString().match(/.*but\sfound\s0.*/)) {
        console.log(`Cannot apply a (so far) non-existing label while in preview`);
        }
        else {console.log(e)}
      }
    }
  }
}


Sigurd

Google Ads Scripts Forum Advisor

unread,
Oct 10, 2023, 10:37:14 AM10/10/23
to adwords...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads Scripts support team.

After reviewing your concern, I understand that you are trying to apply Custom Labels to Campaigns using Google Ads Scripts and getting an error "Ba: Expected one label named 'test' but found 0". I would suggest you refer to the document Labels to get more information. Also, please go through the document AdsApp.​CampaignSelector.

If the issue still persists, kindly provide us with the following details to further investigate your concern: 
  • Google Ads account ID / CID
  • Name of the affected script
  • Screenshot of the issue
  • If the script uses a spreadsheet, please provide a shareable link to the spreadsheet.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2pQJdE:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages