在 Google Ads API 中,更新是使用字段掩码完成的。字段掩码列出 您打算随更新而更改的所有字段,以及任何指定的字段 字段掩码范围外的其他字词,即使发送到服务器,也会被忽略。
FieldMaskUtil
推荐使用我们的内置字段掩码生成字段掩码 实用程序,它可让您通过修改的对象(而不是 从零开始构建它们
以下是一个更新广告系列的示例:
// 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()
方法
自动生成枚举所有已设置字段的字段掩码。然后,您可以
将返回的掩码直接传递给 update 调用。
如果您需要使用现有对象更新一些字段,可以 按如下所示修改代码:
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
出价策略
而不必为出价策略设置任何子字段。
在本例中,使用 allSetFieldsOf()
和 compare()
方法的
FieldMaskUtil 未达到预期目标。
以下示例将生成包含以下内容的字段掩码:
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));
下一个示例演示了如何正确清除 target_cpa_micros
字段(MaximizeConversions
出价策略)。
// 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 中定义为 optional
protocol buffers
。
但自从使用
target_cpa_micros
不是 optional
字段,则“不正确”示例不会更新
清除 target_cpa
字段。