في 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
، حتى تعرف واجهة برمجة التطبيقات الحملة التي يتم
تعديلها. تم ضبط 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
.