字段掩码

在 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 方法,会自动生成枚举所有已设置字段的字段掩码。然后,您可以将返回的掩码直接传递给 update 调用。

有时,您可能需要使用现有对象,并更新几个字段。在这种情况下,您应按如下方式修改代码:

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_microscpc_bid_ceiling_microscpc_bid_floor_micros),也可以根本没有子字段(例如 ManualCpm)。

未定义子字段的消息字段

更新未使用任何子字段定义的 MESSAGE 字段时,请使用 FieldMasks 生成字段掩码,如上所述。

包含已定义子字段的消息字段

如果更新使用子字段定义的 MESSAGE 字段,但没有为该消息明确设置任何子字段,则必须手动将每个可变的 MESSAGE 子字段添加到 FieldMask,类似于上述从头开始创建字段掩码的示例。

一个常见的示例是,在未针对新出价策略设置任何字段的情况下更新广告系列的出价策略。以下示例演示了如何在不设置任何出价策略子字段的情况下,更新广告系列以使用 MaximizeConversions 出价策略。

在这种情况下,使用 FieldMasksAllSetFieldsOf()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 字段。