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 报告,可帮助您跟踪购物广告系列的效果。如需详细了解报告,请参阅我们的报告指南。