使用 AdsApp 的最高成效廣告活動

Google Ads 腳本可管理部分最高成效廣告活動。您可以使用指令碼擷取最高成效廣告活動、管理素材資源群組及執行報表,但無法使用指令碼建立最高成效廣告活動。如要執行更進階的作業,請參閱本指南的其餘部分,瞭解如何使用 mutate 採取更通用的做法。

擷取最高成效廣告活動

最高成效廣告活動可透過 performanceMaxCampaigns 物件的 AdsApp 集合取得。您可以照常擷取這些檔案:

const campaignName = "My Performance Max campaign";

const campaignIterator = AdsApp.performanceMaxCampaigns()
    .withCondition(`campaign.name = "${campaignName}"`)
    .get();

for (const campaign of campaignIterator) {
  ...
}

與其他廣告活動類型不同,最高成效廣告活動沒有可供查看的廣告群組或廣告物件;系統會根據您設定的素材資源群組,自動處理其他廣告活動中與這些概念相關的所有事項。

素材資源和素材資源群組

最高成效廣告活動的廣告會使用素材資源,例如影片、圖片、廣告標題和說明,這些素材資源可能是您提供,也可能是系統自動生成。如要全面瞭解必要素材資源類型,請參閱 Google Ads API 最高成效素材資源指南

最高成效廣告活動的素材資源會綁定成素材資源群組,且每個最高成效廣告活動至少要有一個素材資源群組。您無法直接在指令碼中建立這些素材資源群組,但可以新增及移除現有素材資源群組中的素材資源。

在素材資源群組中新增素材資源

首先,請建立資產:

const imageUrl = "http://www.example.com/example.png";
const imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
const assetOperation = AdsApp.adAssets().newImageAssetBuilder()
   .withName("new asset name")
   .withData(imageBlob)
   .build();
const imageAsset = assetOperation.getResult();

接著,使用剛才建立的素材資源,將其新增至現有素材資源群組:

// First, fetch the Performance Max campaign we want to operate on.
const campaignIterator = AdsApp.performanceMaxCampaigns()
   .withCondition(`campaign.name = '${campaignName}'`)
   .get();
let campaign;
if (campaignIterator.hasNext()) {
   campaign = campaignIterator.next();
} else {
   throw `No campaign found with name ${campaignName}.`
}

// Then, get that campaign's asset groups.
const assetGroupIterator = campaign.assetGroups().get();

// The campaign must have at least one asset group, so we can just assume so here.
const assetGroup = assetGroupIterator.next();

// Add the asset from the previous step.
assetGroup.addAsset(imageAsset, 'MARKETING_IMAGE');

請注意,您必須在最後一個步驟中指定資產類型。如需素材資源類型的完整清單,請參閱 Google Ads API 說明文件

如要使用現有資產,請先建立資產選取器:

const assetSelector = AdsApp.adAssets().assets();

然後使用 withCondition 篩選器,縮小範圍,找出要操作的素材資源。如需完整的篩選器選項清單,請參閱 AssetSelector 參考說明文件。

最後,擷取疊代器並進行疊代,就像處理其他實體一樣:

const assetIterator = assetSelector.get();

for (const asset of assetIterator) {
   ...
}

文字素材資源

文字素材資源的運作方式稍有不同,您不需要預先製作素材資源。您只需要指定文字,系統就會自動建立素材資源。如果文字與現有文字素材資源完全相同,系統會重複使用現有素材資源。

舉例來說,以下說明如何建立廣告標題素材資源:

assetGroup.addAsset('asset text here', 'HEADLINE');

從素材資源群組中移除素材資源

你也可以從素材資源群組中移除素材資源,但請注意,廣告活動必須有特定種類的最低素材資源數量,才能有效放送。

以下說明如何移除上一個範例中新增的資產:

assetGroup.removeAsset(imageAsset, 'MARKETING_IMAGE');

您也可以使用 search 函式,取得特定素材資源群組中的素材資源清單:

// The resource name is a unique identifier for this asset group.
const assetGroupName = assetGroup.getResourceName();
results = AdsApp.search(
   `SELECT asset.resource_name, asset_group_asset.field_type
    FROM asset_group_asset
    WHERE asset_group.resource_name = '${assetGroupName}'`
);

這會選取資產的資源名稱做為專屬 ID。您也可以選取其他欄位,例如 asset.typeasset.text_asset.text,進一步篩選結果。使用這份報表類型的查詢建立工具,自行撰寫查詢。

取得目標資產後,請對素材資源群組呼叫 remove,從素材資源群組中移除資產:

// Let's assume at least one asset is returned. We'll just remove the first
// asset, whatever it is. In your code, customize this to choose the right asset.
const row_info = results.next().asset;
assetGroup.remove(row_info.asset.resource_name, row_info.asset_group_asset.field_type);