फ़ील्ड मास्क

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