फ़ील्ड मास्क

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 फ़ील्ड को हटाने के लिए बिडिंग की रणनीति को अपडेट नहीं करता.