空のメッセージ オブジェクトをフィールドとして設定する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
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
これにより、このキャンペーンでは拡張クリック単価が有効になっている manual_cpc
の入札戦略が使用されていることが API に通知されます。
しかし、空の manual_cpm
を使用したい場合はどうすればよいでしょうか。または、拡張クリック単価を有効にせずに manual_cpc
を使用できますか?これを行うには、クラスの個別の空のインスタンスをキャンペーンにコピーする必要があります。例:
client = GoogleAdsClient.load_from_storage()
empty_cpm = client.get_type('ManualCpm')
client.copy_from(campaign.manual_cpm, empty_cpm)
campaign
オブジェクトに manual_cpm
が指定されていることに注意してください。
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")
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-27 UTC。
[null,null,["最終更新日 2025-08-27 UTC。"],[[["\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\")"]]