หน้ากากภาคสนาม

ใน Google Ads API การอัปเดตจะทำได้โดยใช้ฟิลด์มาสก์ ฟิลด์มาสก์ (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() ในแคมเปญเพื่อ สร้างมาสก์ฟิลด์โดยอัตโนมัติซึ่งแสดงรายการฟิลด์ที่ตั้งค่าทั้งหมด คุณส่งมาสก์ที่ส่งคืนไปยังการเรียกใช้การอัปเดตได้โดยตรง

หากต้องการทำงานกับออบเจ็กต์ที่มีอยู่และอัปเดตฟิลด์ 2-3 รายการ ให้ใช้ 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เครื่องมือสร้าง แล้วเพิ่มชื่อ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();