أقنعة الحقل

في 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" protocol buffers. ولكن نظرًا لأن target_cpa_micros ليس حقل optional، فإن "غير صحيح" الرمز البرمجي لا يعدّل استراتيجية عروض الأسعار لمحو الحقل target_cpa.