ใน Google Ads API การอัปเดตจะดำเนินการโดยใช้ field mask ฟิลด์มาสก์ (google.protobuf.FieldMask) มีรายการเส้นทางฟิลด์ใน snake_case ที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต ระบบจะไม่สนใจช่องที่ระบุซึ่งไม่ได้อยู่ในมาสก์ของช่อง แม้ว่าจะส่งไปยังเซิร์ฟเวอร์ก็ตาม
ยูทิลิตี FieldMasks
วิธีที่แนะนำในการสร้างมาสก์ฟิลด์ในไลบรารีของไคลเอ็นต์ Java คือการใช้คลาสยูทิลิตี FieldMasks ในตัว ซึ่งช่วยให้คุณสร้างมาสก์ฟิลด์จากออบเจ็กต์ที่แก้ไขแล้วแทนที่จะสร้างตั้งแต่ต้น
ตัวอย่างการอัปเดตแคมเปญมีดังนี้
// 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 updates 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 Builder เปล่าก่อน แล้วตั้งชื่อทรัพยากรเพื่อให้ API ทราบว่ากำลังอัปเดตแคมเปญใด
จากนั้นตัวอย่างจะเรียกใช้ FieldMasks.allSetFieldsOf() ในแคมเปญเพื่อสร้างมาสก์ฟิลด์ที่แจกแจงฟิลด์ที่ตั้งค่าไว้ทั้งหมดโดยอัตโนมัติ คุณสามารถส่งมาสก์ที่แสดงผลไปยังการเรียกใช้การอัปเดตได้โดยตรง
หากต้องการทำงานกับออบเจ็กต์ที่มีอยู่และอัปเดตบางฟิลด์ ให้ใช้ FieldMasks.compare() ดังนี้
// Assumes existingCampaign was retrieved from a previous API call.
// Creates a new campaign based on 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 updates 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 Builder และเพิ่มชื่อ snake_case ของแต่ละฟิลด์ที่ต้องการเปลี่ยนแปลง ดังนี้
FieldMask fieldMask =
FieldMask.newBuilder()
.addPaths("status")
.addPaths("name")
.build();
อัปเดตช่องข้อความและช่องย่อย
ช่อง MESSAGE อาจมีช่องย่อย (เช่น MaximizeConversions ซึ่งมี target_cpa_micros, cpc_bid_ceiling_micros และ cpc_bid_floor_micros) หรืออาจไม่มีช่องย่อย (เช่น ManualCpm)
ช่องข้อความที่ไม่มีช่องย่อยที่กำหนดไว้
เมื่ออัปเดตช่อง MESSAGE ที่ไม่ได้กำหนดด้วยช่องย่อยใดๆ ให้ใช้ยูทิลิตี FieldMasks เพื่อสร้างมาสก์ช่องตามที่อธิบายไว้ในส่วนก่อนหน้า
ช่องข้อความที่มีช่องย่อยที่กำหนดไว้
เมื่ออัปเดตช่อง MESSAGE ที่มีช่องย่อยที่กำหนดไว้โดยไม่ได้ตั้งค่าช่องย่อยใดๆ ในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มช่องย่อย MESSAGE ที่เปลี่ยนแปลงได้แต่ละช่องลงใน FieldMask ด้วยตนเอง ซึ่งคล้ายกับการสร้างมาสก์ช่องตั้งแต่ต้น
ตัวอย่างที่พบบ่อยคือการอัปเดตกลยุทธ์การเสนอราคาของแคมเปญ (ช่อง oneof campaign_bidding_strategy) โดยไม่ได้ตั้งค่าช่องใดๆ ในกลยุทธ์การเสนอราคาใหม่ ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตแคมเปญเพื่อใช้กลยุทธ์การเสนอราคา MaximizeConversions โดยไม่ต้องตั้งค่าช่องย่อยใดๆ ในกลยุทธ์การเสนอราคา
ในกรณีนี้ การใช้เมธอด allSetFieldsOf() และ compare() ของ FieldMasks เพียงอย่างเดียวจะไม่บรรลุเป้าหมายที่ต้องการ
ตัวอย่างต่อไปนี้จะสร้างมาสก์ฟิลด์ที่มี 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 FieldMasks.allSetFieldsOf to derive the update
// mask. The field mask includes 'maximize_conversions', which produces 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 results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields with
// subfields cannot be included directly 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 campaign and adds the mutable subfield
// ('maximize_conversions.target_cpa_micros') on the MaximizeConversions
// bidding strategy to the field mask. Because this subfield is included in the
// field mask while excluded from the campaign object, the Google Ads API
// switches the campaign's bidding strategy oneof to MaximizeConversions with
// target_cpa_micros unset.
FieldMask fieldMask =
FieldMasks.allSetFieldsOf(campaign).toBuilder()
.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 จะไม่ถูกล้างตามที่ต้องการ
// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0L.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(
MaximizeConversions.newBuilder().setTargetCpaMicros(0L).build())
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation using FieldMasks.allSetFieldsOf to derive the
// update mask.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that does not clear the field
// cleanly.
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 campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which
// clears 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();