หน้ากากอนามัย

ใน Google Ads API การอัปเดตจะทําโดยใช้มาสก์ช่อง มาสก์ฟิลด์จะแสดงรายการฟิลด์ทั้งหมดที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต และระบบจะไม่สนใจฟิลด์ที่ระบุซึ่งไม่ได้อยู่ในมาสก์ฟิลด์ แม้ว่าจะส่งไปยังเซิร์ฟเวอร์แล้วก็ตาม

FieldMaskUtil

วิธีสร้างมาสก์ฟิลด์ที่แนะนําคือการใช้ยูทิลิตีมาสก์ฟิลด์ในตัว ซึ่งจะช่วยให้คุณสร้างมาสก์ฟิลด์จากออบเจ็กต์ที่แก้ไขแล้วได้โดยไม่ต้องสร้างใหม่ตั้งแต่ต้น

ตัวอย่างการอัปเดตแคมเปญมีดังนี้

// Update campaign by setting its status to paused, and "Search network" to false.
Campaign campaignToUpdate = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    Status = CampaignStatus.Paused,
    NetworkSettings = new NetworkSettings()
    {
        TargetSearchNetwork = false
    }
};

// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaignToUpdate,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaignToUpdate)
};

// Update the campaign.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

ก่อนอื่น เราจะสร้างออบเจ็กต์ Campaign ว่าง จากนั้นตั้งชื่อทรัพยากรเพื่อให้ API ทราบว่าเรากําลังอัปเดตแคมเปญใด

ตัวอย่างนี้ใช้เมธอด FieldMasks.AllSetFieldsOf ในแคมเปญเพื่อสร้างมาสก์ฟิลด์ที่ระบุฟิลด์ที่กำหนดไว้ทั้งหมดโดยอัตโนมัติ จากนั้นคุณสามารถส่งมาสก์ที่แสดงผลไปยังการเรียกใช้การอัปเดตได้โดยตรง

บางครั้งคุณอาจต้องทำงานกับออบเจ็กต์ที่มีอยู่และอัปเดตช่อง 2-3 ช่อง ในกรณีดังกล่าว คุณจะแก้ไขโค้ดได้ดังนี้

Campaign existingCampaign;

// Obtain existingCampaign from elsewhere.
...

// Create a new campaign based off the existing campaign for update.
Campaign campaignToUpdate = new Campaign(existingCampaign);

// Update campaign by setting its status to paused, and "Search network" to
// false.
campaignToUpdate.Status = CampaignStatus.Paused;
campaignToUpdate.NetworkSettings = new NetworkSettings()
{
    TargetSearchNetwork = false
}

// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaignToUpdate,
    UpdateMask = FieldMasks.FromChanges(existingCampaign, campaignToUpdate)
};

หากต้องการสร้างมาสก์ฟิลด์ตั้งแต่ต้น ก่อนอื่นให้สร้างออบเจ็กต์ FieldMask จากนั้นสร้างอาร์เรย์ที่ป้อนข้อมูลชื่อของฟิลด์ทั้งหมดที่คุณต้องการเปลี่ยน และสุดท้ายให้เพิ่มเนื้อหาอาร์เรย์ต่อท้ายฟิลด์ Path ของมาสก์ฟิลด์

FieldMask fieldMask = new FieldMask();
fieldMask.Paths.AddRange(new string[] { "status", "name" });

การอัปเดตช่องข้อความและช่องย่อยของข้อความ

ฟิลด์ MESSAGE อาจมีฟิลด์ย่อย (เช่น MaximizeConversions ซึ่งมี 3 ฟิลด์ ได้แก่ target_cpa_micros, cpc_bid_ceiling_micros และ cpc_bid_floor_micros) หรือไม่มีฟิลด์ย่อยเลยก็ได้ (เช่น ManualCpm)

ช่องข้อความที่ไม่มีการกำหนดช่องย่อย

เมื่ออัปเดตช่อง MESSAGE ที่ไม่ได้กำหนดให้มีช่องย่อย ให้ใช้ FieldMasks เพื่อสร้างมาสก์ช่องตามที่อธิบายไว้ข้างต้น

ช่องข้อความที่มีช่องย่อยที่กําหนดไว้

เมื่ออัปเดตช่อง MESSAGE ที่กําหนดให้มีช่องย่อยโดยไม่ตั้งค่าช่องย่อยใดๆ ในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มช่องย่อย MESSAGE ที่เปลี่ยนแปลงได้แต่ละช่องลงใน FieldMask ด้วยตนเอง ซึ่งคล้ายกับตัวอย่างด้านบนที่สร้างมาสก์ช่องตั้งแต่ต้น

ตัวอย่างที่พบบ่อยอย่างหนึ่งคือการอัปเดตกลยุทธ์การเสนอราคาของแคมเปญโดยไม่กรอกข้อมูลในช่องใดๆ ของกลยุทธ์การเสนอราคาใหม่ ตัวอย่างด้านล่างแสดงวิธีอัปเดตแคมเปญให้ใช้กลยุทธ์การเสนอราคา MaximizeConversions โดยไม่ต้องตั้งค่าช่องย่อยในกลยุทธ์การเสนอราคา

ในกรณีนี้ การใช้วิธีการ AllSetFieldsOf() และ FromChanges() ของ 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 = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    MaximizeConversions = new MaximizeConversions()
};

// 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 = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};

// 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 = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตแคมเปญอย่างถูกต้องเพื่อใช้กลยุทธ์การเสนอราคา MaximizeConversions โดยไม่ต้องตั้งค่าช่องย่อยใดๆ

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
};

// Creates a field mask from the existing campaign and adds all of the fields
// on the MaximizeConversions bidding strategy to the field mask. Because these
// fields are 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 with none of its subfields set.
FieldMask fieldMask = FieldMasks.AllSetFieldsOf(campaign);
// 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
fieldMask.Paths.AddRange(new string[] {
    "maximize_conversions.target_cpa_micros",
});

// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = fieldMask
};

การล้างฟิลด์

คุณสามารถล้างข้อมูลในบางช่องได้อย่างชัดเจน คุณต้องเพิ่มช่องเหล่านี้ลงในมาสก์ฟิลด์อย่างชัดเจน เช่นเดียวกับตัวอย่างข้างต้น ตัวอย่างเช่น สมมติว่าคุณมีแคมเปญที่ใช้กลยุทธ์การเสนอราคา 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 = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    MaximizeConversions = new MaximizeConversions()
    {
        TargetCpaMicros = 0
    }
};

// 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 = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};

// 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 = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

ตัวอย่างถัดไปแสดงวิธีล้างtarget_cpa_micros ช่องในกลยุทธ์การเสนอราคา MaximizeConversions อย่างถูกต้อง

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
};

// 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);
fieldMask.Paths.AddRange(new string[] {
    "maximize_conversions.target_cpa_micros",
});

// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = fieldMask
};

โปรดทราบว่าตัวอย่างที่ "ไม่ถูกต้อง" ข้างต้นทํางานตามที่ตั้งใจไว้สําหรับช่องที่กําหนดเป็น optional ใน Google Ads API protocol buffers แต่เนื่องจาก target_cpa_micros ไม่ใช่ช่อง optional ตัวอย่าง "ไม่ถูกต้อง" จึงไม่อัปเดตกลยุทธ์การเสนอราคาเพื่อล้างช่อง target_cpa