फ़ील्ड मास्क

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 protocol buffers में optional के तौर पर तय किए गए फ़ील्ड के लिए ठीक से काम करता है. हालांकि, target_cpa_micros optional फ़ील्ड नहीं है, इसलिए "गलत" उदाहरण से target_cpa फ़ील्ड को हटाने के लिए, बिडिंग की रणनीति को अपडेट नहीं किया जाता.