ใน Google Ads API การอัปเดตจะทําโดยใช้มาสก์ช่อง หน้ากากฟิลด์จะแสดงรายการช่องทั้งหมดที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต และระบบจะไม่สนใจฟิลด์ที่ระบุซึ่งไม่ได้อยู่ในหน้ากากฟิลด์ แม้ว่าจะส่งไปยังเซิร์ฟเวอร์แล้วก็ตาม
ยูทิลิตี FieldMasks
วิธีสร้างมาสก์ฟิลด์ที่แนะนําคือการใช้ยูทิลิตีมาสก์ฟิลด์ในตัว (FieldMasks
) ซึ่งจะช่วยให้คุณสร้างมาสก์ฟิลด์จากออบเจ็กต์ที่แก้ไขแล้วได้โดยไม่ต้องสร้างตั้งแต่ต้น
ตัวอย่างการอัปเดตแคมเปญมีดังนี้
my $campaign = Google::Ads::GoogleAds::V19::Resources::Campaign->new({
resourceName =>
Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
status => PAUSED,
networkSettings =>
Google::Ads::GoogleAds::V19::Resources::NetworkSettings->new({
targetSearchNetwork => "false"
})
});
my $campaign_operation =
Google::Ads::GoogleAds::V19::Services::CampaignService::CampaignOperation->new({
update => $campaign,
updateMask => all_set_fields_of($campaign)
});
ตัวอย่างนี้จะสร้างออบเจ็กต์ Campaign
ก่อนโดยการตั้งชื่อทรัพยากรโดยใช้ยูทิลิตี ResourceNames
เพื่อให้ API ทราบว่ากําลังอัปเดตแคมเปญใด
ตัวอย่างนี้ใช้เมธอด FieldMasks::all_set_fields_of()
ในแคมเปญเพื่อสร้างมาสก์ฟิลด์โดยอัตโนมัติซึ่งจะแสดงรายการฟิลด์ที่กำหนดไว้ทั้งหมด จากนั้นคุณสามารถส่งมาสก์ที่แสดงผลไปยังการเรียกใช้การอัปเดตได้โดยตรง
FieldMasks::all_set_fields_of()
เป็นเมธอดที่สะดวกสำหรับ FieldMasks::field_mask()
โดยจะเปรียบเทียบออบเจ็กต์ที่ส่งกับออบเจ็กต์ว่างของคลาสเดียวกัน ดังนั้นในโค้ดด้านบน คุณจะใช้
field_mask(Google::Ads::GoogleAds::V19::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
ซึ่งมี 3 ช่อง ได้แก่ target_cpa_micros
, cpc_bid_ceiling_micros
และ cpc_bid_floor_micros
) หรือจะไม่มีเลยก็ได้ (เช่น ManualCpm
)
ช่องออบเจ็กต์ที่ไม่มีการกำหนดช่องย่อย
ฟิลด์ออบเจ็กต์ใน Perl เทียบเท่ากับ protobuf MESSAGE
ในไลบรารีไคลเอ็นต์ที่ทำงานบน gRPC เมื่ออัปเดตฟิลด์ออบเจ็กต์ที่ไม่ได้กำหนดให้มีฟิลด์ย่อย ให้ใช้ยูทิลิตี FieldMasks เพื่อสร้างมาสก์ฟิลด์ตามที่อธิบายไว้ข้างต้น
ฟิลด์ออบเจ็กต์ที่มีฟิลด์ย่อยที่กําหนดไว้
เมื่ออัปเดตช่องออบเจ็กต์ที่กําหนดให้มีช่องย่อยโดยไม่ตั้งค่าช่องย่อยในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มช่องย่อยออบเจ็กต์ที่เปลี่ยนแปลงได้แต่ละช่องลงใน 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::V19::Resources::Campaign->new({
resourceName =>
Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
maximizeConversions =>
Google::Ads::GoogleAds::V19::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::V19::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::V19::Resources::Campaign->new({
resourceName => Google::Ads::GoogleAds::V19::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::V19::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::V19::Resources::Campaign->new({
resourceName => Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
$customer_id, $campaign_id
),
maximizeConversions => Google::Ads::GoogleAds::V19::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::V19::Services::CampaignService::CampaignOperation->new({
update => $campaign,
updateMask => all_set_fields_of($campaign)
});
โปรดทราบว่าคุณจะล้างช่องที่มีช่องย่อยที่ฝังอยู่ได้โดยการล้างช่องย่อยแต่ละช่องเท่านั้น ดังที่แสดงในช่องออบเจ็กต์ที่มีช่องย่อยที่กําหนดไว้