빈 메시지 객체를 필드로 설정
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
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
이렇게 하면 이 캠페인에 향상된 CPC가 사용 설정된 manual_cpc
입찰 전략이 있다고 API에 알릴 수 있습니다.
하지만 비어 있는 manual_cpm
를 사용하려면 어떻게 해야 할까요? 또는 manual_cpc
향상된 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")
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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\")"]]