أقنعة الحقل

في 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".