在 Google Ads API 中,更新作業會使用欄位遮罩完成。欄位遮罩會列出您要隨更新變更的所有欄位,而不在欄位遮罩中的指定欄位也會遭到忽略,即使已傳送至伺服器也是如此。
FieldMaskUtil
如要產生欄位遮罩,建議您使用內建的欄位遮罩公用程式,透過修改的物件產生欄位遮罩,而不必從頭開始建構。
以下是更新廣告活動的範例:
// Update campaign by setting its status to paused, and "Search network" to false.
Campaign campaignToUpdate = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
NetworkSettings = new NetworkSettings()
{
TargetSearchNetwork = false
}
};
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Update = campaignToUpdate,
UpdateMask = FieldMasks.AllSetFieldsOf(campaignToUpdate)
};
// Update the campaign.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
首先,建立空白的 Campaign
物件。接著,我們會設定資源名稱
讓 API 明確知道要更新哪個廣告活動
此範例在廣告活動中使用 FieldMasks.AllSetFieldsOf
方法,會自動產生列舉所有組合欄位的欄位遮罩。接著,您可以將傳回的遮罩直接傳遞至更新呼叫。
您有時可能需要使用現有物件,並更新一些欄位。在這種情況下,您可以按照以下方式修改程式碼:
Campaign existingCampaign;
// Obtain existingCampaign from elsewhere.
...
// Create a new campaign based off the existing campaign for update.
Campaign campaignToUpdate = new Campaign(existingCampaign);
// Update campaign by setting its status to paused, and "Search network" to
// false.
campaignToUpdate.Status = CampaignStatus.Paused;
campaignToUpdate.NetworkSettings = new NetworkSettings()
{
TargetSearchNetwork = false
}
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Update = campaignToUpdate,
UpdateMask = FieldMasks.FromChanges(existingCampaign, campaignToUpdate)
};
如要從頭開始建立欄位遮罩,您必須先建立 FieldMask
物件,然後使用您要變更的所有欄位名稱填入陣列,最後將陣列內容附加至欄位遮罩的 Path
欄位。
FieldMask fieldMask = new FieldMask();
fieldMask.Paths.AddRange(new string[] { "status", "name" });
更新訊息欄位及其子欄位
MESSAGE
欄位可以有子欄位 (例如 MaximizeConversions
,其中包含三個:target_cpa_micros
、cpc_bid_ceiling_micros
和 cpc_bid_floor_micros
),或者也可以完全沒有子欄位 (例如 ManualCpm
)。
未定義子欄位的訊息欄位
更新未在任何子欄位中定義的 MESSAGE
欄位時,請使用 FieldMasks
產生欄位遮罩,如上所述。
含有已定義子欄位的訊息欄位
更新使用子欄位定義的 MESSAGE
欄位時,並未明確設定該訊息中的任何子欄位,您必須手動將每個「可變動」MESSAGE
子欄位新增至 FieldMask
,與上述從頭開始建立欄位遮罩的範例類似。
其中一個常見例子是在不設定新出價策略的任何欄位的情況下,更新廣告活動的出價策略。以下範例說明如何將廣告活動更新為使用 MaximizeConversions
出價策略,而不在出價策略中設定任何子欄位。
在此情況下,使用 FieldMasks
的 AllSetFieldsOf()
和 FromChanges()
方法無法達到預期目標。
以下範例會產生包含 maximize_conversions
的欄位遮罩。但 Google Ads API 不允許這個行為,以免意外清除欄位並產生 FieldMaskError.FIELD_HAS_SUBFIELDS
錯誤。
// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
MaximizeConversions = new MaximizeConversions()
};
// 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 = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
// 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 = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
下例示範如何在不設定任何子欄位的情況下,正確更新廣告活動來使用 MaximizeConversions
出價策略。
// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
};
// Creates a field mask from the existing campaign and adds all of the fields
// on the MaximizeConversions bidding strategy to the field mask. Because these
// fields are 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 with none of its subfields set.
FieldMask fieldMask = FieldMasks.AllSetFieldsOf(campaign);
// 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
fieldMask.Paths.AddRange(new string[] {
"maximize_conversions.target_cpa_micros",
});
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
清除欄位
部分欄位可以明確清除。與上述範例類似,您必須明確將這些欄位新增至欄位遮罩中。舉例來說,假設您有廣告活動採用 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 = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
MaximizeConversions = new MaximizeConversions()
{
TargetCpaMicros = 0
}
};
// 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 = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
// 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 = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
下一個範例說明如何正確清除 MaximizeConversions
出價策略的 target_cpa_micros
欄位。
// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
};
// 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);
fieldMask.Paths.AddRange(new string[] {
"maximize_conversions.target_cpa_micros",
});
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
請注意,上述「錯誤」範例對 Google Ads API protocol buffers
中定義為 optional
的欄位有效。但由於 target_cpa_micros
不是 optional
欄位,因此「錯誤」範例不會更新出價策略,藉此清除 target_cpa
欄位。