在 Google Ads API 中,更新作業會使用欄位遮罩完成。欄位遮罩清單 所有您想要變更更新的欄位,以及任何指定欄位 並忽略不在欄位遮罩中,即使已傳送至伺服器也是如此。
FieldMaskUtil
如要產生欄位遮罩,建議您使用內建欄位遮罩 公用程式,可讓您透過修改的物件產生欄位遮罩,而非 從頭開始建構
以下是更新廣告活動的範例:
// Creates a Campaign object with the proper resource name and any other changes.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// allSetFieldsOf utility to derive the update mask. This mask tells the Google
// Ads API which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
這個範例會先建立一個空白的 Campaign
物件,然後設定其資源
名稱讓 API 知道要更新哪個廣告活動。
這個範例在廣告活動中使用 FieldMasks.allSetFieldsOf()
方法:
自動產生欄位遮罩來列舉所有集合欄位。接著
將傳回的遮罩直接傳遞至更新呼叫。
若您需要使用現有物件來更新幾個欄位,則可以 請依下列方式修改程式碼:
Campaign existingCampaign;
// Obtains existingCampaign from elsewhere.
...
// Creates a new campaign based off the existing campaign and updates the
// campaign by setting its status to paused.
Campaign campaignToUpdate =
existingCampaign.toBuilder()
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// compare utility to derive the update mask. This mask tells the Google Ads API
// which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaignToUpdate)
.setUpdateMask(FieldMasks.compare(existingCampaign, campaignToUpdate))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
如要從頭建立欄位遮罩,您必須先建立
FieldMask
敬上
物件,然後針對要更改為變更內容的每個欄位加入名稱
物件。
FieldMask fieldMask =
FieldMask.newBuilder()
.addPaths("status")
.addPaths("name")
.build();
更新訊息欄位及其子欄位
MESSAGE
欄位可包含子欄位 (例如
MaximizeConversions
,其中包含三個:
target_cpa_micros
,
cpc_bid_ceiling_micros
和 cpc_bid_floor_micros
),或者沒有
(例如 ManualCpm
)。
未定義子欄位的訊息欄位
如要更新尚未以任何子欄位定義的 MESSAGE
欄位,請使用
FieldMaskUtil 用於產生欄位遮罩,如上所述。
含有已定義子欄位的訊息欄位
更新不含子欄位定義的 MESSAGE
欄位時
明確設定該訊息的任何子欄位,您必須手動
將每個可變動 MESSAGE
子欄位新增至 FieldMask
,如下所示:
上述範例會從頭建立欄位遮罩。
有一個常見的例子,就是更新廣告活動的出價策略時,並未設定任何
新出價策略欄位的部分。以下範例說明如何
更新廣告活動
MaximizeConversions
出價策略
不必在出價策略中設定任何子欄位
在這種情況下,請使用 allSetFieldsOf()
和 compare()
FieldMaskUtil 無法達到預期目標。
以下範例產生的欄位遮罩
maximize_conversions
。但 Google Ads API 不允許此行為
可避免不小心清除欄位,
FieldMaskError.FIELD_HAS_SUBFIELDS
敬上
錯誤。
// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(MaximizeConversions.newBuilder().build())
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. The field mask will include 'maximize_conversions`,
// which will produce a FieldMaskError.FIELD_HAS_SUBFIELDS error.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that will result in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
以下範例說明如何正確更新廣告活動,以便使用
MaximizeConversions
出價策略,但未設定任何子欄位。
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Creates a field mask from the existing campaign and adds all of the mutable
// fields (only one in this case) on the MaximizeConversions bidding strategy to
// the field mask. Because this field is included in the field mask but
// excluded from the campaign object, the Google Ads API will set the campaign's
// bidding strategy to a MaximizeConversions object without any of its subfields
// set.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
// Only include 'maximize_conversions.target_cpa_micros' in the field mask
// as it is the only mutable subfield on MaximizeConversions when used as a
// standard bidding strategy.
//
// Learn more about standard and portfolio bidding strategies here:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask)
.build();
清除欄位
部分欄位可以明確清除。與上述範例類似,您必須
將這些欄位明確新增至欄位遮罩中。舉例來說
採用「MaximizeConversions
」出價策略的廣告活動,且獲得
「target_cpa_micros
」欄位的值必須大於 0。
接著程式碼就會執行。不過,maximize_conversions.target_cpa_micros
不會加到欄位遮罩中,也不會變更
target_cpa_micros
欄位:
// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(
MaximizeConversions.newBuilder().setTargetCpa(0).build())
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask will NOT include
// 'maximize_conversions.target_cpa_micros'.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that will succeed but will NOT update
// the 'target_cpa_micros' field because
// 'maximize_conversions.target_cpa_micros' was not included in the field mask.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
下一個範例說明如何正確清除 target_cpa_micros
] 欄位。MaximizeConversions
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Constructs a field mask from the existing campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which will
// clear this field from the bidding strategy without impacting any other fields
// on the bidding strategy.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask))
.build();
請注意,即使上述範例對
在 Google Ads API 中定義為 optional
protocol buffers
。
但自從
target_cpa_micros
敬上
不是 optional
欄位,「不正確」範例「不會」更新
出價策略,以便清除「target_cpa
」欄位。