Thursday, November 7, 2013

Getting to know AWQL: It’s like SQL for AdWords

If you want to get your campaign management data using the AdWords API, AWQL (AdWords Query Language) is a great choice. AWQL offers an SQL-like syntax for setting up your fields, ordering, paging, and predicates all in one simple string. AWQL is mainly used for reports, but is also supported with some services.

Services Supported

There are currently five services that support AWQL: CampaignService, CampaignCriterionService, AdGroupService, AdGroupCriterionService, and AdGroupAdService.

Here is a Java code comparison that finds the IDs and names of ad groups within a specific campaign. Using the standard methods, you have to set fields, ordering, paging, and predicates separately.
Selector selector = new Selector();
selector.setFields(new String[] {"Id", "Name"});
selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
selector.setPaging(new Paging(offset, PAGE_SIZE));
Predicate campaignIdPredicate =
    new Predicate("CampaignId", PredicateOperator.IN, new String[] {campaignId.toString()});
selector.setPredicates(new Predicate[] {campaignIdPredicate});
AdGroupPage page = adGroupService.get(selector);
The same task can be done more simply using AWQL, as shown below.
String query = 
    "SELECT Id, Name" + 
    " WHERE CampaignId IN [%d]" + 
    " ORDER BY Name ASC" + 
    " LIMIT %d, %d";
query = String.format(query, campaignId, offset, PAGE_SIZE);
AdGroupPage page = adGroupService.query(query);
We also have plans to add support for AWQL to more services in upcoming releases.

Reporting Features

Within AdHoc reports, AWQL also supports the unique DURING clause, which is an easy way to filter results in a given date range. For example, if you were interested in finding campaigns with few impressions during October, you could run a query like this.
SELECT CampaignId, CampaignName, Clicks, Impressions
FROM CAMPAIGN_PERFORMANCE_REPORT
WHERE Impressions < 100
DURING 20131001,20131031
Remember that stats are only available via AdHoc reports.

We recommend using AWQL as an expressive, easy way to retrieve data from your AdWords campaigns. Please feel free to ask any questions on the AdWords API forum or on our Google+ page.