עדכונים באמצעות מסכות שדה

ב-Google Ads API, העדכונים מתבצעים באמצעות מסכת שדות. במסכת השדות מפורטים כל השדות שרוצים לשנות בעדכון, וכל השדות שצוינו שלא נמצאים במסכת השדות יתעלמו מהם, גם אם הם נשלחו לשרת.

כלי עזר לאנונימיזציה של שדות

הדרך המומלצת ליצור מסכות שדות היא באמצעות פונקציית העזר field_mask שכלולה בחבילה google.api_core. היא מקבלת שני אובייקטים של protobuf ומחזירה אובייקט של מסכת שדות עם שדה list שמכיל את כל השדות ששונים בין שני האובייקטים.

אם מעבירים את None כפרמטר הראשון, רשימת מסכות השדות תכיל רק את כל השדות באובייקט השני של protobuf שלא מוגדרים לערך ברירת המחדל שלהם.

אחרי שיוצרים את אובייקט האנונימיזציה של השדה, צריך להעתיק אותו לאובייקט הפעולה שיישלח לשרת:

דוגמה לעדכון קמפיין:

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 שאוחזר מה-API, כלומר שם המשאב. האנונימיזציה של השדה שנוצרה תציין ל-API שצריך לשנות רק את השדה network_settings.target_search_network.