Ad Groups
Stay organized with collections
Save and categorize content based on your preferences.
Add an ad group
function addAdGroup(campaignName, adGroupName, defaultCpc = 1.2) {
const campaignIterator = AdsApp.campaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
if (!campaignIterator.hasNext()) {
throw new Error(`No campaign with name "${campaignName} found`);
}
const campaign = campaignIterator.next();
return campaign.newAdGroupBuilder()
.withName(adGroupName)
.withCpc(defaultCpc)
.build();
}
Get all ad groups
function getAllAdGroups() {
// AdsApp.adGroups() will return all ad groups that are not removed by
// default.
const adGroupIterator = AdsApp.adGroups().get();
console.log('Total adGroups found : ' + adGroupIterator.totalNumEntities());
return adGroupIterator;
}
Get an ad group by name
function getAdGroupByName(name) {
const adGroupIterator = AdsApp.adGroups()
.withCondition(`ad_group.name = "${name}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${name}" found`);
}
const adGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
}
return adGroup;
}
Update an ad group's default CPC bid
function setAdGroupCpc(name, cpc) {
const adGroupIterator = AdsApp.adGroups()
.withCondition(`ad_group.name = "${name}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${name}" found`);
}
const adGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
}
adGroup.bidding().setCpc(cpc);
}
Get an ad group's stats
function getAdGroupStats(name, dateRange = 'LAST_MONTH') {
const adGroupIterator = AdsApp.adGroups()
.withCondition(`ad_group.name = "${name}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${name}" found`);
}
const adGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
}
// You can get stats for a custom date range, or, as in this example, a predefined date range.
// A list of valid predefined date ranges is available at
// https://developers.google.com/google-ads/api/docs/query/date-ranges#predefined_date_range
const stats = adGroup.getStatsFor(dateRange);
console.log(`${adGroup.getName()}, ${stats.getClicks()}, ${stats.getImpressions()}`);
return stats;
}
Pause an ad group
function pauseAdGroup(name) {
const adGroupIterator = AdsApp.adGroups()
.withCondition(`ad_group.name = "${name}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${name}" found`);
}
const adGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
}
adGroup.pause();
}
Get an ad group's device bid modifiers
function getAdGroupBidModifiers(name) {
const adGroupIterator = AdsApp.adGroups()
.withCondition(`ad_group.name = "${name}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${name}" found`);
}
const adGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
}
return {
HighEndMobile: adGroup.devices().getMobileBidModifier(),
Tablet: adGroup.devices().getTabletBidModifier(),
Desktop: adGroup.devices().getDesktopBidModifier(),
};
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-03-14 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2022-03-14 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-03-14 UTC."]]