將空白訊息物件設為欄位
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Google Ads API 中,部分訊息欄位會定義為空白訊息物件,例如 campaign.manual_cpm
,或是可能只有不需要設定的選用欄位,例如 campaign.manual_cpc
。設定這些欄位很重要,可讓 API 知道要為指定廣告活動使用哪種出價策略,但如果訊息為空白,就不是那麼直覺。
更新 campaign.name
欄位 (字串) 時,我們會直接更新欄位,就像更新一般 Python 物件屬性一樣:
campaign.name = "Test campaign value"
campaign.manual_cpc
是巢狀欄位,表示它包含另一個 protobuf 訊息,而非字串等原始型別。您也可以直接更新其欄位:
campaign.manual_cpc.enhanced_cpc_enabled = True
這會告知 API,這個廣告活動的出價策略為manual_cpc
,且已啟用成本效益管理系統。
但如果想使用空白的 manual_cpm
呢?或者不啟用成本效益管理系統?manual_cpc
如要這麼做,您需要將類別的另一個空白例項複製到廣告活動,例如:
client = GoogleAdsClient.load_from_storage()
empty_cpm = client.get_type('ManualCpm')
client.copy_from(campaign.manual_cpm, empty_cpm)
請注意 manual_cpm
如何為 campaign
物件指定:
name {
value: "Test campaign value"
}
manual_cpm {
}
已設定 manual_cpm
欄位,但其中沒有任何欄位有值。向使用此模式的 API 傳送要求時,您可以啟用記錄並檢查要求酬載,確認您是否正確設定空白訊息物件。
最後,您需要手動將這個欄位新增至要求物件的 update_mask
。欄位遮罩輔助函式無法判斷欄位是明確設為空物件,還是未設定。
from google.api_core.protobuf_helpers import field_mask
campaign_operation.create = campaign
campaign_operation.update_mask = field_mask(None, campaign)
# Here we manually add the "manual_cpm" field
campaign_operation.update_mask.append("manual_cpm")
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[[["\u003cp\u003eSome Google Ads API message fields are defined as empty or only have optional fields, requiring specific handling to indicate the intended bidding strategy.\u003c/p\u003e\n"],["\u003cp\u003eTo set a bidding strategy using an empty message field (like \u003ccode\u003emanual_cpm\u003c/code\u003e), you need to copy a separate empty instance of the corresponding class onto the campaign object.\u003c/p\u003e\n"],["\u003cp\u003eNested fields within messages (like \u003ccode\u003ecampaign.manual_cpc.enhanced_cpc_enabled\u003c/code\u003e) can be updated directly like normal Python object attributes.\u003c/p\u003e\n"],["\u003cp\u003eWhen using empty message objects, ensure the field is added to the request's \u003ccode\u003eupdate_mask\u003c/code\u003e manually, as the field mask helper cannot automatically detect this.\u003c/p\u003e\n"]]],[],null,["# Setting Empty Message Objects as Fields\n\nIn the Google Ads API some message fields are defined as empty message objects,\nsuch as [`campaign.manual_cpm`](/google-ads/api/fields/v21/campaign#campaign.manual_cpm),\nor they may only have optional fields that don't need to be set, for example\n[`campaign.manual_cpc`](/google-ads/api/fields/v21/campaign#campaign.manual_cpc.enhanced_cpc_enabled).\nSetting these fields is important to tell the API which bidding strategy to use\nfor the given Campaign, but it's not intuitive when the messages are empty.\n\nWhen updating the `campaign.name` field, which is a string, we set the field\nby updating it directly as if it were a normal Python object attribute: \n\n campaign.name = \"Test campaign value\"\n\n`campaign.manual_cpc` is a nested field, meaning it contains\nanother protobuf message and not a primitive type, like a string. You\ncan update its fields directly as well: \n\n campaign.manual_cpc.enhanced_cpc_enabled = True\n\nThis will tell the API that this Campaign has a bidding strategy of `manual_cpc`\nwith enhanced CPC enabled.\n\nBut what if you want to use `manual_cpm`, which is empty? Or `manual_cpc`\nwithout enabling enhanced cpc? To do this you will need to copy a separate\nempty instance of the class onto the campaign, for example: \n\n client = GoogleAdsClient.load_from_storage()\n\n empty_cpm = client.get_type('ManualCpm')\n client.copy_from(campaign.manual_cpm, empty_cpm)\n\nNote how `manual_cpm` is specified for the `campaign` object: \n\n name {\n value: \"Test campaign value\"\n }\n manual_cpm {\n }\n\nThe `manual_cpm` field is set, but none of its fields have values. When sending\nrequest to the API that use this pattern, you can verify that you're setting the\nempty message object correctly by enabling [logging](/google-ads/api/docs/client-libs/python/logging) and inspecting the\nrequest payload.\n\nLastly, you'll need to manually add this field to the request object's\n`update_mask`. The field mask helper has no mechanism to determine the\ndifference between a field that's been explicitly set to an empty object, and a\nfield that hasn't been set. \n\n from google.api_core.protobuf_helpers import field_mask\n\n campaign_operation.create = campaign\n campaign_operation.update_mask = field_mask(None, campaign)\n # Here we manually add the \"manual_cpm\" field\n campaign_operation.update_mask.append(\"manual_cpm\")"]]