फ़ील्ड मास्क

Google Ads API में, फ़ील्ड मास्क का इस्तेमाल करके अपडेट किए जाते हैं. फ़ील्ड मास्क में उन सभी फ़ील्ड की सूची होती है जिन्हें आपको अपडेट के साथ बदलना है. साथ ही, ऐसा कोई भी फ़ील्ड जो फ़ील्ड मास्क में नहीं है, उसे अनदेखा कर दिया जाएगा, भले ही उसे सर्वर पर भेजा गया हो.

FieldMaskUtil

हमारा सुझाव है कि फ़ील्ड मास्क जनरेट करने के लिए, पहले से मौजूद फ़ील्ड मास्क की सुविधा का इस्तेमाल करें. इससे फ़ील्ड मास्क, शुरुआत से बनाने के बजाय, बदले गए ऑब्जेक्ट से जनरेट किए जा सकते हैं.

यहां कैंपेन अपडेट करने का एक उदाहरण दिया गया है:

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

सबसे पहले, हम एक खाली Campaign ऑब्जेक्ट बनाते हैं. फिर, हम इसके संसाधन का नाम सेट करते हैं, ताकि एपीआई को पता चल सके कि हम किस कैंपेन को अपडेट कर रहे हैं.

इस उदाहरण में, कैंपेन पर FieldMasks.AllSetFieldsOf तरीके का इस्तेमाल किया गया है. यह अपने-आप एक फ़ील्ड मास्क बनाता है, जो सभी सेट फ़ील्ड को शामिल करता है. इसके बाद, लौटाए गए मास्क को सीधे अपडेट कॉल में पास किया जा सकता है.

कभी-कभी, आपको किसी मौजूदा ऑब्जेक्ट के साथ काम करने और कुछ फ़ील्ड अपडेट करने की ज़रूरत हो सकती है. ऐसे मामलों में, आपको कोड में इस तरह से बदलाव करना होगा:

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

शुरुआत से फ़ील्ड मास्क बनाने के लिए, आपको सबसे पहले FieldMask ऑब्जेक्ट बनाना होगा. इसके बाद, आपको उन सभी फ़ील्ड के नामों से एक अरे बनानी होगी जिनमें बदलाव करना है. आखिर में, अरे के कॉन्टेंट को फ़ील्ड मास्क के Path फ़ील्ड में जोड़ना होगा.

FieldMask fieldMask = new FieldMask();
fieldMask.Paths.AddRange(new string[] { "status", "name" });

मैसेज के फ़ील्ड और उनके सबफ़ील्ड अपडेट किए जा रहे हैं

MESSAGE फ़ील्ड में सबफ़ील्ड हो सकते हैं (जैसे कि MaximizeConversions, जिसमें तीन: target_cpa_micros, cpc_bid_ceiling_micros, और cpc_bid_floor_micros होते हैं) या इनमें कोई भी नहीं हो सकता (जैसे, ManualCpm).

बिना तय सबफ़ील्ड वाले मैसेज फ़ील्ड

ऐसे MESSAGE फ़ील्ड को अपडेट करते समय जिसे किसी भी सबफ़ील्ड के साथ तय नहीं किया गया है, फ़ील्ड मास्क जनरेट करने के लिए FieldMasks का इस्तेमाल करें. इसका तरीका ऊपर बताया गया है.

तय किए गए सबफ़ील्ड वाले मैसेज फ़ील्ड

मैसेज में किसी भी सबफ़ील्ड को साफ़ तौर पर सेट किए बिना, सबफ़ील्ड के साथ तय किए गए MESSAGE फ़ील्ड को अपडेट करते समय, आपको हर म्यूटेबल MESSAGE सबफ़ील्ड को FieldMask में मैन्युअल तरीके से जोड़ना होगा. यह, ऊपर दिए गए उदाहरण की तरह ही है, जो शुरुआत से फ़ील्ड मास्क बनाता है.

इसका एक सामान्य उदाहरण है, बोली लगाने की नई रणनीति के लिए कोई भी फ़ील्ड सेट किए बिना कैंपेन की बोली लगाने की रणनीति को अपडेट करना. नीचे दिए गए उदाहरण में बताया गया है कि बिडिंग की रणनीति के किसी भी सबफ़ील्ड को सेट किए बिना, कैंपेन को MaximizeConversions बिडिंग की रणनीति का इस्तेमाल करने के लिए कैसे अपडेट करें.

इस मामले में, FieldMasks के AllSetFieldsOf() और FromChanges() तरीकों का इस्तेमाल करने से लक्ष्य हासिल नहीं होगा.

इस उदाहरण में एक ऐसा फ़ील्ड मास्क जनरेट किया गया है जिसमें maximize_conversions शामिल है. हालांकि, Google Ads API इस तरह से फ़ील्ड को गलती से मिटाए जाने से रोकने की अनुमति नहीं देता और यह 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()
};

// Constructs an operation, using the FieldMasks' AllSetFieldsOf utility to
// derive the update mask. The field mask will include 'maximize_conversions`,
// which will produce a FieldMaskError.FIELD_HAS_SUBFIELDS error.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
};

// Sends the operation in a mutate request that will result in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

इस उदाहरण में, किसी भी सबफ़ील्ड को सेट किए बिना, MaximizeConversions बिडिंग की रणनीति का इस्तेमाल करने के लिए, कैंपेन को सही तरीके से अपडेट करने का तरीका बताया गया है.

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
};

// Creates a field mask from the existing campaign and adds all of the fields
// on the MaximizeConversions bidding strategy to the field mask. Because these
// fields are included in the field mask but excluded from the campaign object,
// the Google Ads API will set the campaign's bidding strategy to a
// MaximizeConversions object with none of its subfields set.
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
};

फ़ील्ड खाली करना

कुछ फ़ील्ड को साफ़ तौर पर मिटाया जा सकता है. ऊपर दिए गए उदाहरण की तरह ही, आपको इन फ़ील्ड को फ़ील्ड मास्क में साफ़ तौर पर जोड़ना होगा. उदाहरण के लिए, मान लें कि आपके पास एक ऐसा कैंपेन है जिसमें 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 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 });

अगले उदाहरण में, MaximizeConversions बिडिंग की रणनीति में 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 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
};

ध्यान रखें कि ऊपर दिया गया "गलत" उदाहरण, उन फ़ील्ड के लिए उम्मीद के मुताबिक काम करता है जिन्हें Google Ads API में optional के तौर पर बताया गया है protocol buffers. हालांकि, target_cpa_micros एक optional फ़ील्ड नहीं है, इसलिए "गलत" उदाहरण, target_cpa फ़ील्ड को हटाने के लिए बिडिंग की रणनीति को अपडेट नहीं करता.