फ़ील्ड मास्क

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

FieldMasks यूटिलिटी

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

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

my $campaign = Google::Ads::GoogleAds::V19::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    status => PAUSED,
    networkSettings =>
      Google::Ads::GoogleAds::V19::Resources::NetworkSettings->new({
        targetSearchNetwork => "false"
      })
    });

my $campaign_operation =
  Google::Ads::GoogleAds::V19::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign)
  });

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

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

FieldMasks::all_set_fields_of(), FieldMasks::field_mask() के लिए एक आसान तरीका है. यह आपके पास किए गए ऑब्जेक्ट की तुलना, उसी क्लास के खाली ऑब्जेक्ट से करता है. इसलिए, ऊपर दिए गए कोड में

field_mask(Google::Ads::GoogleAds::V19::Resources::Campaign->new({}), $campaign)

का इस्तेमाल करें.all_set_fields_of($campaign)

मैन्युअल तरीके से मास्क बनाना

फ़ील्ड मास्क को शुरू से बनाने के लिए, आपको पहले एक Google::Ads::GoogleAds::Common::FieldMask ऑब्जेक्ट बनाना होगा. इसके बाद, उन सभी फ़ील्ड के नामों से भरा हुआ कलेक्शन रेफ़रंस बनाएं जिनमें आपको बदलाव करना है. आखिर में, कलेक्शन रेफ़रंस को फ़ील्ड मास्क के paths फ़ील्ड में असाइन करें.

my $field_mask = Google::Ads::GoogleAds::Common::FieldMask->new({
    paths => ["status", "name"]
  });

ऑब्जेक्ट फ़ील्ड और उनके सब-फ़ील्ड अपडेट करना

ऑब्जेक्ट फ़ील्ड में सब-फ़ील्ड हो सकते हैं. जैसे, MaximizeConversions में तीन सब-फ़ील्ड हैं: target_cpa_micros, cpc_bid_ceiling_micros, और cpc_bid_floor_micros. इसके अलावा, ऑब्जेक्ट फ़ील्ड में कोई सब-फ़ील्ड भी नहीं हो सकता. जैसे, ManualCpm.

ऐसे ऑब्जेक्ट फ़ील्ड जिनमें कोई सब-फ़ील्ड नहीं है

Perl में ऑब्जेक्ट फ़ील्ड, gRPC पर चलने वाली क्लाइंट लाइब्रेरी में मौजूद protobuf MESSAGE के बराबर होता है. किसी ऐसे ऑब्जेक्ट फ़ील्ड को अपडेट करते समय जिसे किसी भी सबफ़ील्ड के साथ तय नहीं किया गया है, ऊपर बताए गए तरीके से फ़ील्ड मास्क जनरेट करने के लिए, FieldMasks की सुविधा का इस्तेमाल करें.

तय किए गए सब-फ़ील्ड वाले ऑब्जेक्ट फ़ील्ड

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

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

इस मामले में, FieldMasks की सुविधा के all_set_fields_of() और field_mask() तरीकों का इस्तेमाल करने से, मकसद पूरा नहीं होता.

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

# Creates a campaign with the proper resource name and an empty
# MaximizeConversions field.
my $campaign = Google::Ads::GoogleAds::V19::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V19::Resources::MaximizeConversions->new()
    });

# Constructs an operation, using the FieldMasks' all_set_fields_of utility to
# derive the update mask. The field mask will include 'maximize_conversions',
# which will produce a FieldMaskError.FIELD_HAS_SUBFIELDS error.
my $campaign_operation =
  Google::Ads::GoogleAds::V19::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign)
  });

# Sends the operation in a mutate request that will result in a
# FieldMaskError.FIELD_HAS_SUBFIELDS error because empty object fields cannot
# be included in a field mask.
my $response = $api_client->CampaignService()->mutate({
    customerId => $customer_id,
    operations => [$campaign_operation]
  });

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

# Creates a campaign with the proper resource name.
my $campaign = Google::Ads::GoogleAds::V19::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    )
  });

# 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.
# 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
my $field_mask = all_set_fields_of($campaign);
push @{$field_mask->{paths}}, "maximize_conversions.target_cpa_micros";

# Creates an operation to update the campaign with the specified fields.
my $campaign_operation =
  Google::Ads::GoogleAds::V19::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => $field_mask
  });

फ़ील्ड मिटाना

फ़ील्ड को साफ़ तौर पर हटाया जा सकता है. इसके लिए, उन्हें ऊपर दिखाए गए फ़ील्ड मास्क में जोड़ें या फ़ील्ड को खाली या बिना तय की गई वैल्यू पर सेट करें. उदाहरण के लिए, मान लें कि आपके पास एक कैंपेन है, जो MaximizeConversions बिडिंग की रणनीति का इस्तेमाल करता है और target_cpa_micros फ़ील्ड को 0 से ज़्यादा वैल्यू पर सेट किया गया है.

# Creates a campaign with the proper resource name and a MaximizeConversions
# object with target_cpa_micros set to 0.
my $campaign =
  Google::Ads::GoogleAds::V19::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V19::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    ),
    maximizeConversions => Google::Ads::GoogleAds::V19::Resources::MaximizeConversions->new({
      targetCpaMicros => 0
    })
  });

# Constructs an operation, using the FieldMasks' all_set_fields_of utility to
# derive the update mask, which will include 'maximize_conversions.target_cpa_micros'.
my $campaign_operation =
  Google::Ads::GoogleAds::V19::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign)
  });

ध्यान दें कि नेस्ट किए गए सब-फ़ील्ड वाले फ़ील्ड को सिर्फ़ हर सब-फ़ील्ड को मिटाकर ही खाली किया जा सकता है. इस बारे में तय किए गए सब-फ़ील्ड वाले ऑब्जेक्ट फ़ील्ड में बताया गया है.