字段掩码

在 Google Ads API 中,更新是使用字段掩码完成的。字段掩码会列出您打算通过更新更改的所有字段,并且系统会忽略未包含在字段掩码中的任何指定字段,即使这些字段已发送到服务器也是如此。

FieldMasks 实用程序

建议使用我们内置的字段掩码实用程序 (FieldMasks) 生成字段掩码,这样您就可以通过修改后的对象生成字段掩码,而无需从头开始构建。

以下是更新广告系列的示例:

use Google::Ads::GoogleAds::Utils::FieldMasks qw(all_set_fields_of field_mask);

my $campaign =
  Google::Ads::GoogleAds::V26::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V26::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    status => "PAUSED",
    networkSettings =>
      Google::Ads::GoogleAds::V26::Resources::NetworkSettings
      ->new({
        targetSearchNetwork => "false",
      }),
  });

my $campaign_operation =
  Google::Ads::GoogleAds::V26::Services::CampaignService::CampaignOperation
  ->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign),
  });

此示例首先使用 ResourceNames 实用程序设置 Campaign 对象的资源名称,以便 API 知道正在更新哪个广告系列。

该示例对广告系列使用 FieldMasks::all_set_fields_of() 方法,以自动生成一个枚举所有已设置字段的字段掩码。然后,您可以将返回的掩码直接传递给更新调用。

FieldMasks::all_set_fields_of() 是 FieldMasks::field_mask() 的便捷方法。它会将您传递的对象与同一类的空对象进行比较。在上述代码中,您还可以使用:

field_mask(
  Google::Ads::GoogleAds::V26::Resources::Campaign->new({}),
  $campaign
)

而非 all_set_fields_of($campaign)。

手动创建遮罩

如需从头开始创建字段掩码,您需要先创建一个 Google::Ads::GoogleAds::Common::FieldMask 对象,然后创建一个数组引用,并在其中填充您打算更改的所有字段的名称,最后将该数组引用分配给字段掩码的 paths 字段:

my $field_mask = Google::Ads::GoogleAds::Common::FieldMask->new({
  paths => ["status", "name"],
});

更新对象字段及其子字段

对象字段可以有子字段(例如 MaximizeConversions,其中包含 target_cpa_micros 等子字段),也可以完全没有子字段(例如 ManualCpm)。

未定义子字段的对象字段

Perl 中的对象字段等效于在 gRPC 上运行的客户端库中的 protobuf MESSAGE。更新未定义任何子字段的对象字段时,请使用 FieldMasks 实用程序生成字段掩码,如上一部分所述。

具有已定义子字段的对象字段

更新使用子字段定义的对象字段时,如果未在该消息中明确设置任何子字段,则必须手动将每个可变对象子字段添加到 FieldMask,这与前面从头开始创建字段掩码的示例类似。

一个常见示例是更新广告系列的出价策略,但未在新出价策略中设置任何字段。以下示例演示了如何更新广告系列以使用 MaximizeConversions 出价策略,而无需在出价策略中设置任何子字段。

在这种情况下,使用 FieldMasks 实用程序的 all_set_fields_of() 和 field_mask() 方法无法实现预期目标。

以下示例会生成一个包含 maximize_conversions 的字段掩码。不过,Google Ads API 不允许此行为,以防止意外清除字段,并会产生 FieldMaskError.FIELD_HAS_SUBFIELDS 错误。

# Creates a campaign with the proper resource name and an empty
# MaximizeConversions field.
my $campaign =
  Google::Ads::GoogleAds::V26::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V26::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V26::Resources::MaximizeConversions
      ->new(),
  });

# Constructs an operation, using the FieldMasks' all_set_fields_of utility to
# derive the update mask. The field mask includes 'maximize_conversions',
# which produces a FieldMaskError.FIELD_HAS_SUBFIELDS error.
my $campaign_operation =
  Google::Ads::GoogleAds::V26::Services::CampaignService::CampaignOperation
  ->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign),
  });

# Sends the operation in a mutate request that results in a
# FieldMaskError.FIELD_HAS_SUBFIELDS error because empty object fields cannot
# be included in a field mask.
my $response = $api_client->CampaignService()->mutate({
  customerId => $customer_id,
  operations => [$campaign_operation],
});

以下示例演示了如何正确更新广告系列,以使用 MaximizeConversions 出价策略,而不设置其任何子字段。

# Creates a campaign with the proper resource name.
my $campaign =
  Google::Ads::GoogleAds::V26::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V26::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
  });

# Creates a field mask from the existing campaign and adds the mutable subfield
# 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 sets the campaign's bidding strategy to a
# MaximizeConversions object with none of its subfields set.
# 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.
#
# See the guide on assigning standard and portfolio bidding strategies
# (/google-ads/api/docs/campaigns/bidding/assign-strategies).
my $field_mask = all_set_fields_of($campaign);
push @{$field_mask->{paths}}, "maximize_conversions.target_cpa_micros";

# Creates an operation to update the campaign with the specified fields.
my $campaign_operation =
  Google::Ads::GoogleAds::V26::Services::CampaignService::CampaignOperation
  ->new({
    update     => $campaign,
    updateMask => $field_mask,
  });

清除字段

您可以通过将字段添加到字段掩码中(如上例所示)或将字段设置为一个空值或未定义的值来显式清除字段。例如,假设您有一个广告系列使用 MaximizeConversions 出价策略,并且 target_cpa_micros 字段设置的值大于 0。

# Creates a campaign with the proper resource name and a MaximizeConversions
# object with target_cpa_micros set to 0.
my $campaign =
  Google::Ads::GoogleAds::V26::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V26::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V26::Resources::MaximizeConversions
      ->new({
        targetCpaMicros => 0,
      }),
  });

# Constructs an operation using all_set_fields_of to derive the update mask,
# which includes 'maximize_conversions.target_cpa_micros'.
my $campaign_operation =
  Google::Ads::GoogleAds::V26::Services::CampaignService::CampaignOperation
  ->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign),
  });

请注意,具有嵌套子字段的字段只能通过清除每个单独的子字段来清除,如具有已定义子字段的对象字段中所示。