Selectors

Stay organized with collections Save and categorize content based on your preferences.

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:

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, Google Ads 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". By default, all selectors will set the limit to 50,000. You can increase the limit by manually specifying a limit.

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("metrics.clicks DESC")
    .orderBy("metrics.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("metrics.clicks > 10");
campaignSelector.withCondition("metrics.impressions > 1000");
campaignSelector.orderBy("metrics.impressions DESC");
campaignSelector.forDateRange("YESTERDAY");

can be re-written in a more compact fashion:

var campaignSelector = AdsApp.campaigns()
  .withCondition("metrics.clicks > 10")
  .withCondition("metrics.impressions > 1000")
  .orderBy("metrics.impressions DESC")
  .forDateRange("YESTERDAY");

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

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