在 Google Ads API 中,更新作業是透過欄位遮罩完成。欄位遮罩 (google.protobuf.FieldMask) 包含 snake_case 中的欄位路徑清單,您打算透過更新變更這些路徑。如果指定欄位不在欄位遮罩中,即使傳送至伺服器也會遭到忽略。
FieldMasks 公用程式
建議您在 Java 用戶端程式庫中使用內建的 FieldMasks 公用程式類別產生欄位遮罩,這樣就能從修改過的物件產生欄位遮罩,不必從頭建構。
以下是更新廣告活動的範例:
// 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 updates 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(),自動產生列舉所有已設定欄位的欄位遮罩。您可以將傳回的遮罩直接傳遞至更新呼叫。
如要處理現有物件並更新幾個欄位,請使用 FieldMasks.compare(),如下所示:
// Assumes existingCampaign was retrieved from a previous API call.
// Creates a new campaign based on 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 updates 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
建構工具,並新增您要變更的每個欄位的 snake_case 名稱:
FieldMask fieldMask =
FieldMask.newBuilder()
.addPaths("status")
.addPaths("name")
.build();
更新訊息欄位及其子欄位
MESSAGE 欄位可以有子欄位 (例如 MaximizeConversions,其中包含 target_cpa_micros、cpc_bid_ceiling_micros 和 cpc_bid_floor_micros),也可以沒有子欄位 (例如 ManualCpm)。
沒有定義子欄位的訊息欄位
更新未定義任何子欄位的 MESSAGE 欄位時,請使用 FieldMasks 公用程式產生欄位遮罩,如上一節所述。
定義子欄位的訊息欄位
更新已定義子欄位的 MESSAGE 欄位時,如果未明確設定該訊息的任何子欄位,您必須手動將每個可變動的 MESSAGE 子欄位新增至 FieldMask,類似於從頭建立欄位遮罩。
常見的例子是更新廣告活動的出價策略 (oneof 欄位
campaign_bidding_strategy),但未設定新出價策略的任何欄位。以下範例說明如何更新廣告活動,以使用 MaximizeConversions 出價策略,而不設定出價策略的任何子欄位。
在本案例中,單獨使用 FieldMasks 的 allSetFieldsOf() 和 compare() 和 compare() 方法無法達成預期目標。
以下範例會產生包含 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 FieldMasks.allSetFieldsOf to derive the update
// mask. The field mask includes 'maximize_conversions', which produces 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 results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields with
// subfields cannot be included directly 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 campaign and adds the mutable subfield
// ('maximize_conversions.target_cpa_micros') on the MaximizeConversions
// bidding strategy to the field mask. Because this subfield is included in the
// field mask while excluded from the campaign object, the Google Ads API
// switches the campaign's bidding strategy oneof to MaximizeConversions with
// target_cpa_micros unset.
FieldMask fieldMask =
FieldMasks.allSetFieldsOf(campaign).toBuilder()
.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 不會如預期清除:
// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0L.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(
MaximizeConversions.newBuilder().setTargetCpaMicros(0L).build())
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation using FieldMasks.allSetFieldsOf to derive the
// update mask.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that does not clear the field
// cleanly.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
下一個範例說明如何正確清除 MaximizeConversions 出價策略中的 target_cpa_micros 欄位。
// 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 campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which
// clears 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();