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
方法,選取產品群組階層中的特定產品群組。如果您選取特定產品群組用於出價管理,這個做法會比周遊整個產品群組階層來得簡單。透過下列程式碼片段,瞭解如何選取上個月點擊超過五次且點閱率高於 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
報表,可協助您追蹤購物廣告活動的成效。如要進一步瞭解回報功能,請參閱報告指南。