أقنعة الحقل

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