फ़ील्ड मास्क

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 बिडिंग की रणनीति सबफ़ील्ड सेट किए बिना.

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

नीचे दिया गया उदाहरण एक फ़ील्ड मास्क जनरेट करता है, जिसमें ये शामिल हैं 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 });

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

// 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 फ़ील्ड नहीं है, यह "गलत" है उदाहरण not करता है target_cpa फ़ील्ड को खाली करने के लिए बोली-प्रक्रिया कार्यनीति.