في 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));
ينشئ هذا الرمز أولاً عنصر Campaign ثم يضبط اسم المورد الخاص به باستخدام
ResourceNames
، لكي تعرف واجهة برمجة التطبيقات الحملة التي يتم تعديلها. تم أيضًا ضبط status
على PAUSED
.
ينشئ الرمز بعد ذلك عنصر CampaignOperation
ويضبط الحملة التي تم إنشاؤها سابقًا عليه. بعد ذلك، تستخدِم هذه الطريقة
FieldMasks::allSetFieldsOf()
لإنشاء قناع حقل للحملة باستخدام جميع الحقول التي تم تغييرها. وأخيرًا، يتم تمرير القناع الذي تم إرجاعه إلى عنصر عملية الحملة.
يُرجى العِلم أنّ
FieldMasks::allSetFieldsOf
هي طريقة ملائمة لإجراء
FieldMasks::compare()
.
وتتم مقارنة العنصر الذي تم تمريره بعنصر فارغ من الفئة نفسها. على سبيل المثال، في الرمز السابق، كان بإمكانك استخدام FieldMasks::compare(new
Campaign(), $campaign)
بدلاً من allSetFieldsOf()
.
تعديل حقول الرسائل والحقول الفرعية
يمكن أن تتضمّن حقول 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 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
.