Adding labels on keywords

732 views
Skip to first unread message

gior...@jobtome.com

unread,
Jun 16, 2016, 9:13:29 AM6/16/16
to AdWords API Forum
Hello all,

today I was creating labels for my Adwords account using API while I just found one topic talking about how to add Labels (https://developers.google.com/adwords/api/docs/guides/labels#create_your_labels) and I got no problems on following the guide.

By the way I want to try to assign those labels on my keywords and I was reading the type CampaignLabel (v201605) page (that talks about how to add labels on campaign) says there are 2 fields: 

campaignId (The id of the campaign that the label is applied to)
labelId (The id of an existing label to be applied to the campaign)

Now, if I want to add labels on my keywords, instead of campaigns, how can I do? 

Thanks a lot,

bye!

Shwetha Vastrad (AdWords API Team)

unread,
Jun 16, 2016, 12:25:35 PM6/16/16
to AdWords API Forum
Hi,

To add labels to your keywords, you would need to create AdGroupCriterionLabel which contains:
  • labelId - ID of the label.
  • adGroupId - ID of the AdGroup containing the keyword.
  • criterionId - ID of the keyword.
Once you have created these objects, you need to use the AdGroupCriterionService.mutateLabel with ADD operator to associate an existing Label to an existing AdGroupCriterion.

Regards,
Shwetha, AdWords API Team.

s...@feedonomics.com

unread,
Oct 30, 2018, 5:35:57 PM10/30/18
to AdWords API and Google Ads API Forum
Hi Shwetha,

We are trying to do the same, but how do we set the label text for AdGroupCriterionLabel? I don't see a set function for label text.

Thanks!

Vincent Racaza (AdWords API Team)

unread,
Oct 30, 2018, 7:46:19 PM10/30/18
to AdWords API and Google Ads API Forum
Hi,

Before you can associate the label to your ad group via AdGroupCriterionService, you need to create a label first with its text/name via LabelService.mutate().

Let me know if you have further clarifications.

Thanks,
Vincent
AdWords API Team

s...@feedonomics.com

unread,
Oct 30, 2018, 9:34:08 PM10/30/18
to AdWords API and Google Ads API Forum
$label_id = TempIdGenerator::Generate();
$keyword_label = new TextLabel();
$keyword_label->setId($label_id);
$keyword_label->setName($label_text);
$keyword_label->setAttribute('test');

$label_operation = new LabelOperation(); // Label operation should not be included in the BatchJobService
$label_operation->setOperand($keyword_label);
$label_operation->setOperator('ADD');

$adwords_services = new AdWordsServices();
$label_service = $adwords_services->get($session, LabelService::class);
$result = $label_service->mutate(array($label_operation));

$adgroup_criterion = new AdGroupCriterion();
$adgroup_criterion->setLabels(array(new AdGroupCriterionLabel($adgroup_id, $keyword_id, $label_id)));

$operation = new AdGroupCriterionLabelOperation();
$operation->setOperand($adgroup_criterion);
$operation->setOperator('ADD');

return $operation;

The above code is what ended up working for us, but we are still getting these errors:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rval>
<errorList>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.adGroupId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>adGroupId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.criterionId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>criterionId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.labelId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>labelId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.labelId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>labelId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.criterionId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>criterionId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RequiredError">
<fieldPath>operations[0].operand.adGroupId</fieldPath>
<fieldPathElements>
<field>operations</field>
<index>0</index>
</fieldPathElements>
<fieldPathElements>
<field>operand</field>
</fieldPathElements>
<fieldPathElements>
<field>adGroupId</field>
</fieldPathElements>
<trigger/>
<errorString>RequiredError.REQUIRED</errorString>
<ApiError.Type>RequiredError</ApiError.Type>
<reason>REQUIRED</reason>
</errors>
</errorList>
<index>0</index>
</rval>
</mutateResponse>

Any idea how we can fix these errors? The label is getting added to the keyword now.

Thanks!

Vincent Racaza (AdWords API Team)

unread,
Oct 31, 2018, 3:58:57 PM10/31/18
to AdWords API and Google Ads API Forum
Hi Sam,

Can you confirm if you are using BatchJobService to add labels to your keywords? Please note that LabelOperation is not a supported operation by BatchJobService. If you're indeed using BatchJobService, you may get all of the label_id first and then use these as temporary Ids to set labels in BatchJobService via AdGroupCriterionLabelOperation.

Note: We are running an annual developer feedback survey. Please take opportunity to leave feedback on how we can make managing campaigns at scale even easier.


Thanks,
Vincent
AdWords API Team

s...@feedonomics.com

unread,
Oct 31, 2018, 7:52:19 PM10/31/18
to AdWords API and Google Ads API Forum
Hi Vincent,

We are using LabelService to mutate the label operation and not using BatchJobService. Another issue that seems to be happening, is the label being added to all keywords rather than the one specified by "keyword_id".

Is there anywhere we could find sample code for adding a keyword label?

Thanks!

Sreelakshmi Sasidharan (AdWords API Team)

unread,
Nov 1, 2018, 11:40:58 AM11/1/18
to AdWords API and Google Ads API Forum
Hello, 

From the code snippet it is not clear if you are performing the AdGroupCriterionService.mutateLabel() operation. Please take a look at the attached code snippet (in Java) to get an idea of how to create a label and attach it to a specific keyword. Please let me know if you have any additional questions. 

Thanks,
Sreelakshmi, AdWords API Team

AddLabelToKeywords

s...@feedonomics.com

unread,
Nov 1, 2018, 5:34:33 PM11/1/18
to AdWords API and Google Ads API Forum
Thank you Sreelakshmi, this helps a lot!

bruno...@ciadaconsulta.com.br

unread,
Jan 10, 2019, 7:40:28 PM1/10/19
to AdWords API and Google Ads API Forum
Hi Shwetha,

How can I create label in API?
Because I need to create a label with a date that I updated the CPCmax.

Regards

Sreelakshmi Sasidharan (AdWords API Team)

unread,
Jan 11, 2019, 2:00:02 PM1/11/19
to AdWords API and Google Ads API Forum
Hello, 

You will first need to use the LabelService to create the label with the specific text. In your case with the date when you updated the max CPC as the text. Once that is done, you can use the mutateLabels() method of the AdGroupService or the AdGroupCriterionService depending on at which level you want to apply the label. Please check the guide on labels which explains this in detail. Code samples can be found here. Please give this a try and let me know if you have any additional questions. 
Reply all
Reply to author
Forward
0 new messages