AdsApp.​AdSelector

Fetches ads in an account, except from shared libraries. Supports filtering and sorting.

Typical usage:

var adSelector = AdsApp
    .ads()
    .withCondition("metrics.impressions > 100")
    .forDateRange("LAST_MONTH")
    .orderBy("metrics.clicks DESC");

var adIterator = adSelector.get();
while (adIterator.hasNext()) {
  var ad = adIterator.next();
}
Related:

Methods:

MemberTypeDescription
forDateRange AdsApp.AdSelector Sets a predefined date range onto the selector.
forDateRange AdsApp.AdSelector Sets a custom date range onto the selector.
get AdsApp.AdIterator Fetches the requested ads and returns an iterator.
orderBy AdsApp.AdSelector Specifies the ordering of the resulting entities.
withCondition AdsApp.AdSelector Adds the specified condition to the selector in order to narrow down the results.
withIds AdsApp.AdSelector Restricts this selector to return only ads with the given ad IDs.
withLimit AdsApp.AdSelector Specifies limit for the selector to use.
withResourceNames AdsApp.AdSelector Restricts this selector to return only ads with the given Google Ads API resource names.

forDateRange(dateRange)

Sets a predefined date range onto the selector. Supported values:

TODAY, YESTERDAY, LAST_7_DAYS, LAST_14_DAYS, LAST_30_DAYS, LAST_BUSINESS_WEEK, LAST_WEEK_SUN_SAT, LAST_WEEK_MON_SUN, THIS_WEEK_MON_TODAY, THIS_WEEK_SUN_TODAY, LAST_MONTH, THIS_MONTH, ALL_TIME. Example:

selector.forDateRange("THIS_WEEK_SUN_TODAY");

Date range must be specified if the selector has conditions or ordering for a stat field. Note that only the last date range specified for the selector will take effect.

Arguments:

NameTypeDescription
dateRange String Date range to set onto the selector.

Return values:

TypeDescription
AdsApp.AdSelector The selector with date range applied.

forDateRange(dateFrom, dateTo)

Sets a custom date range onto the selector. Both parameters can be either an object containing year, month, and day fields, or an 8-digit string in YYYYMMDD form. For instance, March 24th, 2013 is represented as either {year: 2013, month: 3, day: 24} or "20130324". The date range is inclusive on both ends, so forDateRange("20130324", "20130324") sets the range of one day.

Date range must be specified if the selector has conditions or ordering for a stat field. Note that only the last date range specified for the selector will take effect.

Arguments:

NameTypeDescription
dateFrom Object Start date of the date range.
dateTo Object End date of the date range.

Return values:

TypeDescription
AdsApp.AdSelector The selector with date range applied.

get()

Fetches the requested ads and returns an iterator.

Return values:

TypeDescription
AdsApp.AdIterator Iterator of the requested ads.

orderBy(orderBy)

Specifies the ordering of the resulting entities. orderBy parameter can have one of the following forms:
  • orderBy("metrics.cost_micros") - orders results by metrics.cost_micros, in ascending order.
  • orderBy("metrics.ctr ASC") - orders results by metrics.ctr, in ascending order.
  • orderBy("ad_group_criterion.cpc_bid_micros DESC") - orders results by ad_group_criterion.cpc_bid_micros, in descending order.

See AdSelector.withCondition(String) for enumeration of columns that can be used.

orderBy() may be called multiple times. Consider the following example:

selector = selector.forDateRange("LAST_14_DAYS")
    .orderBy("metrics.clicks DESC")
    .orderBy("metrics.ctr ASC");

The results will be ordered by metrics.clicks in descending order. Results with equal metrics.clicks value will be ordered by metrics.ctr in ascending order.

If a stats column is used in the ordering, date range must be specified via AdSelector.forDateRange(String) or AdSelector.forDateRange(Object, Object).

LabelNames column cannot be used for ordering.

Arguments:

NameTypeDescription
orderBy String Ordering to apply.

Return values:

TypeDescription
AdsApp.AdSelector The selector with ordering applied.

withCondition(condition)

Adds the specified condition to the selector in order to narrow down the results.

Multiple conditions may be added to the same selector:

selector = selector.forDateRange("LAST_MONTH")
    .withCondition("metrics.clicks > 5")
    .withCondition("metrics.impressions > 100");
All specified conditions are AND-ed together. The above example will retrieve entities that observed over 100 metrics.impressions AND more than 5 clicks.

The parameter to be passed into this method must be of the following form:

"COLUMN_NAME OPERATOR VALUE"

Operators

The operator that can be used in a condition depends on the type of column.
  • For Integer and Long columns (e.g. metrics.clicks and metrics.impressions):
    <  <=  >  >=  =  !=
  • For Double columns (e.g. metrics.ctr):
    <  >
  • For String columns (e.g. campaign.name):
    =  !=  (NOT) (LIKE | CONTAINS | REGEXP_MATCH)
  • For Enumeration columns (ones that can only take one value from a predefined list, such as Status):
    =  !=  IN ()  NOT IN ()
  • For StringSet columns (e.g. campaign.labels):
    CONTAINS ALL ()  CONTAINS ANY ()  CONTAINS NONE ()
Conditions using IN, NOT IN, CONTAINS ALL, CONTAINS ANY and CONTAINS NONE operators look as follows:
withCondition("resource.column_name IN (Value1, Value2)")

Columns

All column names are case-sensitive, and so are all values of enumerated columns (such as Status).

Column Type Example
Stats
metrics.average_cpc Double withCondition("metrics.average_cpc < 1.45")
metrics.average_cpm Double withCondition("metrics.average_cpm > 0.48")
metrics.average_cpv Double withCondition("metrics.average_cpv < 0.23")
metrics.average_page_views Double withCondition("metrics.average_page_views > 0")
metrics.bounce_rate Double withCondition("metrics.bounce_rate < 0.5")
metrics.clicks Long withCondition("metrics.clicks >= 21")
metrics.conversions_from_interactions_rate Double withCondition("metrics.conversions_from_interactions_rate > 0.1")
metrics.conversions Long withCondition("metrics.conversions <= 4")
metrics.cost_micros Double withCondition("metrics.cost_micros > 4480000"). The value is specified in micros. E.g. $4.48 = 4480000.
metrics.ctr Double withCondition("metrics.ctr > 0.01"). Note that metrics.ctr is returned in 0..1 range, so 5% metrics.ctr is represented as 0.05.
metrics.impressions Long withCondition("metrics.impressions != 0")
Ad attributes
ad_group_ad.status Enumeration: ENABLED, PAUSED, REMOVED withCondition("ad_group_ad.status = PAUSED")
ad_group_ad.ad.type Enumeration: EXPANDED_TEXT_AD, GMAIL_IMAGE_AD, GMAIL_SINGLE_PROMOTION_AD, GMAIL_MULTI_PRODUCT_AD, HTML5_AD, IMAGE_AD, MOBILE_AD, MOBILE_IMAGE_AD, PRODUCT_AD, RESPONSIVE_DISPLAY_AD, RICH_MEDIA_AD, TEMPLATE_AD, TEXT_AD withCondition("ad_group_ad.ad.type NOT IN (IMAGE_AD, RICH_MEDIA_AD)")
ad_group_ad.ad.text_ad.headline String withCondition("ad_group_ad.ad.text_ad.headline REGEXP_MATCH '.*leather shoes.*'")
ad_group_ad.ad.text_ad.description1 String withCondition("ad_group_ad.ad.text_ad.description1 REGEXP_MATCH 'Leather.*'")
ad_group_ad.ad.display_url String withCondition("ad_group_ad.ad.display_url REGEXP_MATCH 'www.example.com.*'")
ad_group_ad.ad.final_urls StringSet withCondition("ad_group_ad.ad.final_urls CONTAINS ALL ('https://www.example.com')")
ad_group.name String withCondition("ad_group.name REGEXP_MATCH '.*shoes.*'")
ad_group.status Enumeration: ENABLED, PAUSED, REMOVED withCondition("ad_group.status = ENABLED"). Use to fetch ads from only ENABLED ad groups.
campaign.name String withCondition("campaign.name REGEXP_MATCH '.*promotion.*'")
campaign.status Enumeration: ENABLED, PAUSED, REMOVED withCondition("campaign.status = ENABLED"). Use to fetch ads from only ENABLED campaigns.
ad_group_ad.ad.device_preference Enumeration: MOBILE, ALL withCondition("ad_group_ad.ad.device_preference = MOBILE"). Use to fetch only mobile-preferred ads.

If a stats column is used in the condition, date range must be specified via AdSelector.forDateRange(String) or AdSelector.forDateRange(Object, Object).

Arguments:

NameTypeDescription
condition String Condition to add to the selector.

Return values:

TypeDescription
AdsApp.AdSelector The selector with the condition applied.

withIds(ids)

Restricts this selector to return only ads with the given ad IDs.

All ads are uniquely identified by the combination of their ad group ID and ad ID. The IDs for this selector are thus represented as two-element arrays, with the first element being the ad group ID and the second being the ad ID:

var adIds = [
  ['12345', '987987'],
  ['23456', '876876'],
  ['34567', '765765'],
];
selector = selector.withIds(adIds);
In cases where the ad group ID is already known, the IDs for this selector can then be just an array of ad IDs. Any provided ad group ID and ad ID combination will override the embedded ad group ID. For instance, the following will select the ads with the given ad IDs in the given ad group:
var ids = ['12345', '23456', '34567'];
var ads = adGroup.ads().withIds(ids);

The resulting selector can be further refined by applying additional conditions to it. The ID-based condition will then be AND-ed together with all the other conditions, including any other ID-based conditions. So, for instance, the following selector:

var ids1 = [
  ['12345', '987987'],
  ['23456', '876876'],
  ['34567', '765765'],
];
var ids2 = [
  ['34567', '765765'],
  ['45678', '654654'],
  ['56789', '543543'],
];
AdsApp.ads()
   .withIds(ids1)
   .withIds(ids2);
will only get the ad with ID ['34567', '765765'], since it would be the only ad that satisfies both ID conditions.

Arguments:

NameTypeDescription
ids Object[][] Array of ad IDs.

Return values:

TypeDescription
AdsApp.AdSelector The selector restricted to the given IDs.

withLimit(limit)

Specifies limit for the selector to use. For instance, withLimit(50) returns only the first 50 entities.

Arguments:

NameTypeDescription
limit int How many entities to return.

Return values:

TypeDescription
AdsApp.AdSelector The selector with limit applied.

withResourceNames(resourceNames)

Restricts this selector to return only ads with the given Google Ads API resource names.
const adResourceNames = [
  'customers/1234567890/adGroupAds/111~222',
  'customers/1234567890/adGroupAds/111~333',
  'customers/1234567890/adGroupAds/444~555',
];
selector = selector.withResourceNames(adResourceNames);

The resulting selector can be further refined by applying additional conditions to it. The resource name condition will then be AND-ed together with all the other conditions.

The selector can only support up to 10,000 resource names. If more than 10,000 resource names are specified, the corresponding get() call will fail with a runtime error.

Arguments:

NameTypeDescription
resourceNames String[] Array of ad resource names.

Return values:

TypeDescription
AdsApp.AdSelector The selector restricted to the given resource names.