Google Ads 脚本可用于管理购物广告系列。您可以使用脚本处理现有购物广告系列、创建和管理商品组层次结构,以及生成购物报告。不过,您无法使用脚本制作购物广告系列、在广告系列一级设置购物属性(例如广告系列优先级、商品目录过滤条件等)或关联 Merchant Center 账号。
检索购物广告系列和广告组
您可以通过 AdsApp
对象的 shoppingCampaigns
集合获取购物广告系列。您可以照常通过脚本检索它们:
const campaignName = "My first shopping campaign";
const campaignIterator = AdsApp.shoppingCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
for (const campaign of campaignIterator) {
...
}
检索广告系列后,您可以以类似的方式获取其广告组。只有在您需要同时对广告系列及其广告组执行操作时,才应采用这种方法。
const adGroupIterator = campaign.adGroups()
.withCondition(`ad_group.name = "${adGroupName}"`)
.get();
for (const adGroup of adGroupIterator) {
...
}
如果您只打算对特定广告组执行操作,则可以使用 AdsApp.shoppingAdGroups()
方法提取广告组,而无需先提取广告系列:
const adGroupIterator = AdsApp.shoppingAdGroups()
.withCondition(`campaign.name = "${campaignName}"`)
.withCondition(`ad_group.name = "${adGroupName}"`)
.get();
for (const adGroup of adGroupIterator) {
...
}
产品广告
借助 Google Ads 脚本,您可以使用 ShoppingAdGroup
的 ads()
方法检索商品广告。您可以使用 ShoppingAdGroup
的 newAdBuilder()
方法制作新的商品广告。
遍历产品组层次结构
您可以使用 ShoppingAdGroup
的 rootProductGroup
方法访问产品组层次结构的根。然后,您可以使用 children
方法迭代子产品组并遍历产品组层次结构。每个节点都是一个 ProductGroup
对象,您可以使用 getDimension
方法来确定商品组的实际类型。您还可以使用相应的转换方法(例如 asBrand
)将其转换为更具体的类型(例如 ProductBrand
)。以下代码段展示了如何递归遍历产品组层次结构。
walkTree(shoppingAdGroup.rootProductGroup(), 1);
function walkTree(root, level) {
// Logger.log(root.getDimension());
let description = "";
switch (root.getDimension()) {
case "ROOT":
description = "Root";
break;
case "CATEGORY":
description = root.asCategory().getName();
break;
case "BRAND":
description = root.asBrand().getName();
break;
// Handle more types here.
...
}
if (root.isOtherCase()) {
description = "Other";
}
const padding = new Array(level + 1).join('-');
console.log("%s, %s, %s, %s, %s, %s",
padding,
description,
root.getDimension(),
root.getMaxCpc(),
root.isOtherCase(),
root.getId().toFixed());
const children = root.children().get();
for (const child of children) {
walkTree(child, level + 1);
}
}
选择特定产品组
您可以使用 AdsApp
、ShoppingCampaign
或 ShoppingAdGroup
实例的 productGroups
方法,在产品组层次结构中选择特定的产品组。与出于出价管理目的选择特定商品组时遍历整个商品组层次结构相比,这种方法更为简单。以下代码段展示了如何选择在过去一个月内获得的点击次数超过 5 次且点击率高于 0.01
的所有商品组,并将其出价提高 0.01
。
function main() {
const productGroups = AdsApp.productGroups()
.withCondition("metrics.clicks > 5")
.withCondition("metrics.ctr > 0.01")
.forDateRange("LAST_MONTH")
.get();
for (const productGroup of productGroups) {
productGroup.setMaxCpc(productGroup.getMaxCpc() + 0.01);
}
}
更新产品组层次结构
您可以使用现有商品组的 newChild
方法将子商品组添加到该商品组。这会为您提供一个 ProductGroupBuilderSpace
对象,您可以使用该对象构建适当的产品组。以下代码段会在根目录下为“cardcow”品牌添加一个细分,然后进一步细分为新品和翻新品。
const root = shoppingAdGroup.rootProductGroup();
// Add a brand product group for a "cardcow" under root.
const brandProductGroup = root.newChild()
.brandBuilder()
.withName("cardcow")
.withBid(1.2)
.build()
.getResult();
// Add new conditions for New and Refurbished cardcow brand items.
const newItems = brandProductGroup.newChild()
.conditionBuilder()
.withCondition("New")
.withBid(1.5)
.build()
.getResult();
// Refurbished items will use the bid from "cardcow" product group.
const refurbishedItems = brandProductGroup.newChild()
.conditionBuilder()
.withCondition("Refurbished")
.build()
.getResult();
同样,您可以使用 ProductGroup
的 remove
方法移除分区。此操作还会删除要移除的产品组下的整个产品组层次结构。
脚本会确保在创建每个产品组后,产品组层次结构处于一致状态,因此您在更新产品组层次结构时无需创建或删除与“其他所有产品”对应的产品组。
“其他”产品组
购物产品组层次结构在每个级别都包含一个“其他所有产品”(“其他”)产品组,用于处理与您在产品组层次结构中创建的自定义条件不匹配的商品。您可以使用 isOtherCase
方法来区分您添加的普通产品组和“其他”产品组。
以下代码段会检索根商品组层次结构下的“其他”商品组,并输出其出价。
const root = shoppingAdGroup.rootProductGroup();
const childProductGroups = root.children().get();
let everythingElseProductGroupFound = false;
for (const childProductGroup of childProductGroups) {
if (childProductGroup.isOtherCase()) {
console.log("'Everything else' product group found. Type of the " +
"product group is %s and bid is %s.",
childProductGroup.getDimension(),
childProductGroup.getMaxCpc());
everythingElseProductGroupFound = true;
break;
}
}
if (!everythingElseProductGroupFound) {
console.log("No 'Everything else' product group found under root " +
"product group.");
}
当您细分叶级产品组时,脚本会自动创建一个“其他”产品组,以确保产品组层次结构保持有效。“其他”产品组会继承父级产品组的出价。
创建新的购物广告组
借助 Google Ads 脚本,您可以使用 ShoppingCampaign
的 newAdGroupBuilder
方法创建新的购物广告组。创建 ShoppingAdGroup
后,您可以使用其 createRootProductGroup
方法创建新的商品组层次结构。
报告
Google Ads 脚本支持 product_group_view
和 shopping_performance_view
报告,可帮助您跟踪购物广告系列的效果。如需详细了解举报功能,请参阅我们的举报指南。