在 Google Ads API 中,系統會使用欄位遮罩完成更新;欄位遮罩會列出要以更新方式變更的所有欄位,且系統會忽略不在欄位遮罩中的任何指定欄位,即使傳送至伺服器也一樣。
FieldmasUtil
如要產生欄位遮罩,建議您使用我們內建的欄位遮罩公用程式,以便您從已修改的物件產生欄位遮罩,而不必從頭開始建立。
以下範例說明如何更新廣告活動:
// 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
出價策略,而無須在出價策略中設定任何子欄位。
在這種情況下,FieldFieldUtil 的 allSetFieldsOf()
和 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 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));
下一個範例說明如何正確清除 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 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 protocol buffers
中定義為 optional
的欄位。不過,由於 target_cpa_micros
不是 optional
欄位,因此「不正確」範例並不會更新出價策略,以清除 target_cpa
欄位。