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
方法移除子區域。這麼做也會刪除移除的產品群組下方的整個產品群組階層。
建立每個產品群組後,指令碼會確保產品群組階層處於一致狀態,因此您在更新產品群組階層結構時,不必建立或刪除對應「其他」的產品群組。
「所有其他項目」產品群組
購物產品群組階層會在每個層級中包含「其他」(「Other」) 產品群組,用於處理不符合您在產品群組階層中建立的自訂條件產品。您可以使用 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
報表,協助您追蹤購物廣告活動的成效。如要進一步瞭解檢舉功能,請參閱檢舉指南。