使用字段掩码进行更新

在 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 将与 从 API 中检索到的 initial_campaign,即资源名称。 生成的字段掩码会告知 API 只有 network_settings.target_search_network字段需要更改。