Selectors help the programmer construct the query that fetches desired Google Ads entities. With selectors, one can narrow down the list of retrieved entities and order it. Most selectors have the following methods:
- 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.
- adds a collection of IDs as a condition. An ID-based condition will be AND-ed together with all the others.
- 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, Google Ads scripts will need to know the date range to look into.
- specifies the ordering of the returned entities.
- 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 = AdsApp.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 = AdsApp.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
Read Best Practices for tips and tricks on efficient selector usage.