Can't select accounts in AdsManagerApp by Label

94 views
Skip to first unread message

Sam Green

unread,
May 23, 2023, 4:11:49 AM5/23/23
to Google Ads Scripts Forum
Hi there,

In the new experience, I don't seem to be able to select accounts via the label. For example, the code below doesn't return any accounts: 

```function selectAccountsByLabelResourceName(label) {
    var query = "customer_client.applied_labels CONTAINS ANY ('customers/8xxxxxxxxx/labels/4xxxxxxxxx')";

    let accountIterator = AdsManagerApp.accounts()
        .withCondition(query)
        .get();
 
    for (const account of accountIterator) {
        const accountName = account.getName() ? account.getName() : '--';
        console.log('123:: %s,%s,%s,%s', account.getCustomerId(), accountName);
  }
}```

What am I missing?

Google Ads Scripts Forum Advisor

unread,
May 24, 2023, 1:42:26 AM5/24/23
to adwords...@googlegroups.com

Hi Sam,

 

Thank you for reaching out to the Google Ads Scripts Forum Channel.

 

Kindly note that you would need to implement the script inside a main function or call this function (selectAccountsByLabelResourceName) within the main function. Upon testing the content of your script on my end, I was able to retrieve an account a label was applied to:

function main() {
  
  var query = "customer_client.applied_labels CONTAINS ANY ('customers/8xxxxxxxxx/labels/4xxxxxxxxx')";
 
    let accountIterator = AdsManagerApp.accounts()
        .withCondition(query)
        .get();
 
    for (const account of accountIterator) {
        const accountName = account.getName() ? account.getName() : '--';
        console.log('123:: %s,%s,%s,%s', account.getCustomerId(), accountName);
  }
  
}

 

Kindly try this on your end and let us know how it goes. If you'd still encounter any issues, please provide your Google Ads account ID as well as the name of this script so that we may be able to further investigate.

 

This message is in relation to case "ref:_00D1U1174p._5004Q2lKjX7:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Martijn Kraan

unread,
Jun 2, 2023, 8:06:00 AM6/2/23
to Google Ads Scripts Forum
I'm having the same issue.
Created a label, applied the label to 2 accounts in my MCC, fetched the resource name (customers/9245129884/labels/21866560486) but the sample script you've provided returns 0 accounts
What to do?

Script used:

function main() {
 
  var query = "customer_client.applied_labels CONTAINS ANY ('customers/9245129884/labels/21866560486')";

 
    let accountIterator = AdsManagerApp.accounts()
        .withCondition(query)
        .get();
 
    console.log(accountIterator.totalNumEntities());
}

Omnicom Digital

unread,
Jun 6, 2023, 6:17:38 AM6/6/23
to Google Ads Scripts Forum
The applied_labels method causes issues. I have similar one and it also returns 0 accounts in selector.

It happens when I deal with Ad Account that is connected via other MCC.
Script MCC: A (name: Youtube exclusion)
Other connected MCC: B
AdAccount: B -> account
We have admin priviliges to that ad account.

var selector = AdsManagerApp.accounts().orderBy('Name');
for (var i = 0; i < customerIds.length; i++) {
if (LABEL_TO_SKIP_ID) {
      var query = "customer_client.applied_labels CONTAINS ANY ('customers/" +   String(customerIds[i])/*.replace(/-/gi, "")*/ + "/labels/" + String(LABEL_TO_SKIP_ID) + "')";
       
        Logger.log(query);
        selector = selector.withCondition(query);
        var iter = selector.get();
        Logger.log(JSON.stringify( iter));
        // selector.get().totalNumEntities();
        Logger.log(JSON.stringify(AdsManagerApp.accounts().withIds(['743-488-7791']).get()));
        Logger.log(AdsManagerApp.accounts().withIds(['743-488-7791']).get().totalNumEntities());


2 thing happens:
  • For account  743-488-7791 I get error: Exception: Call to GoogleAdsService.Search failed: part of the resource name is invalid. This happens AFTER the withCondition at line  selector.get().totalNumEntities();    If you look at logs, it logs the same thing when accessing methods of the iterator via the withCondition selector and via withIds selector.  a.PNG
            AdsManagerApp.accounts().withIds(['743-488-7791']).get().totalNumEntities() works just fine.

Zweitze de Vries

unread,
Jun 6, 2023, 11:16:07 AM6/6/23
to Google Ads Scripts Forum
I looked in the script examples and found this (script Account Anomaly Detector - Manager Account):
    if (CONFIG.account_label) {
      accountSelector
.withCondition(
         
'LabelNames CONTAINS \'' + CONFIG.account_label + '\'');
   
}
I can confirm this works, with the limitation that you're not looking for labels with a single quote in their name. And I think this is case sensitive.

There's another gotcha when you intend to select accounts in sub MCCs, filtering on labels. But that's another issue.

Sigurd Fabrin

unread,
Jun 8, 2023, 4:34:18 AM6/8/23
to Google Ads Scripts Forum
I still using the old syntax when selecting accounts filtered by labels. Not sure why, but it's still working.

Like this:
var iter = AdsManagerApp.accounts()
    .withCondition('LabelNames = "Name of account label"')
    .get();
  while (iter.hasNext()) {
    var account = iter.next();
    AdsManagerApp.select(account);
 // your code here
}


sigurd

Reply all
Reply to author
Forward
0 new messages