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