ใน Google Ads API การอัปเดตจะทําโดยใช้มาสก์ช่อง หน้ากากฟิลด์จะแสดงรายการช่องทั้งหมดที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต และระบบจะไม่สนใจฟิลด์ที่ระบุซึ่งไม่ได้อยู่ในหน้ากากฟิลด์ แม้ว่าจะส่งไปยังเซิร์ฟเวอร์ก็ตาม
FieldMaskUtil
วิธีสร้างมาสก์ฟิลด์ที่แนะนําคือการใช้ยูทิลิตีมาสก์ฟิลด์ในตัว ซึ่งจะซ่อนรายละเอียดที่เฉพาะเจาะจงจํานวนมากและให้คุณสร้างมาสก์ฟิลด์โดยอัตโนมัติโดยการตรวจสอบการเปลี่ยนแปลงที่คุณทํากับฟิลด์ของเอนทิตี
วิธีสร้างมาสก์ช่องสําหรับอัปเดตแคมเปญมีดังนี้
campaign = client.resource.campaign
campaign.resource_name = client.path.campaign(customer_id, campaign_id)
mask = client.field_mask.with campaign do
campaign.status = :PAUSED
campaign.network_settings = client.resource.network_settings do |ns|
ns.target_search_network = false
end
end
โค้ดจะสร้างออบเจ็กต์แคมเปญว่างก่อน จากนั้นตั้งชื่อทรัพยากรเพื่อแจ้งให้ API ทราบว่ามีการอัปเดตแคมเปญ
ตัวอย่างนี้ใช้เมธอด client.field_mask.with
ในแคมเปญเพื่อเริ่มบล็อกที่รวมการอัปเดต เมื่อสิ้นสุดบล็อกนี้ ยูทิลิตีจะเปรียบเทียบสถานะปัจจุบันของแคมเปญหลังบล็อกกับสถานะเริ่มต้นของแคมเปญก่อนบล็อก และสร้างมาสก์ฟิลด์โดยอัตโนมัติเพื่อแสดงรายการฟิลด์ที่มีการเปลี่ยนแปลง คุณสามารถระบุมาสก์ฟิลด์นั้นให้กับการดำเนินการเมื่อสร้างการดำเนินการสำหรับการเรียกใช้การดัดแปลง ดังนี้
operation = client.operation.campaign
operation.update = campaign
operation.update_mask = mask
เราขอแนะนําให้ใช้วิธีนี้เมื่อดําเนินการที่ซับซ้อนและต้องการการควบคุมอย่างละเอียดในทุกขั้นตอน อย่างไรก็ตาม ในกรณีส่วนใหญ่ คุณสามารถใช้ยูทิลิตีไลบรารี Ruby ที่ง่ายกว่านี้ได้
operation = client.operation.update_resource.campaign do |c|
c.status = :PAUSED
c.network_settings = client.resource.network_settings do |ns|
ns.target_search_network = false
end
end
วิธีนี้จะสร้างทรัพยากรแคมเปญใหม่ว่างเปล่าโดยอัตโนมัติ สร้างมาสก์ช่องตามการเปลี่ยนแปลงที่คุณทําภายในบล็อก สร้างการดำเนินการอัปเดต และแสดงผลการดำเนินการสุดท้ายที่มี update
และ update_mask
อยู่แล้ว นอกจากนี้ คุณยังส่งแคมเปญไปยังเมธอด campaign
เพื่อระบุสถานะเริ่มต้นของแคมเปญได้ด้วย รูปแบบนี้ใช้ได้กับทรัพยากรทั้งหมดที่รองรับการดำเนินการอัปเดต
การสร้างมาสก์ด้วยตนเอง
หากต้องการสร้างมาสก์ฟิลด์ตั้งแต่ต้นโดยไม่ใช้ยูทิลิตีของไลบรารี คุณจะต้องสร้าง Google::Protobuf::FieldMask
ก่อน จากนั้นสร้างอาร์เรย์ที่ป้อนข้อมูลชื่อของฟิลด์ทั้งหมดที่คุณต้องการเปลี่ยน และสุดท้ายให้กําหนดค่าอาร์เรย์ให้กับฟิลด์ path
ของมาสก์ฟิลด์
mask = Google::Protobuf::FieldMask.new
mask.path = ["status", "name"]
การอัปเดตช่องข้อความและช่องย่อยของข้อความ
ช่อง MESSAGE
อาจมีช่องย่อย (เช่น MaximizeConversions
ซึ่งมี 3 ช่อง ได้แก่ target_cpa_micros
, cpc_bid_ceiling_micros
และ cpc_bid_floor_micros
) หรือจะไม่มีช่องย่อยเลยก็ได้ (เช่น ManualCpm
)
ช่องข้อความที่ไม่มีการกำหนดช่องย่อย
เมื่ออัปเดตช่อง MESSAGE
ที่ไม่ได้กำหนดให้มีช่องย่อย ให้ใช้ FieldMaskUtil เพื่อสร้างมาสก์ช่องตามที่แสดงไว้ก่อนหน้านี้
ช่องข้อความที่มีช่องย่อยที่กําหนดไว้
เมื่ออัปเดตฟิลด์ MESSAGE
ที่กําหนดให้มีช่องย่อยโดยไม่ตั้งค่าช่องย่อยใดๆ ในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มช่องย่อย MESSAGE
ที่เปลี่ยนแปลงได้แต่ละช่องลงใน FieldMask
ด้วยตนเอง ซึ่งคล้ายกับตัวอย่างก่อนหน้านี้ที่สร้างมาสก์ฟิลด์ตั้งแต่ต้น
ตัวอย่างที่พบบ่อยอย่างหนึ่งคือการอัปเดตกลยุทธ์การเสนอราคาของแคมเปญโดยไม่ตั้งค่าช่องใดๆ ในกลยุทธ์การเสนอราคาใหม่ ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตแคมเปญให้ใช้กลยุทธ์การเสนอราคา MaximizeConversions
โดยไม่ต้องตั้งค่าช่องย่อยในกลยุทธ์การเสนอราคา
ในตัวอย่างนี้ การใช้การเปรียบเทียบในตัวของ FieldMaskUtil ไม่ได้บรรลุเป้าหมายที่ต้องการ
โค้ดต่อไปนี้จะสร้างมาสก์ฟิลด์ที่มี maximize_conversions
อย่างไรก็ตาม Google Ads API ไม่อนุญาตให้มีการทำงานลักษณะนี้เพื่อป้องกันไม่ให้ล้างช่องโดยไม่ตั้งใจและทำให้เกิดข้อผิดพลาด FieldMaskError.FIELD_HAS_SUBFIELDS
# Creates a campaign with the proper resource name.
campaign = client.resource.campaign do |c|
c.resource_name = client.path.campaign(customer_id, campaign_id)
end
# Update the maximize conversions field within the update block, so it's
# captured in the field mask
operation = client.operation.update_resource.campaign(campaign) do |c|
c.maximize_conversions = client.resource.maximize_conversions
end
# Sends the operation in a mutate request that will result in a
# FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
# be included in a field mask.
response = client.service.campaign.mutate_campaigns(
customer_id: customer_id,
operations: [operation],
)
โค้ดต่อไปนี้แสดงวิธีอัปเดตแคมเปญอย่างถูกต้องเพื่อใช้กลยุทธ์การเสนอราคา MaximizeConversions
โดยไม่ต้องตั้งค่าช่องย่อยใดๆ
# Create the operation directly from the campaign's resource name. Don't do
# anything in the block so that the field mask is empty. You could modify other
# fields in this block, just not the message field that is intended to have a
# blank subfield. We'll add that below.
campaign_resource_name = client.path.campaign(customer_id, campaign_id)
operation = client.operation.update_resource.campaign(campaign_resource_name) {}
# Manually add the maximize conversions subfield to the field mask so the API
# knows to clear it.
operation.update_mask.paths << "maximize_conversions.target_cpa_micros"
# This operation succeeds.
response = client.service.campaign.mutate_campaigns(
customer_id: customer_id,
operations: [operation],
)
การล้างช่อง
คุณสามารถล้างข้อมูลในบางช่องได้อย่างชัดเจน เช่นเดียวกับตัวอย่างก่อนหน้านี้ คุณต้องเพิ่มฟิลด์เหล่านี้ลงในมาสก์ฟิลด์อย่างชัดเจน ตัวอย่างเช่น สมมติว่าคุณมีแคมเปญที่ใช้กลยุทธ์การเสนอราคา MaximizeConversions
และตั้งค่าช่อง target_cpa_micros
ด้วยค่าที่มากกว่า 0
โค้ดต่อไปนี้จะทํางาน แต่ระบบจะไม่เพิ่ม maximize_conversions.target_cpa_micros
ลงในมาสก์ช่อง ดังนั้นจึงไม่มีการเปลี่ยนแปลงในช่อง target_cpa_micros
# Create a campaign object representing the campaign you want to change.
campaign = client.resource.campaign do |c|
c.resource_name = client.path.campaign(customer_id, campaign_id)
end
# The field mask in this operation will include 'maximize_conversions',
# but not 'maximize_conversions.target_cpa_micros', so it will result in an
# error.
operation = client.operation.update_resource.campaign(campaign) do |c|
c.maximize_conversions = client.resource.maximize_conversions do |mc|
mc.target_cpa_micros = 0
end
end
# Operation will fail since field mask is incorrect.
response = client.service.campaign.mutate_campaigns(
customer_id: customer_id,
operations: [operation],
end
โค้ดต่อไปนี้แสดงวิธีล้างtarget_cpa_micros
ช่องในกลยุทธ์การเสนอราคา MaximizeConversions
อย่างถูกต้อง
# Create a campaign including the maximize conversions fields right away, since
# we're going to manually add them to the field mask.
campaign = client.resource.campaign do |c|
c.resource_name = client.path.campaign(customer_id, campaign_id)
c.maximize_conversions = client.resource.maximize_conversions do |mc|
mc.target_cpa_micros = 0
end
end
# Create the operation with an empty field mask. You may add a block here with
# other changes that will automatically get added to the field mask.
operation = client.operation.update_resource.campaign(campaign) {}
# Add the field to the field mask so the API knows to clear it.
operation.update_mask.paths << 'maximize_conversions.target_cpa_micros'
# Operation will succeed since we specified the correct field mask.
response = client.service.campaign.mutate_campaigns(
customer_id: customer_id,
operations: [operation],
end
โปรดทราบว่าโค้ด "ไม่ถูกต้อง" จะทํางานตามที่ตั้งใจไว้สําหรับช่องที่กําหนดเป็น optional
ใน Google Ads API protocol buffers
แต่เนื่องจาก target_cpa_micros
ไม่ใช่ช่อง optional
รหัส "ไม่ถูกต้อง" จึงไม่ได้อัปเดตกลยุทธ์การเสนอราคาเพื่อล้างช่อง target_cpa