Belanja
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
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();
}
Menindaklanjuti setiap grup produk dalam hierarki
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);
}
}
Dapatkan 'Lainnya' grup produk
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;
}
Perbarui tawaran untuk grup produk
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);
}
}
Dapatkan iklan produk
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;
}
Buat iklan produk
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}"`);
}
}
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2024-09-10 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2024-09-10 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."]]],[]]