Google Ads API में, फ़ील्ड मास्क का इस्तेमाल उन फ़ील्ड की सूची देने के लिए किया जाता है जिन्हें एपीआई अनुरोध को अपडेट करना चाहिए. फ़ील्ड मास्क में शामिल न किए गए किसी भी फ़ील्ड को अनदेखा कर दिया जाएगा. भले ही, उसे सर्वर पर भेजा गया हो.
FieldMaskUtil क्लास
फ़ील्ड मास्क जनरेट करने का सबसे सही तरीका, पहले से मौजूद FieldMaskUtil
क्लास का इस्तेमाल करना है. इससे, फ़ील्ड मास्क को नए सिरे से बनाने के बजाय, बदले गए ऑब्जेक्ट से जनरेट किया जा सकता है.
यहां कैंपेन को अपडेट करने का एक उदाहरण दिया गया है. इसमें 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 elsewhere.
...
// Create a new campaign based off 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 सर्वर को उम्मीद है कि अनुरोध में उन फ़ील्ड के लिए फ़ील्ड मास्क मौजूद होंगे. हालांकि, इस स्थिति में FieldMaskUtil
फ़ील्ड मास्क को सही तरीके से जनरेट नहीं कर सकता, क्योंकि अनुरोध में इन फ़ील्ड को सेट नहीं किया जा रहा है.
ऐसे मामलों में, फ़ील्ड मास्क में मैन्युअल तरीके से इस तरह बदलाव किया जा सकता है:
// 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 here:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
fieldMask.Paths.AddRange(new string[] {
"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 फ़ील्ड के लिए डिफ़ॉल्ट वैल्यू शून्य होती है.
यहां दिए गए कोड के उदाहरण में, MaximizeConversions
बिडिंग की रणनीति के target_cpa_micros
फ़ील्ड को मिटाने का तरीका बताया गया है.
सही कोड
यहां दिया गया कोड, target_cpa_micros
फ़ील्ड को मिटा देता है, क्योंकि हम फ़ील्ड मास्क में maximize_conversions.target_cpa_micros
को सेट कर रहे हैं और campaign.MaximizeConversions.TargetCpaMicros
फ़ील्ड को सेट नहीं कर रहे हैं.
// 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 will
// clear this field from the bidding strategy without impacting any other fields
// on the bidding strategy.
FieldMask fieldMask = FieldMasks.AllSetFieldsOf(campaign);
fieldMask.Paths.AddRange(new string[] {
"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
फ़ील्ड को खाली नहीं करता, क्योंकि हम इस फ़ील्ड को शून्य पर सेट कर रहे हैं. FieldMaskUtils
और Google Ads API सर्वर, दोनों इस वैल्यू को अनदेखा कर देंगे. इसके अलावा, इस मामले में आपको कोई गड़बड़ी नहीं दिख सकती, क्योंकि सर्वर ने वैल्यू को अनदेखा कर दिया था.
// Creates a campaign with the proper resource name and a MaximizeConversions
// object. Attempt 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 the FieldMasks' AllSetFieldsOf utility to
// derive the update mask. However, the field mask will NOT include
// 'maximize_conversions.target_cpa_micros'.
CampaignOperation operation = new CampaignOperation()
{
Update = campaign,
UpdateMask = 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.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });