Di Google Ads API, pembaruan dilakukan menggunakan mask kolom. Mask kolom mencantumkan semua kolom yang ingin Anda ubah dengan update, dan kolom tertentu yang tidak ada di mask kolom akan diabaikan, meskipun dikirim ke server.
Bantuan mask kolom
Cara yang direkomendasikan untuk membuat mask kolom adalah dengan menggunakan fungsi
pembantu field_mask
yang disertakan dalam paket google.api_core
. Metode ini menerima dua objek protobuf dan menampilkan objek mask kolom dengan kolom list
yang berisi semua kolom yang berbeda di antara kedua objek.
Jika None
diteruskan sebagai parameter pertama, daftar mask kolom hanya akan berisi semua kolom pada objek protobuf kedua yang tidak ditetapkan ke nilai defaultnya.
Setelah dibuat, objek mask kolom harus disalin ke objek operasi yang akan dikirim ke server:
Berikut contoh untuk memperbarui kampanye:
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)
Pertama, kita membuat objek CampaignOperation kosong. Kemudian, kita menetapkan pengambilan
objek Kampanye kosong darinya. Kemudian, kita memperbarui objek kampanye tersebut dan membuat mask kolom baru, membandingkannya dengan None
, yang akan menghasilkan daftar mask kolom yang hanya berisi kolom network_settings.target_search_network
yang diubah.
Berikut contoh cara memperbarui kampanye yang ada. Di sini kita mengasumsikan bahwa skrip telah
diberi parameter resource_name
yang merupakan nama resource yang valid untuk
kampanye dan customer_id
yang valid:
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)
Dengan strategi ini, updated_campaign
akan membagikan semua kolom yang sama dengan
initial_campaign
yang diambil dari API, yaitu nama resource.
Mask kolom yang dihasilkan akan memberi tahu API bahwa hanya kolom
network_settings.target_search_network
yang perlu diubah.