ใน 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 ในแคมเปญเพื่อเริ่มต้น
บล็อกที่รวมการอัปเดตไว้ด้วย เมื่อสิ้นสุดบล็อกนี้ ยูทิลิตี
เปรียบเทียบสถานะปัจจุบันของแคมเปญหลังจากการบล็อกกับโฆษณาเริ่มต้น
ของแคมเปญก่อนการบล็อก และสร้างฟิลด์โดยอัตโนมัติ
มาสก์ที่แจกแจงฟิลด์ที่เปลี่ยนแปลง คุณสามารถระบุฟิลด์มาสก์นั้นให้กับ
เมื่อสร้างคำสั่งสำหรับ Mutate ดังนี้
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