欄位遮罩

在 Google Ads API 中,系統會使用欄位遮罩來更新資料。欄位遮罩會列出您要隨更新變更的所有欄位,並且忽略任何不在欄位遮罩中的指定欄位,即使傳送至伺服器也一樣。

FieldMask 公用程式

產生欄位遮罩的建議做法是使用內建欄位遮罩公用程式 (FieldMasks),可讓您透過已修改的物件產生欄位遮罩,而不必從頭開始建構。

以下舉例說明如何更新廣告活動:

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

my $campaign_operation =
  Google::Ads::GoogleAds::V16::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::V16::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 中的物件欄位相當於在 gRPC 上執行的用戶端程式庫中的 protobuf MESSAGE。如果更新未以任何子欄位定義的物件欄位,請使用 FieldMasks 公用程式產生欄位遮罩,如上所述。

含已定義子欄位的物件欄位

在未明確設定訊息上任何子欄位的情況下更新透過子欄位定義的物件欄位時,您必須手動將每個可變動物件子欄位新增至 FieldMask,類似上方會從頭開始建立欄位遮罩的例子。

更新廣告活動出價策略的常見例子之一,就是沒有為新出價策略設定任何欄位。以下範例說明如何更新廣告活動,以便使用 MaximizeConversions 出價策略,但無須在出價策略中設定任何子欄位。

在這種情況下,使用 FieldMask 公用程式的 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::V16::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V16::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V16::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::V16::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::V16::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V16::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::V16::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::V16::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V16::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    ),
    maximizeConversions => Google::Ads::GoogleAds::V16::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::V16::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign)
  });

請注意,如要清除含有巢狀子欄位的欄位,您必須清除每個個別子欄位,如含有已定義子欄位的物件欄位中所示。