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