ใน Google Ads API จะใช้ฟิลด์มาสก์เพื่อระบุรายการฟิลด์ ที่คำขอ API ควรจะอัปเดต ระบบจะไม่สนใจฟิลด์ที่ไม่ได้ระบุในฟิลด์ มาสก์ แม้ว่าจะส่งไปยังเซิร์ฟเวอร์ก็ตาม
คลาส FieldMasks
วิธีที่แนะนําในการสร้างมาสก์ฟิลด์ในไลบรารีไคลเอ็นต์ .NET คือการใช้
คลาสยูทิลิตี FieldMasks ในตัว ซึ่งช่วยให้คุณสร้างมาสก์ฟิลด์
จากออบเจ็กต์ที่แก้ไขแล้วแทนที่จะสร้างจากศูนย์
ตัวอย่างการอัปเดตแคมเปญที่ใช้วิธี FieldMasks.AllSetFieldsOf เพื่อสร้างมาสก์ฟิลด์ที่แสดงรายการฟิลด์ที่ตั้งค่าทั้งหมด
จากนั้นคุณจะส่งฟิลด์มาสก์ที่สร้างขึ้นไปยังการเรียกอัปเดตได้โดยตรง
// 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 });
ในบางครั้ง คุณอาจต้องทำงานกับออบเจ็กต์ที่มีอยู่และอัปเดตฟิลด์บางรายการ
ในกรณีดังกล่าว ให้ใช้วิธี FieldMasks.FromChanges แทน เมธอดนี้
จะสร้างมาสก์ฟิลด์ที่แสดงความแตกต่างระหว่างออบเจ็กต์ 2 รายการ
Campaign existingCampaign;
// Obtain existingCampaign from an earlier API call.
// Create a new campaign based on 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)
};
จัดการข้อผิดพลาด 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()
};
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
การเรียก API นี้ล้มเหลวโดยมีข้อผิดพลาด FieldMaskError.FIELD_HAS_SUBFIELDS เนื่องจาก MaximizeConversions มีฟิลด์ย่อย เซิร์ฟเวอร์ Google Ads API จึงคาดหวังว่าจะมีมาสก์ของฟิลด์สำหรับฟิลด์ย่อยที่เปลี่ยนแปลงได้อยู่ในคำขอ อย่างไรก็ตาม FieldMasks ไม่สามารถสร้างมาสก์ฟิลด์ย่อยโดยอัตโนมัติ
ในกรณีนี้เนื่องจากคำขอไม่ได้ตั้งค่าฟิลด์ย่อยใดๆ
ในกรณีดังกล่าว คุณสามารถเพิ่มเส้นทางไปยัง fieldMask.Paths ด้วยตนเอง (ซึ่งเป็น
RepeatedField<string>) ได้โดยทำดังนี้
// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
};
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 at:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
ล้างช่อง
Google Ads API รองรับการล้างค่าฟิลด์บางรายการ หากต้องการล้างช่อง คุณต้อง
รวมช่องนั้นไว้ในฟิลด์มาสก์ด้วยตนเองในขณะที่ปล่อยให้ช่องไม่ได้ตั้งค่าใน
ออบเจ็กต์ทรัพยากร การตั้งค่าช่องเป็นค่าเริ่มต้น (เช่น 0 สำหรับช่อง int64) จะไม่ล้างช่อง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีล้างฟิลด์ target_cpa_micros ของ
MaximizeConversions กลยุทธ์การเสนอราคา
รหัสที่ถูกต้อง
โค้ดต่อไปนี้จะล้างฟิลด์ target_cpa_micros เนื่องจากจะเพิ่ม
maximize_conversions.target_cpa_micros ลงในมาสก์ของฟิลด์โดยไม่ได้ตั้งค่าพร็อพเพอร์ตี้ campaign.MaximizeConversions.TargetCpaMicros
// 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
// clears this field from the bidding strategy without impacting any other
// fields on the bidding strategy.
FieldMask fieldMask = FieldMasks.AllSetFieldsOf(campaign);
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
รหัสไม่ถูกต้อง
โค้ดต่อไปนี้ไม่ได้ล้างฟิลด์ target_cpa_micros เนื่องจาก
ตั้งค่าฟิลด์เป็น 0 ทั้งยูทิลิตี FieldMasks และเซิร์ฟเวอร์ Google Ads API
จะไม่สนใจค่านี้เมื่อ TargetCpaMicros เป็น 0 หรือเมื่อละเว้นเส้นทางจากมาสก์ และเซิร์ฟเวอร์จะไม่แสดงข้อผิดพลาด
// Creates a campaign with the proper resource name and a
// MaximizeConversions object. Attempts to clear the target_cpa_micros
// field by setting it to 0.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
MaximizeConversions = new MaximizeConversions()
{
TargetCpaMicros = 0
}
};
// Constructs an operation using FieldMasks.AllSetFieldsOf to derive the
// update mask.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
// Sends the operation in a mutate request that succeeds without clearing
// the previous 'target_cpa_micros' value cleanly.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });