Google Ads API では、フィールド マスクを使用して更新を行います。フィールド マスクには、更新で変更するすべてのフィールドが一覧表示されます。フィールド マスクに含まれていない指定フィールドは、サーバーに送信された場合でも無視されます。
フィールド マスク ヘルパー
フィールド マスクを生成するには、google.api_core
パッケージに含まれる field_mask
ヘルパー関数を使用することをおすすめします。2 つの protobuf オブジェクトを受け取り、2 つのオブジェクト間で異なるフィールドをすべて含む list
フィールドを持つフィールド マスク オブジェクトを返します。
最初のパラメータとして None
が渡された場合、フィールド マスクのリストには、デフォルト値に設定されていない 2 番目の 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
と同じフィールド、つまりリソース名を共有します。生成されたフィールド マスクは、network_settings.target_search_network
フィールドのみを変更する必要があることを API に伝えます。