Google Ads API में, फ़ील्ड मास्क का इस्तेमाल करके उन फ़ील्ड की सूची दी जाती है जिन्हें एपीआई अनुरोध से अपडेट किया जाना चाहिए. फ़ील्ड मास्क में जो फ़ील्ड तय नहीं किया गया है उसे अनदेखा कर दिया जाता है. भले ही, उसे सर्वर पर भेजा गया हो.
FieldMasks क्लास
.NET क्लाइंट लाइब्रेरी में फ़ील्ड मास्क जनरेट करने का सुझाया गया तरीका, पहले से मौजूद FieldMasks यूटिलिटी क्लास का इस्तेमाल करना है. इससे आपको किसी ऑब्जेक्ट में बदलाव करके फ़ील्ड मास्क जनरेट करने की सुविधा मिलती है. इसके लिए, आपको फ़ील्ड मास्क को नए सिरे से बनाने की ज़रूरत नहीं पड़ती.
यहां किसी कैंपेन को अपडेट करने का उदाहरण दिया गया है. इसमें FieldMasks.AllSetFieldsOf तरीके का इस्तेमाल करके, सेट किए गए सभी फ़ील्ड की गिनती करने वाला फ़ील्ड मास्क जनरेट किया जाता है. इसके बाद, जनरेट किए गए फ़ील्ड मास्क को सीधे अपडेट कॉल में पास किया जा सकता है:
// Update campaign by setting its status to paused, and "Search network" to
// false.
Campaign campaignToUpdate = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
NetworkSettings = new NetworkSettings()
{
TargetSearchNetwork = false
}
};
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Update = campaignToUpdate,
UpdateMask = FieldMasks.AllSetFieldsOf(campaignToUpdate)
};
// Update the campaign.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
कभी-कभी, आपको किसी मौजूदा ऑब्जेक्ट के साथ काम करना पड़ सकता है और कुछ फ़ील्ड अपडेट करने पड़ सकते हैं. ऐसे मामलों में, FieldMasks.FromChanges तरीके का इस्तेमाल करें. यह तरीका, एक फ़ील्ड मास्क जनरेट करता है, जो दो ऑब्जेक्ट के बीच का अंतर दिखाता है:
Campaign existingCampaign;
// Obtain existingCampaign from an earlier API call.
// Create a new campaign based on the existing campaign for update.
Campaign campaignToUpdate = new Campaign(existingCampaign);
// Update campaign by setting its status to paused, and "Search network" to
// false.
campaignToUpdate.Status = CampaignStatus.Paused;
campaignToUpdate.NetworkSettings = new NetworkSettings()
{
TargetSearchNetwork = false
};
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Update = campaignToUpdate,
UpdateMask = FieldMasks.FromChanges(existingCampaign, campaignToUpdate)
};
FieldMaskError.FIELD_HAS_SUBFIELDS गड़बड़ियों को ठीक करना
कभी-कभी, आपको किसी मैसेज फ़ील्ड को उसके किसी भी सबफ़ील्ड को अपडेट किए बिना सेट करना पड़ सकता है. नीचे दिए गए उदाहरण पर ध्यान दें:
// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
MaximizeConversions = new MaximizeConversions()
};
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
यह एपीआई कॉल, FieldMaskError.FIELD_HAS_SUBFIELDS गड़बड़ी के साथ पूरा नहीं होता. MaximizeConversions में सबफ़ील्ड होते हैं. इसलिए, Google Ads API सर्वर को उम्मीद होती है कि अनुरोध में, बदलाव किए जा सकने वाले सबफ़ील्ड के लिए फ़ील्ड मास्क मौजूद हों. हालांकि, इस स्थिति में FieldMasks सबफ़ील्ड मास्क अपने-आप जनरेट नहीं कर सकता, क्योंकि अनुरोध में कोई सबफ़ील्ड सेट नहीं किया गया है.
ऐसे मामलों में, fieldMask.Paths (जो कि एक RepeatedField<string> है) में पाथ को मैन्युअल तरीके से जोड़ा जा सकता है:
// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
};
FieldMask 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 at:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
फ़ील्ड में मौजूद कॉन्टेंट मिटाना
Google Ads API की मदद से, कुछ फ़ील्ड की वैल्यू हटाई जा सकती हैं. किसी फ़ील्ड को खाली करने के लिए, आपको फ़ील्ड मास्क में उस फ़ील्ड को मैन्युअल तरीके से शामिल करना होगा. साथ ही, संसाधन ऑब्जेक्ट पर फ़ील्ड को सेट नहीं करना होगा. किसी फ़ील्ड को उसकी डिफ़ॉल्ट वैल्यू पर सेट करने से, फ़ील्ड की वैल्यू नहीं हटती. जैसे, int64 फ़ील्ड के लिए 0.
यहां दिए गए कोड के उदाहरण में, MaximizeConversions बिडिंग की रणनीति के target_cpa_micros फ़ील्ड को हटाने का तरीका बताया गया है:
सही कोड
यहां दिया गया कोड, target_cpa_micros फ़ील्ड को खाली कर देता है. ऐसा इसलिए होता है, क्योंकि यह campaign.MaximizeConversions.TargetCpaMicros प्रॉपर्टी को सेट किए बिना, फ़ील्ड मास्क में maximize_conversions.target_cpa_micros को जोड़ता है:
// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(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 fieldMask = FieldMasks.AllSetFieldsOf(campaign);
fieldMask.Paths.Add("maximize_conversions.target_cpa_micros");
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = fieldMask
};
कोड गलत है
यहां दिया गया कोड, target_cpa_micros फ़ील्ड को नहीं हटाता, क्योंकि यह फ़ील्ड को 0 पर सेट करता है. जब TargetCpaMicros की वैल्यू 0 हो या मास्क से पाथ को हटा दिया गया हो, तब FieldMasks यूटिलिटी और Google Ads API सर्वर, दोनों इस वैल्यू को अनदेखा कर देते हैं. साथ ही, सर्वर कोई गड़बड़ी नहीं दिखाता:
// Creates a campaign with the proper resource name and a
// MaximizeConversions object. Attempts to clear the target_cpa_micros
// field by setting it to 0.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
MaximizeConversions = new MaximizeConversions()
{
TargetCpaMicros = 0
}
};
// Constructs an operation using FieldMasks.AllSetFieldsOf to derive the
// update mask.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};
// Sends the operation in a mutate request that succeeds without clearing
// the previous 'target_cpa_micros' value cleanly.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });