Getting the right labels from the AdWords API

230 views
Skip to first unread message

Eirik Ovesen

unread,
Dec 13, 2019, 7:10:05 AM12/13/19
to AdWords API and Google Ads API Forum
Hi!

This is my first meeting with the Google Ads API and I'm attempting to achieve labeling of ads in my Python application.

Case:
1. The app fetches all ads marked with a specific label (e.g. "LabelName": "X") for a given AccountA->Campaign->Adgroup structure 
2. A user agent clicks a button
3. On click, the app has to:
3a) Look for a label "Y"
3b) If one exists, get the ID of that label, if not, create a label "Y" and get its ID
3c) Remove the label "X" from said ads
3d) Apply the label "Y" to said ads

So here's my problem:
I'm finding label "Y" (using LabelService), but in trying to add it to an ad (using the AdGroupAdService),
I get this error: 
[EntityNotFound.INVALID_ID @ operations[0].operand.labelId; trigger:'LabelId: yyyyyyyyyy'] #Redacted id


I have but one theory as to why this happens: 
The label "Y" doesn't exist for the AccountA in the structure, so it is not available to link to these ads when looking at the adgroup in the Google Ads GUI, but my 
find_or_create(c, "Y") method still finds (doesn't create) a label "Y" and returns its ID. I believe this label "Y" to be owned by AccountB, which I'm also able to manage through my Google Ads user.
This is a slightly flawed theory, because find_or_create(c, "X") does finds one label "X" every time, even though I have several labels "X" across different accounts, and I'm able to remove the "X" label from the ads.

I know that the find_or_create() and add_label_to_ad() methods are able to create a new label and link it to the ads of interest.
If I try to find_or_create() a label "Stupid Labels" and use it to label the ads, it works. I believe this is because there exists no such label in any other accounts,
so the methods are able to create and link it with no trouble of finding the Entity.

TLDR
How do I make sure that the label I'm finding belongs to the account (AccountA) that owns the ads I want to change?
Which service/selector/predicate combination would I need to use?
Is there any way to find the Label IDs in the Google Ads GUI to verify my finds?

Here's my code:

def find_or_create_label(client, label_name):
    service
= client.GetService("LabelService", version=API_VERSION)
    selector = {
       
"fields": ["LabelId", "LabelName"],
       
"predicates": [
             
{"field": "LabelName", "operator": "EQUALS", "values": [label_name]}
       
]
   
}
    label
= service.get(selector)
   
   
for entry in label.entries:
       
if entry["name"] == label_name:
           
return entry["id"]

    operation
= {
       
"operator": "ADD",
       
"operand": {"name": label_name, "xsi_type": "TextLabel"}

    result
= service.mutate(operation)
   
return result["value"][0].id



Google Ads API Forum Advisor Prod

unread,
Dec 13, 2019, 12:05:58 PM12/13/19
to eirik...@gmail.com, adwor...@googlegroups.com
Hi Erik, 

Thank you for the detailed explanation. Based on the logs it looks like you're using AdWords API. Could you please confirm if you're using Google Ads API or AdWords API? You will be able to find the specific labels in the account using the LabelService.get() operation or the Label Report which will generate the Label name along with the Label Ids. It is not possible to look up the Label Id through the Google Ads UI. Could you please try the above methods and try assigning them to an ad? You may refer to this guide for more information.

Regards,
Bharani, Google Ads API Team

ref:_00D1U1174p._5001UOEUyn:ref

Eirik Ovesen

unread,
Dec 16, 2019, 7:04:07 AM12/16/19
to AdWords API and Google Ads API Forum
Hey Brahani, and thanks for the swift reply 

Sorry I couldn't get back to you sooner.

I can confirm that I'm using the AdWords API. I was mistaken to say it was the Ads API in the question description.
I've been looking at the links you sent me, but I'm not getting anywhere. 
I tried generating the LABEL_REPORT by following this guide, which gives me this output:
Report type "LABEL_REPORT" contains the following fields:
 - AccountDescriptiveName (String)
  := []
 - AdGroupCreativesCount (Long)
  := []
 - AdGroupCriteriaCount (Long)
  := []
 - AdGroupsCount (Long)
  := []
 - CampaignsCount (Long)
  := []
 - ExternalCustomerId (Long)
  := []
 - LabelId (Long)
  := []
 - LabelName (String)
  := []
 - UserListsCount (Long)
  := []

I've also tried using the LabelService.get() method to get all labels, and discovered something curious. I'll type out the print
service = client.GetService("LabelService", version=API_VERSION)
selector = { "fields": ["LabelId", "LabelName"] }
result = service.get(selector)
print(result)

# Print:
# {
#    'totalNumEntries': 4
#    'Page.Type': 'LabelPage',
#    'entries': [
#        {
#            'id': xxxxxxxxxx,
#            'name': 'X',
#            'attribute....
#        },
#        {
#            'id': yyyyyyyyyy,
#            'name': 'Y',
#            ...
#        },
#        {
#            'id': zzzzzzzzzz,
#            'name': 'Stupid Labels',
#            ...
#        },
#        {
#            'id': wwwwwwwwww,
#            'name': 'SomeLabel',
#            ...
#        }
#    ]
#}

The interesting thing here is that I'm finding 4 labels, but I can only see 3 of them when attempting to add labels to this account's Ads.
As previously stated, the "Y" label is the one I'm missing in the GUI, which I'm guessing is connected to the EntityNotFound error I'm experiencing.
To reiterate: I have several labels "X" on other accounts, and one label "Y" on another account, so I do believe that the label "Y" for this account exists, and that it's the one I'm getting,
but that there's a link missing somewhere. 

The "Stupid Labels" label was created by my find_or_create() method, and I was able to label the ads after creating it.

Here's the code I'm using to label ads after getting the ID from the find_or_create() method
def add_label_to_ad(client, ad_id, adgroup_id, label_id):
    adgroupadservice = client.GetService("AdGroupAdService", version=API_VERSION)

    add_op = (
        {
            "xsi_type": "AdGroupAdLabelOperation",
            "operator": "ADD",
            "operand": {
                "xsi_type": "AdGroupAdLabel",
                "adGroupId": adgroup_id,
                "adId": ad_id,
                "labelId": label_id,
            },
        },
    )

    result = adgroupadservice.mutateLabel(add_op)
    return result

Google Ads API Forum Advisor Prod

unread,
Dec 16, 2019, 3:19:50 PM12/16/19
to eirik...@gmail.com, adwor...@googlegroups.com
Hi Erik,

Could you please share the API SOAP logs for the LabelService.get() operation? This will help be take a look at the results and address this concern. You can share the details privately via Reply privately to author option.

Thanks,
Reply all
Reply to author
Forward
0 new messages