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 फ़ील्ड को अपडेट करना है जिसे सब-फ़ील्ड के साथ तय किया गया है और आपने उस मैसेज में किसी भी सब-फ़ील्ड को साफ़ तौर पर सेट नहीं किया है, तो आपको FieldMask में हर बदलाव किए जा सकने वाले MESSAGE सब-फ़ील्ड को मैन्युअल तरीके से जोड़ना होगा. यह ठीक उसी तरह से करना होगा जैसे पिछले उदाहरण में, फ़ील्ड मास्क को शुरू से बनाया गया था.
इसका एक सामान्य उदाहरण यह है कि नई बिडिंग की रणनीति के किसी भी फ़ील्ड को सेट किए बिना, कैंपेन की बिडिंग की रणनीति को अपडेट करना. नीचे दिए गए कोड में, बिडिंग की रणनीति के किसी भी सब-फ़ील्ड को सेट किए बिना, कैंपेन को 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 साफ़ नहीं होता.