Google Ads scripts support the following ad extensions:
To learn more about ad extensions, visit the help center.
Google Ads scripts let you access supported ad extensions in your account. For example, the following code snippet accesses sitelinks:
const sitelinkIterator = AdsApp.extensions().sitelinks().get();
for (const sitelink of sitelinkIterator) {
// Do something with each sitelink
}
You can retrieve other supported ad extensions similarly using their respective iterators.
Creation
Google Ads scripts let you create supported ad extensions. For example, the following code snippet uses a phone number builder to create a phone number in your account:
const phoneNumberBuilder = AdsApp.extensions().newPhoneNumberBuilder();
const newPhoneNumber = phoneNumberBuilder
.withCountry("US")
.withPhoneNumber("6502530000")
.withCallOnly(false)
.build()
.getResult();
When build()
is called, the phone number is created in the account, but it
won't show alongside any ads just yet. You need to add it to a campaign or an
ad group first:
// Add a phone number to a campaign.
campaign.addPhoneNumber(newPhoneNumber);
// Add a phone number to an ad group.
adGroup.addPhoneNumber(newPhoneNumber);
Other supported ad extensions can be created and associated to campaigns or ad groups in the same way with their respective builders.
Getting stats
Google Ads scripts let you access stats for supported ad extensions at the account, campaign, or ad group level.
For example, to get sitelink stats:
// Account-level stats
// Get a sitelink in the account.
const sitelinkIterator = AdsApp.extensions().sitelinks().get();
const sitelink = sitelinkIterator.next();
const sitelinkStats = sitelink.getStatsFor("LAST_30_DAYS");
console.log(sitelinkStats.getClicks());
// Campaign-level stats.
// Get a sitelink in a campaign.
const campaignSitelinkIterator = campaign.extensions().sitelinks().get();
const campaignSitelink = campaignSitelinkIterator.next();
const campaignSitelinkStats = campaignSitelink.getStatsFor("LAST_30_DAYS");
console.log(campaignSitelinkStats.getClicks());
// Ad-group-level stats.
// Get a sitelink in an ad group.
const adGroupSitelinkIterator = adGroup.extensions().sitelinks().get();
const adGroupSitelink = adGroupSitelinkIterator.next();
const adGroupSitelinkStats = adGroupSitelink.getStatsFor("LAST_30_DAYS");
console.log(adGroupSitelinkStats.getClicks());
Stats for other supported ad extensions can be accessed in a similar way.
Modifying ad extensions
Existing supported ad extensions can be modified with Google Ads scripts. For example, the following code snippet will modify an existing sitelink:
// Get a sitelink in the account.
const sitelinkIterator = AdsApp.extensions().sitelinks().get();
const sitelink = sitelinkIterator.next();
console.log(sitelink.getLinkText()); // "original text"
// Get a sitelink from a campaign. Assume it's the same one as above.
const campaignSitelinkIterator = campaign.extensions().sitelinks().get();
const campaignSitelink = campaignSitelinkIterator.next();
console.log(campaignSitelink.getLinkText()); // "original text"
// Get a sitelink from an ad group. Assume it's the same one as above.
const adGroupSitelinkIterator = adGroup.extensions().sitelinks().get();
const adGroupSitelink = adGroupSitelinkIterator.next();
console.log(adGroupSitelink.getLinkText()); // "original text"
// Change the sitelink's link text. This change will affect all the campaigns
// and ad groups to which the sitelink belongs.
campaignSitelink.setLinkText("new link text");
// Same text!
console.log(campaignSitelink.getLinkText()); // "new link text"
console.log(adGroupSitelink.getLinkText()); // "new link text"
console.log(sitelink.getLinkText()); // "new link text"
The same concepts apply for other supported ad extensions.
Accessing ad-group-level ad extensions
Google Ads scripts let you access ad-group-level ad extensions. The following method call will return phone numbers that have been explicitly added to an ad group. Note that if phone numbers have been added to the campaign to which the ad group belongs, the following method call will not return them even when the phone numbers are eligible to appear on ads served from that ad group.
// This will return phone numbers that have been explicitly added to this
// ad group.
const adGroupPhoneNumberIterator = adGroup.extensions().phoneNumbers().get();
Other supported ad extensions can be accessed at the ad group level in a similar way.
Accessing account-level ad extensions
Google Ads scripts let you access account-level ad extensions. Callouts, mobile apps, and reviews can be added as account-level ad extensions, but account-level sitelinks and phone numbers are not available. The following method call will return callouts that have been explicitly added to your account.
// This will return callouts that have been explicitly added to your account.
const accountCalloutIterator =
AdsApp.currentAccount().extensions().callouts().get();
Account-level mobile apps and reviews can be accessed in a similar way.
Adding account-level ad extensions is similar to adding campaign-level or ad-group-level ad extensions. The following example demonstrates how to add an account-level callout extension:
// Create a new callout in the account. Without adding the new callout as an ad
// group, campaign or account extension, it won't actually serve.
const calloutBuilder = AdsApp.extensions().newCalloutBuilder();
const newCallout = calloutBuilder.withText("Sample Text").build().getResult();
// Add the new callout as an account-level extension. This enables it to serve
// for all campaigns in the account.
AdsApp.currentAccount().addCallout(newCallout);
Account-level mobile apps and reviews can be added in a similar way.
Removing campaign, ad group, and account ad extensions
Supported ad extensions can be removed from campaigns and ad groups at the account level. Google Ads scripts do not support removing ad extensions from an account all at once.
// Get a mobile app from a campaign.
const campaignMobileAppIterator = campaign.extensions().mobileApps().get();
const campaignMobileApp = campaignMobileAppIterator.next();
// Remove the mobile app.
campaign.removeMobileApp(campaignMobileApp);
// The mobile app still exists in the account and will be returned in the
// following iterator.
const mobileAppIterator = AdsApp.extensions().mobileApps().get();
Similarly, to remove an ad-group-level or account-level mobile app:
// Get a mobile app from an ad group.
const adGroupMobileAppIterator = adGroup.extensions().mobileApps().get();
const adGroupMobileApp = adGroupMobileAppIterator.next();
// Remove the mobile app.
adGroup.removeMobileApp(adGroupMobileApp);
// Get an account-level mobile app.
const accountMobileAppIterator =
AdsApp.currentAccount().extensions().mobileApps().get();
const accountMobileApp = accountMobileAppIterator.next();
// Remove the mobile app.
// Note that this removes the mobile app from the account level, so it will
// not serve as an account-level extension, but it will still exist in the
// account. It can still be added to an AdGroup or Campaign, or again as an
// account-level extension in the future.
AdsApp.currentAccount().removeMobileApp(accountMobileApp);
If all mobile apps are removed from a campaign, the campaign will no longer have a mobile app extension. To remove all mobile apps from your campaign, you need to retrieve the list of mobile apps for that campaign and remove them one at a time. The procedure is the same for other supported ad extensions.