การอัปเดตโดยใช้มาสก์ฟิลด์

ใน Google Ads API การอัปเดตจะทำโดยใช้ฟิลด์มาสก์ ฟิลด์มาสก์จะแสดงช่องทั้งหมดที่คุณตั้งใจจะเปลี่ยนแปลงเมื่อมีการอัปเดต และระบบจะไม่สนใจช่องที่ระบุซึ่งไม่ได้อยู่ในมาสก์ของช่อง แม้ว่าจะส่งไปที่เซิร์ฟเวอร์ก็ตาม

ตัวช่วยมาสก์ฟิลด์

วิธีที่แนะนำในการสร้างมาสก์ของช่องคือการใช้ฟังก์ชันตัวช่วย field_mask ที่รวมอยู่ในแพ็กเกจ google.api_core โดยรับออบเจ็กต์ Protobuf 2 รายการและส่งออบเจ็กต์มาสก์ฟิลด์ที่มีช่อง list ซึ่งประกอบด้วยช่องทั้งหมดที่ต่างกันระหว่างออบเจ็กต์ทั้ง 2 รายการ

หากส่งผ่าน None เป็นพารามิเตอร์แรก รายการมาสก์ของช่องจะมีเพียงทุกช่องในออบเจ็กต์ protobuf ที่ 2 ที่ไม่ได้ตั้งค่าเป็นค่าเริ่มต้น

เมื่อสร้างแล้ว ออบเจ็กต์มาสก์ฟิลด์ควรคัดลอกไปยังออบเจ็กต์การดำเนินการที่จะส่งไปยังเซิร์ฟเวอร์:

ตัวอย่างการอัปเดตแคมเปญมีดังนี้

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)

ก่อนอื่น เราจะสร้างออบเจ็กต์ CampaignOperating ที่ว่างเปล่า จากนั้น เราจะตั้งค่า ออบเจ็กต์แคมเปญที่ว่างเปล่า จากนั้นเราจะอัปเดตออบเจ็กต์แคมเปญดังกล่าวและสร้างฟิลด์มาสก์ใหม่ เปรียบเทียบกับ 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 เท่านั้น