Shopping
Stay organized with collections
Save and categorize content based on your preferences.
function getAllShoppingCampaigns() {
// AdsApp.shoppingCampaigns() will return all campaigns that are not removed
// by default.
const campaignIterator = AdsApp.shoppingCampaigns().get();
console.log(`Total shopping campaigns found : ${
campaignIterator.totalNumEntities()}`);
return campaignIterator;
}
function getShoppingCampaignByName(shoppingCampaignName) {
const campaignIterator = AdsApp.shoppingCampaigns()
.withCondition(`campaign.name = "${shoppingCampaignName}"`)
.get();
if (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
console.log(`Campaign Name: ${campaign.getName()}`);
console.log(`Enabled: ${campaign.isEnabled()}`);
console.log(`Bidding strategy: ${campaign.getBiddingStrategyType()}`);
console.log(`Ad rotation: ${campaign.getAdRotationType()}`);
console.log(`Start date: ${formatDate(campaign.getStartDate())}`);
console.log(`End date: ${formatDate(campaign.getEndDate())}`);
return campaign;
} else {
throw new Error(
`No shopping campaign named "${shoppingCampaignName}" found`);
}
}
function formatDate(date) {
function zeroPad(number) {
return Utilities.formatString('%02d', number);
}
return (date == null) ?
'None' :
zeroPad(date.year) + zeroPad(date.month) + zeroPad(date.day);
}
function getShoppingAdGroupByName(shoppingAdGroupName) {
const adGroupIterator = AdsApp.shoppingAdGroups()
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
}
const shoppingAdGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
}
return shoppingAdGroup;
}
function createElectronicsShoppingAdGroup() {
// This example snippet assumes a user has a shopping campaign named
// 'Shopping' and creates a new ad group named 'Electronics' in the campaign.
// Please customize the snippet to suit your use case.
const shoppingCampaignName = 'Shopping';
const newAdGroupName = 'Electronics';
const shoppingCampaign = AdsApp.shoppingCampaigns()
.withCondition(`campaign.name = "${shoppingCampaignName}"`)
.get()
.next();
const adGroupOperation = shoppingCampaign.newAdGroupBuilder()
.withName(newAdGroupName)
.withCpc(0.75)
.build();
if (adGroupOperation.isSuccessful()) {
const adGroup = adGroupOperation.getResult();
console.log(`Successfully created ad group "${
adGroup.getName()}" in campaign "${adGroup.getCampaign().getName()}"`);
} else {
const errors = adGroupOperation.getErrors();
console.error(`Creation failed with errors: ${errors}`);
throw new Error(`Failed to create ad group "${
newAdGroupName}" in campaign "${shoppingCampaignName}"`);
}
}
function createElectronicsProductGroups() {
// This example snippet assumes a user has a shopping campaign named
// 'Shopping' that includes an ad group named 'Electronics'. Please customize
// the product group hierarchy to suit your use case.
const shoppingCampaignName = 'Shopping';
const shoppingAdGroupName = 'Electronics';
const shoppingAdGroup = AdsApp.shoppingAdGroups()
.withCondition(`campaign.name = "${shoppingCampaignName}"`)
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get()
.next();
const rootProductGroup = shoppingAdGroup.rootProductGroup();
// The created product group hierarchy will be
// - root
// - 'Cardcow' brand
// - New condition
// - Refurbished condition
// - Other conditions
// - Other brands
// Add a brand product group for 'Cardcow' under the root product group.
const brandNode = rootProductGroup.newChild()
.brandBuilder()
.withName('Cardcow')
.withBid(1.2)
.build()
.getResult();
// Add groups for new and refurbished Cardcow brand items.
const newItems = brandNode.newChild()
.conditionBuilder()
.withCondition('NEW')
.build()
.getResult();
const refurbishedItems = brandNode.newChild()
.conditionBuilder()
.withCondition('REFURBISHED')
.withBid(0.9)
.build()
.getResult();
}
Act on each product group in the hierarchy
function actOnAllElectronicsProductGroups() {
// This example snippet assumes a user has a hierarchy of product groups under
// an ad group named 'Electronics' in a shopping campaign named 'Shopping'. It
// applies the function 'actOnProductGroupAndChildren' to each product group
// in the hierarchy. Please customize the 'actOnProductGroupAndChildren'
// function to suit your specific use case.
const shoppingCampaignName = 'Shopping';
const shoppingAdGroupName = 'Electronics';
const shoppingAdGroup = AdsApp.shoppingAdGroups()
.withCondition(`campaign.name = "${shoppingCampaignName}"`)
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get()
.next();
const rootProductGroup = shoppingAdGroup.rootProductGroup();
actOnProductGroupAndChildren(rootProductGroup, 0);
}
function actOnProductGroupAndChildren(productGroup, level) {
// This example function logs descriptive information about the given
// productGroup and all children of the given productGroup. Please customize
// the function to suit your particular use case.
let description = '';
if (productGroup.isOtherCase()) {
description = 'Other';
} else if (productGroup.getDimension() == 'CATEGORY') {
description = productGroup.asCategory().getName();
} else {
description = productGroup.getValue();
}
// Note: Child product groups may not have a max cpc if it has been excluded.
const padding = new Array(level + 1).join('-');
console.log(
'%s %s, %s, %s, %s, %s', padding, description,
productGroup.getDimension(), productGroup.getMaxCpc(),
productGroup.isOtherCase(), productGroup.getId().toFixed());
for (const childProductGroup of productGroup.children()) {
actOnProductGroupAndChildren(childProductGroup, level + 1);
}
}
Get the 'Everything else' product group
function getEverythingElseProductGroupForAdGroup(shoppingAdGroupName) {
const adGroupIterator = AdsApp.shoppingAdGroups()
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
}
const shoppingAdGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
}
const rootProductGroup = shoppingAdGroup.rootProductGroup();
for (const childProductGroup of rootProductGroup.children()) {
if (childProductGroup.isOtherCase()) {
// Note: Child product groups may not have a max cpc if it has been
// excluded.
console.log(
`"Everything else" product group found. Type of the product group is ${
childProductGroup.getDimension()} and bid is ${
childProductGroup.getMaxCpc()}`);
return childProductGroup;
}
}
console.warn(
'"Everything else" product group not found under root product group.');
return null;
}
Update bids for product groups
function updateVariousProductGroupBids() {
// This example snippet modifies the bids of some product groups based on
// criteria. Please modify the snippet to suit your use case.
const productGroups = AdsApp.productGroups()
.withCondition('Clicks > 5')
.withCondition('Ctr > 0.01')
.forDateRange('LAST_MONTH')
.get();
for (const productGroup of productGroups) {
productGroup.setMaxCpc(productGroup.getMaxCpc() + 0.01);
}
}
Get product ads
function getProductAdsInShoppingAdGroup(shoppingAdGroupName) {
const adGroupIterator = AdsApp.shoppingAdGroups()
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get();
if (!adGroupIterator.hasNext()) {
throw new Error(`No ad group with name "${shoppingAdGroupName}" found`);
}
const shoppingAdGroup = adGroupIterator.next();
if (adGroupIterator.totalNumEntities() > 1) {
console.warn(`Multiple ad groups named "${shoppingAdGroupName}" found.
Using the one from campaign "${shoppingAdGroup.getCampaign().getName()}"`);
}
const productAdIterator = shoppingAdGroup.ads().get();
console.log(`Ad Group "${shoppingAdGroup.getName()}" has ${
productAdIterator.totalNumEntities()} ads`);
return productAdIterator;
}
Create product ads
function createElectronicsProductAd() {
// This example snippet assumes a user has a shopping campaign named
// 'Shopping' that includes an ad group named 'Electronics'. Please customize
// the snippet to suit your use case.
const shoppingCampaignName = 'Shopping';
const shoppingAdGroupName = 'Electronics';
const shoppingAdGroup = AdsApp.shoppingAdGroups()
.withCondition(`campaign.name = "${shoppingCampaignName}"`)
.withCondition(`ad_group.name = "${shoppingAdGroupName}"`)
.get()
.next();
const adOperation =
shoppingAdGroup.newAdBuilder().withMobilePreferred(true).build();
if (adOperation.isSuccessful()) {
const productAd = adOperation.getResult();
console.log(`Successfully created product ad in ad group "${
productAd.getAdGroup().getName()}"`);
} else {
const errors = adOperation.getErrors();
console.error(`Creation failed with errors: ${errors}`);
throw new Error(
`Failed to create product ad in ad group "${shoppingAdGroupName}"`);
}
}
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.
[[["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."],[[["The provided code snippets demonstrate how to manage Shopping campaigns and their components (ad groups, product groups, and ads) within Google Ads using scripts."],["You can retrieve all Shopping campaigns, access specific campaigns and ad groups by name, and create new ad groups within existing campaigns."],["It is possible to define a product group hierarchy within a Shopping ad group and perform actions on each individual product group or update their bids in bulk."],["The snippets showcase ways to fetch existing Product Ads within a specified Shopping ad group, as well as create new Product Ads with specific settings, like mobile preference."],["The code leverages the Google Ads Scripts API to interact with various entities and elements within the platform, enabling automated management of Shopping campaign structures."]]],[]]