字段掩码

在 Google Ads API 中,更新是使用字段掩码完成的。字段掩码列出 您打算随更新而更改的所有字段,以及任何指定的字段 字段掩码范围外的其他字符将被忽略,即使发送到服务器也是如此。

FieldMasks 实用程序

推荐使用我们的内置字段掩码生成字段掩码 实用 (FieldMasks), 这让您可以根据修改的对象生成字段掩码,而不是构建 从头开始

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

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

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

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

该示例使用 FieldMasks::all_set_fields_of() 方法,以自动生成一个字段掩码, set 字段。然后,您可以将返回的掩码直接传递给 update 调用。

FieldMasks::all_set_fields_of() 是一种便捷方法, FieldMasks::field_mask()。 它会将传递的对象与同一类的空对象进行比较。因此,在 也可以使用

field_mask(Google::Ads::GoogleAds::V17::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_microscpc_bid_ceiling_microscpc_bid_floor_micros);或 也可以完全没有参数(例如 ManualCpm)。

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

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

包含已定义子字段的对象字段

更新使用子字段定义且未明确显示的对象字段时 设置消息的任何子字段,则必须手动添加每个 将 mutable 对象子字段添加到 FieldMask,类似于上面的示例 从头开始创建字段掩码。

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

在本例中,使用 all_set_fields_of()field_mask() 方法的 FieldMasks 实用程序无法实现预期目标。

以下示例将生成包含以下内容的字段掩码: 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::V17::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V17::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V17::Resources::MaximizeConversions->new()
    });

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

# Sends the operation in a mutate request that will result 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::V17::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V17::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    )
  });

# 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.
# 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
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::V17::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::V17::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V17::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    ),
    maximizeConversions => Google::Ads::GoogleAds::V17::Resources::MaximizeConversions->new({
      targetCpaMicros => 0
    })
  });

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

请注意,带有嵌套子字段的字段只能通过清除 各个子字段,如定义了 子字段