Google Ads API में, फ़ील्ड मास्क का इस्तेमाल करके अपडेट किए जाते हैं. फ़ील्ड मास्क में उन सभी फ़ील्ड की सूची होती है जिनमें आपको अपडेट के साथ बदलाव करना है. साथ ही, फ़ील्ड मास्क में शामिल नहीं किए गए किसी भी फ़ील्ड को अनदेखा कर दिया जाता है. भले ही, उसे सर्वर पर भेजा गया हो. मैन्युअल तरीके से फ़ील्ड मास्क बनाने के लिए, FieldMask (Google\Protobuf\FieldMask) बनाएं. इसके बाद, उन सभी फ़ील्ड के नामों से एक कलेक्शन बनाएं जिनमें आपको बदलाव करना है. इसके बाद, उस कलेक्शन को फ़ील्ड मास्क के paths फ़ील्ड में असाइन करें.
हमारी पहले से मौजूद फ़ील्ड मास्क सुविधा (FieldMasks) का भी इस्तेमाल किया जा सकता है. यह सुविधा, कई खास जानकारी को छिपाती है. साथ ही, इकाई के फ़ील्ड में किए गए बदलावों की जांच करके, फ़ील्ड मास्क अपने-आप जनरेट करती है.
यहां कैंपेन अपडेट करने का एक उदाहरण दिया गया है:
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
यह कोड पहले एक Campaign ऑब्जेक्ट बनाता है. इसके बाद, ResourceNames का इस्तेमाल करके, इसके संसाधन का नाम सेट करता है, ताकि एपीआई को पता चल सके कि कौनसा कैंपेन अपडेट किया जा रहा है. status को भी CampaignStatus::PAUSED पर सेट किया गया है.
इसके बाद, कोड एक CampaignOperation ऑब्जेक्ट बनाता है और उस पर पहले से बनाए गए कैंपेन को सेट करता है. इसके बाद, यह FieldMasks::allSetFieldsOf() का इस्तेमाल करके, कैंपेन के लिए फ़ील्ड मास्क बनाता है. इसके लिए, यह बदले गए सभी फ़ील्ड की गिनती करता है. आखिर में, यह कैंपेन ऑपरेशन ऑब्जेक्ट को रिटर्न किया गया मास्क पास करता है.
ध्यान दें कि FieldMasks::allSetFieldsOf(), FieldMasks::compare() के लिए एक आसान तरीका है. यह आपके पास किए गए ऑब्जेक्ट की तुलना, उसी क्लास के खाली ऑब्जेक्ट से करता है. उदाहरण के लिए, पिछले कोड में, FieldMasks::allSetFieldsOf($campaign) के बजाय FieldMasks::compare(new Campaign(), $campaign) का इस्तेमाल किया जा सकता था.
मैसेज फ़ील्ड और उनके सब-फ़ील्ड अपडेट करना
MESSAGE फ़ील्ड में सब-फ़ील्ड हो सकते हैं. जैसे, MaximizeConversions, जिसमें target_cpa_micros, cpc_bid_ceiling_micros, और cpc_bid_floor_micros जैसे सब-फ़ील्ड शामिल हैं. इसके अलावा, ऐसा भी हो सकता है कि उनमें कोई सब-फ़ील्ड न हो. जैसे, ManualCpm.
मैसेज फ़ील्ड, जिनमें कोई सबफ़ील्ड तय नहीं किया गया है
किसी ऐसे MESSAGE फ़ील्ड को अपडेट करते समय जिसे किसी सब-फ़ील्ड के साथ तय नहीं किया गया है, फ़ील्ड मास्क जनरेट करने के लिए FieldMasks का इस्तेमाल करें. इसके बारे में पहले बताया गया है.
तय किए गए सब-फ़ील्ड वाले मैसेज फ़ील्ड
किसी ऐसे MESSAGE फ़ील्ड को अपडेट करते समय जिसे सब-फ़ील्ड के साथ तय किया गया है, लेकिन उस मैसेज पर किसी भी सब-फ़ील्ड को साफ़ तौर पर सेट नहीं किया गया है, तो आपको हर बदलाव किए जा सकने वाले MESSAGE सब-फ़ील्ड को FieldMask में मैन्युअल तरीके से जोड़ना होगा. यह पिछले उदाहरण की तरह ही है, जिसमें फ़ील्ड मास्क को शुरुआत से बनाया गया था.
इसका एक सामान्य उदाहरण है, बिडिंग की नई रणनीति के किसी भी फ़ील्ड को सेट किए बिना, कैंपेन की बिडिंग की रणनीति को अपडेट करना. यहां दिए गए कोड से पता चलता है कि बिडिंग की रणनीति पर कोई भी सबफ़ील्ड सेट किए बिना, MaximizeConversions बिडिंग की रणनीति का इस्तेमाल करने के लिए, कैंपेन को कैसे अपडेट किया जाए.
इस मामले में, FieldMasks के allSetFieldsOf() और compare() तरीकों का इस्तेमाल करने से, तय किया गया लक्ष्य हासिल नहीं होता.
नीचे दिया गया कोड, एक फ़ील्ड मास्क जनरेट करता है जिसमें 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 includes 'maximize_conversions',
// which produces 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 results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build($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 the mutable
// subfield 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 sets 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:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
$fieldMask->getPaths()[] = '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);
फ़ील्ड में मौजूद कॉन्टेंट मिटाना
कुछ फ़ील्ड को साफ़ तौर पर मिटाया जा सकता है. पिछले उदाहरण की तरह, आपको इन फ़ील्ड को फ़ील्ड मास्क में साफ़ तौर पर जोड़ना होगा. ऐसा इसलिए, क्योंकि FieldMasks, Proto3 स्केलर फ़ील्ड को उनकी डिफ़ॉल्ट वैल्यू पर सेट करता है. जैसे, 0, false या "". उदाहरण के लिए, मान लें कि आपके पास एक ऐसा कैंपेन है जो 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_micros' => 0
]),
'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask does 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 succeeds, but does 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(
MutateCampaignsRequest::build($customerId, [$campaignOperation])
);
यहां दिए गए कोड से पता चलता है कि MaximizeConversions बिडिंग की रणनीति में, target_cpa_micros फ़ील्ड को सही तरीके से कैसे हटाया जा सकता है.
// 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
// clears this field from the bidding strategy without impacting any other
// fields on the bidding strategy.
$fieldMask = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->getPaths()[] = '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);
ध्यान दें कि डिफ़ॉल्ट वैल्यू सेट करने से, उन फ़ील्ड के लिए उम्मीद के मुताबिक काम होता है जिन्हें Google Ads API के प्रोटोकॉल बफ़र में optional के तौर पर तय किया गया है. हालांकि, target_cpa_micros एक optional फ़ील्ड नहीं है. इसलिए, फ़ील्ड मास्क में पाथ को जोड़े बिना इसे 0 पर सेट करने से, बिडिंग की रणनीति अपडेट नहीं होती है. साथ ही, target_cpa_micros को हटाया नहीं जा सकता.