Page Summary
-
This guide explains how to create, retrieve, and report on various Google Ads types using Google Ads scripts.
-
Scripts can create ads using the
newAd()method onAdGroupinstances, which returns anAdBuilderSpacefor building different ad types. -
To access fields specific to an ad's type, use the
asType()method to create anAdViewSpace, but ensure the ad's type is known to avoid errors. -
The
ad_group_adview can be used in reporting to query type-specific ad fields alongside regular statistics.
Google Ads supports a variety of ad types, such as text, image, and mobile ads. This guide covers how to create, retrieve, and report on ads using Google Ads scripts. For an overview of all ad types supported by Google Ads, see the API guide.
Creation
Scripts can create ads using the
newAd()
method on
AdGroup
instances. This returns an
AdBuilderSpace
that creates builders for
supported ad types.
The following snippet demonstrates how to create a responsive search ad:
let adOperation = adGroup.newAd().responsiveSearchAdBuilder()
.withHeadlines(["Headline 1", "Headline 2", "Headline 3"])
.withDescriptions(["Description 1", "Description 2"])
.withFinalUrl("http://www.example.com")
.withPath1("path1")
.withPath2("path2")
.build();
Inspection
Some information associated with all ad types is immediately available from an
Ad, such as an
ad's ID and approval status. Additionally, any ad can be paused, enabled, or
removed.
To access fields specific to an ad's type, such as a responsive search ad's
headlines, use the
asType()
method to create an
AdViewSpace.
This provides access to an extended version of the
Ad that exposes
type-specific methods.
The following snippet gets the headlines of every responsive search ad:
const iterator = AdsApp.ads().withCondition("Type = RESPONSIVE_SEARCH_AD").get();
while (iterator.hasNext()) {
let ad = iterator.next();
let responsiveSearchAd = ad.asType().responsiveSearchAd();
let headlines = responsiveSearchAd.getHeadlines();
}
Notice that the condition Type = RESPONSIVE_SEARCH_AD ensures every ad from
the iterator is a responsive search ad. Attempting to view an ad with an
incorrect type will result in an error that stops your script's execution, so
it's important to view type-specific fields only when an ad's type is known.
The following snippet shows how to determine if an ad is the correct type
using the
Ad.isType()
method:
if (ad.isType().responsiveSearchAd()) {
let responsiveSearchAd = ad.asType().responsiveSearchAd();
let headlines = responsiveSearchAd.getHeadlines();
let descriptions = responsiveSearchAd.getDescriptions();
}
Reporting
The ad_group_ad view can also be
used to query type-specific ad fields in addition to regular stats, such as
ad_group_ad.ad.type.
The following snippet shows how to retrieve the stats for all responsive search
ads:
const results = AdsApp.search(
"SELECT ad_group_ad.ad_group.id, " +
"ad_group_ad.ad.id, " +
"metrics.clicks, " +
"metrics.impressions, " +
"metrics.cost " +
"FROM ad_group_ad " +
"WHERE ad_group_ad.ad.type = 'RESPONSIVE_SEARCH_AD' " +
"AND segments.date DURING LAST_7_DAYS");
while (results.hasNext()) {
let row = results.next();
let adId = row.adGroupAd.ad.id;
let clicks = row.metrics.clicks;
...
}
See the reports guide for more information on reporting in scripts.