Selectors

Selectors help the programmer construct the query that fetches desired AdWords entities. With selectors, one can narrow down the list of retrieved entities and order it. Most selectors have the following methods:

withCondition()
adds a condition to a selector. If multiple conditions are used, they are AND-ed together, in other words, the selector will only return entities that satisfy all of the specified conditions.
withIds()
adds a collection of IDs as a condition. An ID-based condition will be AND-ed together with all the others.
forDateRange()
is needed when a condition or ordering clause references a Stats field, such as Ctr or Impressions. If you request all campaigns with over 100 impressions, AdWords scripts will need to know the date range to look into.
orderBy()
specifies the ordering of the returned entities.
withLimit()
limits the number of returned entities to the specified value. It is particularly useful in conjunction with orderBy() in order to fetch things like "10 keywords with most impressions yesterday".

These methods can be called in any order. One exception is orderBy(), where order of calls indeed matters: multiple calls to this method will specify multiple ordering clauses, and they will apply in order. Consider the following snippet:

selector = selector.forDateRange("LAST_14_DAYS")
    .orderBy("Clicks DESC")
    .orderBy("CTR ASC");

The results will be ordered by Clicks in descending order. Results with equal Clicks values will be ordered by Ctr in ascending order.

Calls to a selector's methods can be chained together. The following code

var campaignSelector = AdWordsApp.campaigns();
campaignSelector.withCondition("Clicks > 10");
campaignSelector.withCondition("Impressions > 1000");
campaignSelector.orderBy("Impressions DESC");
campaignSelector.forDateRange("YESTERDAY");

can be re-written in a more compact fashion:

var campaignSelector = AdWordsApp.campaigns()
  .withCondition("Clicks > 10")
  .withCondition("Impressions > 1000")
  .orderBy("Impressions DESC")
  .forDateRange("YESTERDAY");

Once the selector is constructed, one can obtain an Iterator from it by calling selector.get().

Some selectors (e.g. AdParamSelector and LabelSelector) expose fewer methods since they operate on entities that are more restricted (don't have any stats or meaningful fields to order by).

Read Best Practices for tips and tricks on efficient selector usage.

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.