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 पर सेट करता है. FieldMasks यूटिलिटी और Google Ads API सर्वर, दोनों इस वैल्यू को अनदेखा करते हैं. ऐसा तब होता है, जब TargetCpaMicros, 0 होता है या जब पाथ को मास्क से हटा दिया जाता है और सर्वर कोई गड़बड़ी नहीं दिखाता:
// 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 });