欄位遮罩

在 Google Ads API 中,欄位遮罩用於提供 API 要求應更新的欄位清單。即使傳送至伺服器,系統也會忽略欄位遮罩中未指定的任何欄位。

FieldMasks 類別

建議您在 .NET 用戶端程式庫中使用內建的 FieldMasks 公用程式類別產生欄位遮罩,這樣就能從修改過的物件產生欄位遮罩,不必從頭建構。

以下範例說明如何更新廣告活動,使用 FieldMasks.AllSetFieldsOf 方法產生列舉所有設定欄位的欄位遮罩。然後,您可以將產生的欄位遮罩直接傳遞至更新呼叫:

// 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 });

有時您可能需要使用現有物件,並更新幾個欄位。在這種情況下,請改用 FieldMasks.FromChanges 方法。這個方法會產生欄位遮罩,代表兩個物件之間的差異:

Campaign existingCampaign;

// Obtain existingCampaign from an earlier API call.

// Create a new campaign based on 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)
};

處理 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()
};

CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};

MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

這項 API 呼叫會失敗,並顯示 FieldMaskError.FIELD_HAS_SUBFIELDS 錯誤。由於 MaximizeConversions 含有子欄位,Google Ads API 伺服器會預期要求中含有可變動子欄位的欄位遮罩。不過,由於要求未設定任何子欄位,因此 FieldMasks 無法在此情況下自動產生子欄位遮罩。

在這種情況下,您可以手動將路徑新增至 fieldMask.Paths (這是 RepeatedField<string>):

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
};

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 at:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");

// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = fieldMask
};

清除欄位

Google Ads API 支援清除部分欄位值。如要清除欄位,您必須手動將該欄位納入欄位遮罩,同時在資源物件上取消設定該欄位。將欄位設為預設值 (例如 int64 欄位的 0) 不會清除欄位。

以下程式碼範例說明如何清除 MaximizeConversions 出價策略的 target_cpa_micros 欄位:

正確代碼

下列程式碼會清除 target_cpa_micros 欄位,因為程式碼會將 maximize_conversions.target_cpa_micros 新增至欄位遮罩,但不會設定 campaign.MaximizeConversions.TargetCpaMicros 屬性:

// 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
// clears this field from the bidding strategy without impacting any other
// fields on the bidding strategy.
FieldMask fieldMask = FieldMasks.AllSetFieldsOf(campaign);
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");

// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = fieldMask
};

驗證碼有誤

下列程式碼不會清除 target_cpa_micros 欄位,因為該程式碼會將欄位設為 0。如果 TargetCpaMicros 為 0,或路徑從遮罩中省略,FieldMasks 公用程式和 Google Ads API 伺服器都會忽略這個值,且伺服器不會傳回錯誤:

// Creates a campaign with the proper resource name and a
// MaximizeConversions object. Attempts to clear the target_cpa_micros
// field by setting it to 0.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    MaximizeConversions = new MaximizeConversions()
    {
        TargetCpaMicros = 0
    }
};

// Constructs an operation using FieldMasks.AllSetFieldsOf to derive the
// update mask.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};

// Sends the operation in a mutate request that succeeds without clearing
// the previous 'target_cpa_micros' value cleanly.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });