फ़ील्ड मास्क का इस्तेमाल करके अपडेट करने की सुविधा

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

फ़ील्ड मास्क हेल्पर

फ़ील्ड मास्क जनरेट करने का सुझाव है, google.api_core पैकेज में शामिल field_mask हेल्पर फ़ंक्शन का इस्तेमाल करना. यह दो प्रोटोबफ़ ऑब्जेक्ट को स्वीकार करता है और list फ़ील्ड के साथ एक फ़ील्ड मास्क ऑब्जेक्ट दिखाता है. इस फ़ील्ड में, दो ऑब्जेक्ट के बीच अलग-अलग फ़ील्ड होते हैं.

अगर None को पहले पैरामीटर के तौर पर पास किया जाता है, तो फ़ील्ड मास्क सूची में दूसरे प्रोटोबफ़ ऑब्जेक्ट पर वे सभी फ़ील्ड शामिल होंगे जो अपनी डिफ़ॉल्ट वैल्यू पर सेट नहीं हैं.

फ़ील्ड मास्क बनाने के बाद, उसे सर्वर पर भेजे जाने वाले ऑपरेशन ऑब्जेक्ट पर कॉपी किया जाना चाहिए:

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

from google.api_core import protobuf_helpers
from google.ads.googleads.client import GoogleAdsClient

# Retrieve a GoogleAdsClient instance.
client = GoogleAdsClient.load_from_storage()
# Create a new campaign operation.
campaign_operation = client.get_type('CampaignOperation')
# Retrieve a new campaign object from its update field.
campaign = campaign_operation.update
# Mutate the campaign.
campaign.network_settings.target_search_network.value = False

# Create a field mask using the updated campaign.
# The field_mask helper is only compatible with raw protobuf message
# instances, which we can access using the ._pb attribute.
field_mask = protobuf_helpers.field_mask(None, campaign._pb)

# Copy the field_mask onto the operation's update_mask field.
client.copy_from(campaign_operation.update_mask, field_mask)

सबसे पहले, हम एक खाली CampaignOperation ऑब्जेक्ट बनाते हैं. फिर, हम उससे एक खाली कैंपेन ऑब्जेक्ट को फ़ेच करते हैं. इसके बाद, हम उस कैंपेन ऑब्जेक्ट को अपडेट करके एक नया फ़ील्ड मास्क बनाते हैं और इसकी तुलना None से करते हैं. इससे एक ऐसा फ़ील्ड मास्क सूची जनरेट होगी जिसमें सिर्फ़ बदला गया network_settings.target_search_network फ़ील्ड होगा.

यहां किसी मौजूदा कैंपेन को अपडेट करने का उदाहरण दिया गया है. यहां हम मान लेते हैं कि स्क्रिप्ट से resource_name पैरामीटर दिया गया है, जो किसी कैंपेन के लिए रिसॉर्स का मान्य नाम और मान्य customer_id है:

import proto

from google.api_core import protobuf_helpers
from google.ads.googleads.client import GoogleAdsClient

# Retrieve a GoogleAdsClient instance.
client = GoogleAdsClient.load_from_storage()
# Retrieve an instance of the GoogleAdsService.
googleads_service = client.get_service('GoogleAdsService')

# Search query to retrieve campaign.
query = f"""
    SELECT
      campaign.network_settings.target_search_network,
      campaign.resource_name
    FROM campaign
    WHERE campaign.resource_name = {resource_name}"""

# Submit a query to retrieve a campaign instance.
response = googleads_service.search_stream(customer_id=customer_id, query=query)

# Iterate over results to retrieve the campaign.
for batch in response:
    for row in batch.results:
        initial_campaign = row.campaign

# Create a new campaign operation.
campaign_operation = client.get_type('CampaignOperation')
# Set the copied campaign object to a variable for easy reference.
updated_campaign = campaign_operation.update
# Copy the retrieved campaign into the new campaign.
# Here we use the proto.Message.copy_from method because of its simple
# compatibility with the protobuf message instances, which are wrapped
# by the proto-plus library.
proto.Message.copy_from(updated_campaign, initial_campaign)
# Mutate the new campaign.
updated_campaign.network_settings.target_search_network = False

# Create a field mask using the updated campaign.
field_mask = protobuf_helpers.field_mask(
    initial_campaign._pb, updated_campaign._pb
)

# Copy the field mask onto the operation's update_mask field.
# Note that the client's copy_from  method is designed to work with both native
# messages and messages wrapped by proto-plus, so it works here for the
# update_mask, even though it's an instance of the native message class
# google.protobuf.field_mask_pb2.FieldMask.
client.copy_from(campaign_operation.update_mask, field_mask)

इस रणनीति का इस्तेमाल करने पर, updated_campaign उन सभी फ़ील्ड को शेयर करेगा जो एपीआई से वापस लिए गए initial_campaign फ़ील्ड के बारे में होते हैं. जैसे, रिसॉर्स का नाम. जनरेट किया गया फ़ील्ड मास्क, एपीआई को यह बताएगा कि सिर्फ़ network_settings.target_search_network फ़ील्ड को बदलने की ज़रूरत है.