फ़ील्ड मास्क

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

फ़ील्डमास्क की सुविधा

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

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

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

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

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

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

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

field_mask(Google::Ads::GoogleAds::V16::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).

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

पर्ल में कोई ऑब्जेक्ट फ़ील्ड, gRPC पर चल रही क्लाइंट लाइब्रेरी के प्रोटोबफ़ 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::V16::Resources::Campaign->new({
    resourceName =>
      Google::Ads::GoogleAds::V16::Utils::ResourceNames::campaign(
        $customer_id, $campaign_id
      ),
    maximizeConversions =>
      Google::Ads::GoogleAds::V16::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::V16::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::V16::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V16::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::V16::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::V16::Resources::Campaign->new({
    resourceName => Google::Ads::GoogleAds::V16::Utils::ResourceNames::campaign(
      $customer_id, $campaign_id
    ),
    maximizeConversions => Google::Ads::GoogleAds::V16::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::V16::Services::CampaignService::CampaignOperation->new({
    update     => $campaign,
    updateMask => all_set_fields_of($campaign)
  });

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