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

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

นอกจากนี้ คุณยังใช้ยูทิลิตีการมาสก์ฟิลด์ในตัวของเรา (FieldMasks) ซึ่งซ่อนรายละเอียดที่เฉพาะเจาะจงหลายอย่างและ ช่วยให้คุณสร้างการมาสก์ฟิลด์โดยอัตโนมัติได้ด้วยการตรวจสอบการเปลี่ยนแปลงที่คุณทำ กับฟิลด์ของเอนทิตี

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

$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);

$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));

โค้ดนี้จะสร้างออบเจ็กต์ Campaign ก่อน จากนั้นจึงตั้งชื่อทรัพยากรโดยใช้ ResourceNames เพื่อให้ API ทราบว่าแคมเปญใดที่กำลังอัปเดต status ยังตั้งค่าเป็น CampaignStatus::PAUSED ด้วย

จากนั้นโค้ดจะสร้างออบเจ็กต์ CampaignOperation และตั้งค่าแคมเปญที่สร้างไว้ก่อนหน้านี้ในออบเจ็กต์ หลังจากนั้นจะใช้ FieldMasks::allSetFieldsOf() เพื่อสร้างฟิลด์ มาสก์สําหรับแคมเปญโดยการแจงนับฟิลด์ที่แก้ไขทั้งหมด สุดท้าย จะส่งผ่าน มาสก์ที่ส่งคืนไปยังออบเจ็กต์การดำเนินการของแคมเปญ

โปรดทราบว่า FieldMasks::allSetFieldsOf() เป็นวิธีการที่สะดวกสำหรับ FieldMasks::compare() โดยจะเปรียบเทียบออบเจ็กต์ที่ส่งผ่านกับ ออบเจ็กต์ว่างเปล่าของคลาสเดียวกัน เช่น ในโค้ดก่อนหน้านี้ คุณอาจใช้ FieldMasks::compare(new Campaign(), $campaign) แทน FieldMasks::allSetFieldsOf($campaign) ได้

อัปเดตฟิลด์ข้อความและฟิลด์ย่อย

ฟิลด์ MESSAGE อาจมีฟิลด์ย่อย (เช่น MaximizeConversions ซึ่งรวมถึงฟิลด์ย่อย เช่น 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 includes 'maximize_conversions',
// which produces 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 results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build($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 the mutable
// subfield 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 sets 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:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
$fieldMask->getPaths()[] = '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);

ล้างช่อง

คุณล้างข้อมูลบางช่องได้อย่างชัดเจน เช่นเดียวกับตัวอย่างก่อนหน้า คุณต้องเพิ่มฟิลด์เหล่านี้ลงในมาสก์ฟิลด์อย่างชัดเจน เนื่องจาก FieldMasks จะไม่สนใจฟิลด์สเกลาร์ Proto3 ที่ตั้งค่าเป็นค่าเริ่มต้น (เช่น 0, false หรือ "") ตัวอย่างเช่น สมมติว่าคุณมีแคมเปญที่ใช้กลยุทธ์การเสนอราคา 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_micros' => 0
    ]),
    'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);

// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask does 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 succeeds, but does 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(
    MutateCampaignsRequest::build($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
// clears this field from the bidding strategy without impacting any other
// fields on the bidding strategy.
$fieldMask = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->getPaths()[] = '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 อย่างไรก็ตาม เนื่องจาก target_cpa_micros ไม่ใช่ฟิลด์ optional การตั้งค่าเป็น 0 โดยไม่ได้เพิ่มเส้นทางไปยังฟิลด์มาสก์อย่างชัดเจนจะไม่อัปเดตกลยุทธ์การเสนอราคาให้เป็น target_cpa_micros