ใน Google Ads API การอัปเดตจะทำโดยใช้มาสก์ฟิลด์ Field Mask จะแสดง ฟิลด์ทั้งหมดที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต และระบบจะ ละเว้นฟิลด์ที่ระบุซึ่งไม่ได้อยู่ใน Field Mask แม้ว่าจะส่งไปยังเซิร์ฟเวอร์ก็ตาม
FieldMaskUtil
วิธีที่แนะนำในการสร้างมาสก์ฟิลด์คือการใช้ยูทิลิตีมาสก์ฟิลด์ ในตัวของเรา ซึ่งช่วยให้คุณสร้างมาสก์ฟิลด์จากออบเจ็กต์ที่แก้ไขแล้วได้แทนที่จะ สร้างตั้งแต่ต้น
ตัวอย่างการอัปเดตแคมเปญมีดังนี้
// Creates a Campaign object with the proper resource name and any other changes.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// allSetFieldsOf utility to derive the update mask. This mask tells the Google
// Ads API which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
ตัวอย่างนี้จะสร้างออบเจ็กต์ Campaign
ที่ว่างเปล่าก่อน จากนั้นจึงตั้งค่าชื่อทรัพยากรเพื่อให้ API ทราบว่าแคมเปญใดที่กำลังอัปเดต
ตัวอย่างนี้ใช้วิธี FieldMasks.allSetFieldsOf()
ในแคมเปญเพื่อ
สร้างมาสก์ฟิลด์โดยอัตโนมัติซึ่งแสดงรายการฟิลด์ที่ตั้งค่าทั้งหมด จากนั้นคุณจะ
ส่งมาสก์ที่ส่งคืนไปยังการเรียกใช้การอัปเดตได้โดยตรง
หากต้องการทำงานกับออบเจ็กต์ที่มีอยู่เพื่ออัปเดตฟิลด์ 2-3 รายการ คุณสามารถ แก้ไขโค้ดได้ดังนี้
Campaign existingCampaign;
// Obtains existingCampaign from elsewhere.
...
// Creates a new campaign based off the existing campaign and updates the
// campaign by setting its status to paused.
Campaign campaignToUpdate =
existingCampaign.toBuilder()
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// compare utility to derive the update mask. This mask tells the Google Ads API
// which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaignToUpdate)
.setUpdateMask(FieldMasks.compare(existingCampaign, campaignToUpdate))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
หากต้องการสร้างมาสก์ฟิลด์ตั้งแต่ต้น คุณต้องสร้างออบเจ็กต์
FieldMask
ก่อน จากนั้นเพิ่มชื่อของแต่ละฟิลด์ที่คุณต้องการเปลี่ยนลงในออบเจ็กต์
FieldMask fieldMask =
FieldMask.newBuilder()
.addPaths("status")
.addPaths("name")
.build();
การอัปเดตฟิลด์ข้อความและฟิลด์ย่อย
ฟิลด์ MESSAGE
อาจมีฟิลด์ย่อย (เช่น MaximizeConversions
ซึ่งมี 3 ฟิลด์ ได้แก่ target_cpa_micros
, cpc_bid_ceiling_micros
และ cpc_bid_floor_micros
) หรืออาจไม่มีเลย (เช่น ManualCpm
)
ฟิลด์ข้อความที่ไม่มีฟิลด์ย่อยที่กำหนด
เมื่ออัปเดตฟิลด์ MESSAGE
ที่ไม่ได้กำหนดด้วยฟิลด์ย่อยใดๆ ให้ใช้ FieldMaskUtil เพื่อสร้างฟิลด์มาสก์ตามที่อธิบายไว้ข้างต้น
ฟิลด์ข้อความที่มีฟิลด์ย่อยที่กำหนด
เมื่ออัปเดตMESSAGE
ฟิลด์ที่กำหนดด้วยฟิลด์ย่อยโดยไม่ได้
ตั้งค่าฟิลด์ย่อยใดๆ ในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มMESSAGE
ฟิลด์ย่อยแต่ละรายการที่แก้ไขได้ลงในFieldMask
ด้วยตนเอง เช่นเดียวกับ
ตัวอย่างด้านบนที่สร้างมาสก์ฟิลด์ตั้งแต่ต้น
ตัวอย่างที่พบบ่อยอย่างหนึ่งคือการอัปเดตกลยุทธ์การเสนอราคาของแคมเปญโดยไม่ได้ตั้งค่าฟิลด์ใดๆ ในกลยุทธ์การเสนอราคาใหม่ ตัวอย่างด้านล่างแสดงวิธี
อัปเดตแคมเปญให้ใช้กลยุทธ์การเสนอราคา
MaximizeConversions
โดยไม่ต้องตั้งค่าฟิลด์ย่อยใดๆ ในกลยุทธ์การเสนอราคา
ในกรณีนี้ การใช้เมธอด allSetFieldsOf()
และ compare()
ของ
FieldMaskUtil จะไม่บรรลุเป้าหมายที่ต้องการ
ตัวอย่างต่อไปนี้สร้างมาสก์ฟิลด์ที่มี
maximize_conversions
อย่างไรก็ตาม Google Ads API ไม่อนุญาตให้ดำเนินการดังกล่าวเพื่อ
ป้องกันการล้างช่องโดยไม่ตั้งใจและทำให้เกิดข้อผิดพลาด
FieldMaskError.FIELD_HAS_SUBFIELDS
// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(MaximizeConversions.newBuilder().build())
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. The field mask will include 'maximize_conversions`,
// which will produce a FieldMaskError.FIELD_HAS_SUBFIELDS error.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// 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.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตแคมเปญอย่างถูกต้องเพื่อใช้
MaximizeConversions
กลยุทธ์การเสนอราคาโดยไม่ต้องตั้งค่าฟิลด์ย่อย
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Creates a field mask from the existing campaign and adds all of the mutable
// fields (only one in this case) on the MaximizeConversions bidding strategy to
// the field mask. Because this field is 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 without any of its subfields
// set.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
// 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
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask)
.build();
ล้างช่อง
คุณล้างข้อมูลบางช่องได้อย่างชัดเจน เช่นเดียวกับตัวอย่างข้างต้น คุณต้อง
เพิ่มฟิลด์เหล่านี้ลงในมาสก์ฟิลด์อย่างชัดเจน เช่น สมมติว่าคุณมีแคมเปญที่ใช้กลยุทธ์การเสนอราคา MaximizeConversions
และตั้งค่าฟิลด์ target_cpa_micros
เป็นค่าที่มากกว่า 0
โค้ดต่อไปนี้จะทํางาน แต่จะไม่เพิ่ม maximize_conversions.target_cpa_micros
ลงในมาสก์ฟิลด์ จึงไม่มีการเปลี่ยนแปลงฟิลด์ target_cpa_micros
// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(
MaximizeConversions.newBuilder().setTargetCpa(0).build())
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask will NOT include
// 'maximize_conversions.target_cpa_micros'.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that will succeed but will NOT update
// the 'target_cpa_micros' field because
// 'maximize_conversions.target_cpa_micros' was not included in the field mask.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
ตัวอย่างถัดไปแสดงวิธีล้างฟิลด์ target_cpa_micros
ในMaximizeConversions
กลยุทธ์การเสนอราคาอย่างถูกต้อง
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Constructs a field mask from the existing campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which will
// clear this field from the bidding strategy without impacting any other fields
// on the bidding strategy.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask))
.build();
โปรดทราบว่าตัวอย่าง "ไม่ถูกต้อง" ด้านบนทํางานตามที่ต้องการสําหรับฟิลด์ที่กําหนดเป็น optional
ใน Google Ads API
protocol buffers
แต่เนื่องจาก
target_cpa_micros
ไม่ใช่ฟิลด์ optional
ตัวอย่าง "ไม่ถูกต้อง" จึงไม่อัปเดตกลยุทธ์การเสนอราคาเพื่อล้างฟิลด์ target_cpa