購物廣告活動

Google Ads 腳本可管理部分購物廣告活動。您可以使用指令碼處理現有的購物廣告活動、建立及管理產品群組階層,以及執行購物報表。不過,您無法使用指令碼建立購物廣告活動、在廣告活動層級設定購物屬性 (例如:廣告活動優先順序、商品目錄篩選器等),或連結 Merchant Center 帳戶。

擷取購物廣告活動和廣告群組

購物廣告活動可透過 shoppingCampaigns AdsApp 物件的集合取得。您可以透過指令碼照常擷取這些值:

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 指令碼,透過 ads() 方法擷取產品廣告ShoppingAdGroup你可以使用 newAdBuilder() ShoppingAdGroup 方法建立新的產品廣告。

逐一查看產品群組階層

您可以使用 ShoppingAdGrouprootProductGroup 方法,存取產品群組階層的根目錄。然後,您可以使用 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);
  }
}

選取特定產品群組

您可以使用 AdsAppShoppingCampaignShoppingAdGroup 執行個體的 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();

同樣地,您可以使用 ProductGroupremove 方法移除細分。系統也會一併刪除要移除的產品群組底下的整個產品群組階層。

指令碼會確保建立每個產品群組後,產品群組階層維持一致狀態,因此更新產品群組階層結構時,您不需要建立或刪除對應「其他」的產品群組。

「所有其他項目」產品群組

購物產品群組階層的每個層級都包含「其他」產品群組,可處理不符合您在產品群組階層中建立的自訂條件的產品。您可以使用 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 腳本,透過 ShoppingCampaignnewAdGroupBuilder 方法建立新的購物廣告群組。建立 ShoppingAdGroup 後,您可以使用 createRootProductGroup 方法建立新的產品群組階層。

報表

Google Ads 腳本支援 product_group_viewshopping_performance_view 報表,有助於追蹤購物廣告活動的成效。如要進一步瞭解報表,請參閱報表指南