Adding Labels with Adwords API
The Adwords API is a beast, although it is well documented it can take a while to wrap your head around it. Release v201406 of the API added support to manage Labels, this was further improved in v201409. Labels are a great tool to add metadata to Adwords Keywords, they can help in organizing, filtering and performing bulk actions on the the Adwords interface.
Labels can be applied to keywords, campaigns, ad groups, and ads, which also enables you to see how the custom categories you create are performing relative to each other and to the entities in your account. Labels provide an easy filtering mechanism also on the Adwords interface as seen in the screenshot below -
More information on Labels can be found in this post. We here at crealytics use camato to manage large Adwords accounts and use Labels to effectively manage data. This blog post provides some guidance to add Labels to Keywords programmatically with Ruby, since this feature of the API is thinly documented I believe this post might help some developers out there.
On a web browser this might look something like this -
This post makes a few assumptions -
- You have basic Adwords API knowledge.
- You have signed-up for the API and have the necessary credentials, in Ruby we usually put these in a YAML file.
- You have OAuth2 tokens to work with the API.
So lets start with the first class, which helps us read the static API config -
Using the static config and merging it with our account specific OAuth config, we can create an Adwords service builder -
Using this service builder we can create a Label management service -
The Labels class provides a find_or_create method which first looks for a label by its text in an Adwords account and if it is not found creates one. For Label creation the operation in the code above is label_service.mutate which uses the Google Adwords API Ruby gem to interact with the LabelService. Adwords API operates primarily as a SOAP service, however dealing with SOAP requests / response is clunky so we use the Ruby gem which provides a nice abstraction over raw SOAP calls for us.
Now that we have created a Label, let us associate it with a Keyword -
To associate a Label with a Keyword, we use the AdGroupCriterionService with the mutate_label operation. We also have a Keyword model which we create to manage the keywords downloaded from Adwords.
The sample (Rails) Keyword model can look like this -
So the controller’s job is as simple as doing -
That is it, using the excellent Adwords API gem we have been able to create Labels and associate them with Keywords elegantly. The main gotchas are to build the services correctly, call the correct operations and pass them with the appropriate parameters. Hope the code above will help some developers out there, the same principles can be used to interact with Adwords API with other programming languages such as Python / Java.