फ़ील्ड मास्क

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

FieldMaskUtil

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

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

// Creates a Campaign object with the proper resource name and any other changes.
Campaign campaign =
    Campaign.newBuilder()
        .setResourceName(ResourceNames.campaign(customerId, campaignId))
        .setStatus(CampaignStatus.PAUSED)
        .build();

// Constructs an operation that will update the campaign, using the FieldMasks'
// allSetFieldsOf utility to derive the update mask. This mask tells the Google
// Ads API which attributes of the campaign you want to change.
CampaignOperation operation =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
        .build();

// Sends the operation in a mutate request.
MutateCampaignsResponse response =
    campaignServiceClient.mutateCampaigns(
        customerId.toString(), Collections.singletonList(operation));

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

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

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

Campaign existingCampaign;

// Obtains existingCampaign from elsewhere.
...

// Creates a new campaign based off the existing campaign and updates the
// campaign by setting its status to paused.
Campaign campaignToUpdate =
    existingCampaign.toBuilder()
        .setStatus(CampaignStatus.PAUSED)
        .build();

// Constructs an operation that will update the campaign, using the FieldMasks'
// compare utility to derive the update mask. This mask tells the Google Ads API
// which attributes of the campaign you want to change.
CampaignOperation operation =
    CampaignOperation.newBuilder()
        .setUpdate(campaignToUpdate)
        .setUpdateMask(FieldMasks.compare(existingCampaign, campaignToUpdate))
        .build();

// Sends the operation in a mutate request.
MutateCampaignsResponse response =
    campaignServiceClient.mutateCampaigns(
        customerId.toString(), Collections.singletonList(operation));

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

FieldMask fieldMask =
    FieldMask.newBuilder()
        .addPaths("status")
        .addPaths("name")
        .build();

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

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

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

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

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

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

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

इस मामले में, FieldMaskUtil के allSetFieldsOf() और compare() तरीकों का इस्तेमाल करने से वह लक्ष्य हासिल नहीं होगा जो सही होगा.

इस उदाहरण में एक ऐसा फ़ील्ड मास्क जनरेट किया गया है जिसमें maximize_conversions शामिल है. हालांकि, Google Ads API इस तरह से फ़ील्ड को गलती से मिटाए जाने से रोकने की अनुमति नहीं देता और यह FieldMaskError.FIELD_HAS_SUBFIELDS गड़बड़ी पैदा करता है.

// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = Campaign.newBuilder()
    .setResourceName(ResourceNames.campaign(customerId, campaignId))
    .setMaximizeConversions(MaximizeConversions.newBuilder().build())
    .build();

// 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 =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
        .build();

// 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 =
    campaignServiceClient.mutateCampaigns(
        customerId.toString(), Collections.singletonList(operation));

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

// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
    .setResourceName(ResourceNames.campaign(customerId, campaignId))
    .build();

// Creates a field mask from the existing campaign and adds all of the mutable
// fields (only one in this case) on the MaximizeConversions bidding strategy to
// the field mask. Because this field is 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 without any of its subfields
// set.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
    .toBuilder()
    // 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
    .addPaths("maximize_conversions.target_cpa_micros")
    .build();

// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(fieldMask)
        .build();

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

कुछ फ़ील्ड को साफ़ तौर पर मिटाया जा सकता है. ऊपर दिए गए उदाहरण की तरह ही, आपको इन फ़ील्ड को फ़ील्ड मास्क में साफ़ तौर पर जोड़ना होगा. उदाहरण के लिए, मान लें कि आपके पास एक ऐसा कैंपेन है जिसमें 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 =
    Campaign.newBuilder()
        .setResourceName(ResourceNames.campaign(customerId, campaignId))
        .setMaximizeConversions(
            MaximizeConversions.newBuilder().setTargetCpa(0).build())
        .setStatus(CampaignStatus.PAUSED)
        .build();

// 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 =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
        .build();

// 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 =
    campaignServiceClient.mutateCampaigns(
        customerId.toString(), Collections.singletonList(operation));

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

// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
    .setResourceName(ResourceNames.campaign(customerId, campaignId))
    .build();

// 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)
    .toBuilder()
    .addPaths("maximize_conversions.target_cpa_micros")
    .build();

// Creates an operation to update the campaign with the specified field.
CampaignOperation operation =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(fieldMask))
        .build();

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