在 Google Ads API 中,更新作業會使用欄位遮罩。欄位遮罩會列出您想透過更新變更的所有欄位,且系統會忽略任何不在欄位遮罩中的指定欄位,即使這些欄位已傳送至伺服器也是如此。
FieldMasks 公用程式
建議您使用內建的欄位遮罩公用程式 (FieldMasks
) 產生欄位遮罩,這樣您就能從修改過的物件產生欄位遮罩,而非從頭開始建立。
以下是更新廣告活動的範例:
my $campaign = Google::Ads::GoogleAds::V21::Resources::Campaign->new({
resourceName =>
Google::Ads::GoogleAds::V21::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
status => PAUSED,
networkSettings =>
Google::Ads::GoogleAds::V21::Resources::NetworkSettings->new({
targetSearchNetwork => "false"
})
});
my $campaign_operation =
Google::Ads::GoogleAds::V21::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::V21::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
、cpc_bid_ceiling_micros
和 cpc_bid_floor_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::V21::Resources::Campaign->new({
resourceName =>
Google::Ads::GoogleAds::V21::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
maximizeConversions =>
Google::Ads::GoogleAds::V21::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::V21::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::V21::Resources::Campaign->new({
resourceName => Google::Ads::GoogleAds::V21::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::V21::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::V21::Resources::Campaign->new({
resourceName => Google::Ads::GoogleAds::V21::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
maximizeConversions => Google::Ads::GoogleAds::V21::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::V21::Services::CampaignService::CampaignOperation->new({
update => $campaign,
updateMask => all_set_fields_of($campaign)
});
請注意,如果欄位含有巢狀子欄位,您只能清除每個個別子欄位,才能清除該欄位。如需瞭解詳情,請參閱「含有已定義子欄位的物件欄位」。