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()
方法来 检索您的 产品广告。您可以使用 创建新的产品广告,方法是使用
newAdBuilder()的 ShoppingAdGroup。
遍历产品组层次结构
您可以使用 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);
}
}
选择特定产品组
您可以使用
productGroups 方法来选择产品组层次结构中的特定产品组,该方法是 AdsApp、ShoppingCampaign 或
ShoppingAdGroup 实例的方法。与遍历整个产品组层次结构相比,这种方法在选择特定产品组以进行出价管理时更简单。以下代码段展示了如何选择上个月点击次数超过 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();
同样,您可以使用 remove 方法来移除细分。ProductGroup这也会删除被移除产品组下的整个产品组层次结构。
脚本将确保在创建每个产品组后,产品组层次结构处于一致的状态,因此在更新产品组层次结构时,您无需创建或删除与“其他”对应的产品组。
“其他”产品组
购物产品组层次结构的每个级别都包含一个“其他”产品组,用于处理与您在产品组层次结构中创建的自定义条件不匹配的产品。您可以使用
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 报告,以帮助您跟踪
您的购物广告系列的效果。如需详细了解报告,请参阅我们的
报告指南。