إشعارات باستخدام أقنعة الحقول

في Google Ads API، يتم إجراء التعديلات باستخدام قناع حقل. تعرض قائمة قناع الحقل جميع الحقول التي تريد تغييرها باستخدام التعديل، وسيتم تجاهل أي حقول محددة غير مضمّنة في قناع الحقل، حتى إذا تم إرسالها إلى الخادم.

أداة مساعدة لقناع الحقل

الطريقة المقترَحة لإنشاء أقنعة الحقول هي استخدام الدالة المساعدة field_mask المضمّنة في حزمة google.api_core. يقبل هذا الإجراء عنصرَين من عناصر protobuf ويعرض عنصر قناع حقل يتضمّن الحقل 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 فارغًا. بعد ذلك، نضبط استرداد كائن Campaign فارغ منه. بعد ذلك، نعدّل عنصر الحملة هذا وننشئ قناع حقل جديدًا، ونقارنه بـ 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 فقط.