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 });