ใน Google Ads API การอัปเดตจะทําโดยใช้มาสก์ช่อง หน้ากากฟิลด์จะแสดงรายการช่องทั้งหมดที่คุณต้องการเปลี่ยนแปลงด้วยการอัปเดต และระบบจะไม่สนใจฟิลด์ที่ระบุซึ่งไม่ได้อยู่ในหน้ากากฟิลด์ แม้ว่าจะส่งไปยังเซิร์ฟเวอร์ก็ตาม คุณสามารถสร้างมาสก์ฟิลด์ด้วยตนเองได้โดยสร้าง Google\Protobuf\FieldMask
, สร้างอาร์เรย์ที่มีชื่อของฟิลด์ทั้งหมดที่คุณต้องการเปลี่ยน แล้วกําหนดค่าให้กับช่องเส้นทางของมาสก์ฟิลด์
นอกจากนี้ คุณยังใช้ยูทิลิตีการมาสก์ฟิลด์ในตัวของเรา (FieldMasks) ได้ด้วย ซึ่งจะซ่อนรายละเอียดที่เฉพาะเจาะจงจำนวนมากและให้คุณสร้างการมาสก์ฟิลด์โดยอัตโนมัติโดยการตรวจสอบการเปลี่ยนแปลงที่คุณทำกับฟิลด์ของเอนทิตี
ตัวอย่างการอัปเดตแคมเปญมีดังนี้
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
'status' => CampaignStatus::PAUSED
]);
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
โค้ดนี้จะสร้างออบเจ็กต์แคมเปญก่อน จากนั้นตั้งชื่อทรัพยากรโดยใช้ ResourceNames
เพื่อให้ API ทราบว่ากําลังอัปเดตแคมเปญใด ตั้งค่า status
เป็น PAUSED
ด้วย
จากนั้นโค้ดจะสร้างออบเจ็กต์ CampaignOperation
และตั้งค่าแคมเปญที่สร้างไว้ก่อนหน้านี้ หลังจากนั้นจะใช้
FieldMasks::allSetFieldsOf()
เพื่อสร้างมาสก์ฟิลด์สําหรับแคมเปญโดยใช้ฟิลด์ทั้งหมดที่มีการเปลี่ยนแปลง สุดท้าย ระบบจะส่งผ่านมาสก์ที่แสดงผลไปยังออบเจ็กต์การดำเนินการของแคมเปญ
โปรดทราบว่า
FieldMasks::allSetFieldsOf
เป็นวิธีที่สะดวกสำหรับ
FieldMasks::compare()
โดยจะเปรียบเทียบออบเจ็กต์ที่ส่งกับออบเจ็กต์ว่างของคลาสเดียวกัน ตัวอย่างเช่น ในโค้ดก่อนหน้านี้ คุณอาจใช้ FieldMasks::compare(new
Campaign(), $campaign)
แทน allSetFieldsOf()
ได้
การอัปเดตช่องข้อความและช่องย่อยของข้อความ
ฟิลด์ MESSAGE
อาจมีฟิลด์ย่อย (เช่น MaximizeConversions
ซึ่งมี 3 ฟิลด์ ได้แก่ target_cpa_micros
, cpc_bid_ceiling_micros
และ cpc_bid_floor_micros
) หรือไม่มีฟิลด์ย่อยเลยก็ได้ (เช่น ManualCpm
)
ช่องข้อความที่ไม่มีการกำหนดช่องย่อย
เมื่ออัปเดตช่อง MESSAGE
ที่ไม่ได้กำหนดให้มีช่องย่อย ให้ใช้ FieldMasks
เพื่อสร้างมาสก์ช่องตามที่อธิบายไว้ก่อนหน้านี้
ช่องข้อความที่มีช่องย่อยที่กําหนดไว้
เมื่ออัปเดตช่อง MESSAGE
ที่กําหนดให้มีช่องย่อยโดยไม่ตั้งค่าช่องย่อยใดๆ ในข้อความนั้นอย่างชัดเจน คุณต้องเพิ่มช่องย่อย MESSAGE
ที่เปลี่ยนแปลงได้แต่ละช่องลงใน FieldMask
ด้วยตนเอง ซึ่งคล้ายกับตัวอย่างก่อนหน้านี้ที่สร้างมาสก์ช่องตั้งแต่ต้น
ตัวอย่างที่พบบ่อยอย่างหนึ่งคือการอัปเดตกลยุทธ์การเสนอราคาของแคมเปญโดยไม่กรอกข้อมูลในช่องใดๆ ของกลยุทธ์การเสนอราคาใหม่ โค้ดด้านล่างแสดงวิธีอัปเดตแคมเปญให้ใช้กลยุทธ์การเสนอราคา 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 = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
'maximize_conversions' => 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 = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(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.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);
โค้ดต่อไปนี้แสดงวิธีอัปเดตแคมเปญอย่างถูกต้องเพื่อใช้กลยุทธ์การเสนอราคา MaximizeConversions
โดยไม่ต้องตั้งค่าช่องย่อยใดๆ
// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId)
]);
// 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 = 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->setPaths(array_merge(
iterator_to_array($fieldMask->getPaths()->getIterator()),
['maximize_conversions.target_cpa_micros']
));
// Creates an operation to update the campaign with the specified fields.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($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 = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
'maximize_conversions' => new MaximizeConversions(['target_cpa' => 0]),
'status' => CampaignStatus::PAUSED
]);
// 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 = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(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.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);
โค้ดต่อไปนี้แสดงวิธีล้างtarget_cpa_micros
ช่องในกลยุทธ์การเสนอราคา MaximizeConversions
อย่างถูกต้อง
// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($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 = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->setPaths(array_merge(
iterator_to_array($fieldMask->getPaths()->getIterator()),
['maximize_conversions.target_cpa_micros']
));
// Creates an operation to update the campaign with the specified field.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($fieldMask);
โปรดทราบว่าโค้ด "ไม่ถูกต้อง" จะทํางานตามที่ตั้งใจไว้สําหรับช่องที่กําหนดเป็น optional
ใน Google Ads API protocol buffers
แต่เนื่องจาก target_cpa_micros
ไม่ใช่ช่อง optional
รหัส "ไม่ถูกต้อง" จึงไม่อัปเดตกลยุทธ์การเสนอราคาเพื่อล้างช่อง target_cpa