फ़ील्ड मास्क

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

पहले से मौजूद फ़ील्ड मास्क यूटिलिटी (FieldMasks) का भी इस्तेमाल किया जा सकता है. इससे खास जानकारी छिपी हुई है. साथ ही, इकाई के फ़ील्ड में किए गए बदलावों को मॉनिटर करके, अपने-आप फ़ील्ड मास्क जनरेट किए जा सकते हैं.

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

    $campaign = new Campaign([
        'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
        'status' => CampaignStatus::PAUSED
    ]);

    $campaignOperation = new CampaignOperation();
    $campaignOperation->setUpdate($campaign);
    $campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));

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

इसके बाद, कोड एक CampaignOperation ऑब्जेक्ट बनाता है और उस पर पहले बनाया गया कैंपेन सेट करता है. इसके बाद, FieldMasks::allSetFieldsOf() का इस्तेमाल करके, सभी बदले गए फ़ील्ड का इस्तेमाल करके कैंपेन के लिए फ़ील्ड मास्क बनाया जा सकता है. आखिर में, यह रिटर्न किए गए मास्क को कैंपेन ऑपरेशन ऑब्जेक्ट में पास करता है.

ध्यान दें कि FieldMasks::compare() के लिए, FieldMasks::allSetFieldsOf एक आसान तरीका है. यह आपके पास किए गए ऑब्जेक्ट की तुलना, उसी क्लास के किसी खाली ऑब्जेक्ट से करता है. उदाहरण के लिए, पिछले कोड में आप allSetFieldsOf() के बजाय FieldMasks::compare(new Campaign(), $campaign) का इस्तेमाल कर सकते थे.

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

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

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

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

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

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

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

इस मामले में, FieldMasks के 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 = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'maximize_conversions' => 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 = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(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.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);

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

// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId)
]);

// 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 = 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->setPaths(array_merge(
    iterator_to_array($fieldMask->getPaths()->getIterator()),
    ['maximize_conversions.target_cpa_micros']
));

// Creates an operation to update the campaign with the specified fields.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($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 = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'maximize_conversions' => new MaximizeConversions(['target_cpa' => 0]),
    'status' => CampaignStatus::PAUSED
]);

// 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 = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(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.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);

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

// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($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 = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->setPaths(array_merge(
    iterator_to_array($fieldMask->getPaths()->getIterator()),
    ['maximize_conversions.target_cpa_micros']
));

// Creates an operation to update the campaign with the specified field.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($fieldMask);

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