1414# limitations under the License.
1515"""This example illustrates how to generate audience insights."""
1616
17- import argparse
1817import argparse
1918import sys
19+ from typing import Any
2020
2121from google .ads .googleads .client import GoogleAdsClient
2222from google .ads .googleads .errors import GoogleAdsException
23+ from google .ads .googleads .v20 .enums .types import (
24+ AudienceInsightsDimensionEnum ,
25+ )
26+ from google .ads .googleads .v20 .services .services .audience_insights_service import (
27+ AudienceInsightsServiceClient ,
28+ )
29+ from google .ads .googleads .v20 .services .services .google_ads_service import (
30+ GoogleAdsServiceClient ,
31+ )
32+ from google .ads .googleads .v20 .services .types .audience_insights_service import (
33+ GenerateAudienceCompositionInsightsRequest ,
34+ GenerateAudienceCompositionInsightsResponse ,
35+ GenerateSuggestedTargetingInsightsRequest ,
36+ GenerateSuggestedTargetingInsightsResponse ,
37+ InsightsAudienceAttributeGroup ,
38+ ListAudienceInsightsAttributesRequest ,
39+ ListAudienceInsightsAttributesResponse ,
40+ )
41+ from google .ads .googleads .v20 .common .types import (
42+ AudienceInsightsAttribute ,
43+ LocationInfo
44+ )
2345
24-
25- def main (client , customer_id , custom_name ):
46+ def main (
47+ client : GoogleAdsClient , customer_id : str , custom_name : str
48+ ) -> None :
2649 """The main method that creates all necessary entities for the example.
2750
2851 Args:
2952 client: an initialized GoogleAdsClient instance.
3053 customer_id: a client customer ID.
3154 custom_name: custom name to define audience.
3255 """
33- location_id = "2840" # US
34- product_name = "Google"
35- user_interest_category = "92948" # Technology
56+ location_id : str = "2840" # US
57+ product_name : str = "Google"
58+ user_interest_category : str = "92948" # Technology
3659 # Initialize appropriate services.
37- audience_insights_service = client .get_service ("AudienceInsightsService" )
38- googleads_service = client .get_service ("GoogleAdsService" )
60+ audience_insights_service : AudienceInsightsServiceClient = client .get_service (
61+ "AudienceInsightsService"
62+ )
63+ googleads_service : GoogleAdsServiceClient = client .get_service (
64+ "GoogleAdsService"
65+ )
3966
4067 audience_composition_insights (
4168 client ,
@@ -65,17 +92,17 @@ def main(client, customer_id, custom_name):
6592
6693# [START composition_insights]
6794def audience_composition_insights (
68- client ,
69- audience_insights_service ,
70- googleads_service ,
71- customer_id ,
72- location_id ,
73- user_interest ,
74- custom_name ,
75- ):
95+ client : GoogleAdsClient ,
96+ audience_insights_service : AudienceInsightsServiceClient ,
97+ googleads_service : GoogleAdsServiceClient ,
98+ customer_id : str ,
99+ location_id : str ,
100+ user_interest : str ,
101+ custom_name : str ,
102+ ) -> None :
76103 """Returns a collection of attributes represented in an audience of interest.
77104
78- Please refere here for more:
105+ Please refer here for more:
79106 https://developers.google.com/google-ads/api/data/codes-formats
80107
81108 Args:
@@ -88,47 +115,55 @@ def audience_composition_insights(
88115 user_interest: The criterion ID of the category.
89116 custom_name: custom defined name.
90117 """
91- request = client .get_type ("GenerateAudienceCompositionInsightsRequest" )
118+ request : GenerateAudienceCompositionInsightsRequest = client .get_type (
119+ "GenerateAudienceCompositionInsightsRequest"
120+ )
92121 request .customer_id = customer_id
93122
94- insights_info = client .get_type ("InsightsAudienceAttributeGroup" )
95- attributes = client .get_type ("AudienceInsightsAttribute" )
123+ insights_info : InsightsAudienceAttributeGroup = client .get_type (
124+ "InsightsAudienceAttributeGroup"
125+ )
126+ attributes : AudienceInsightsAttribute = client .get_type (
127+ "AudienceInsightsAttribute"
128+ )
96129 attributes .user_interest .user_interest_category = (
97130 googleads_service .user_interest_path (customer_id , user_interest )
98131 )
99132
100133 insights_info .attributes .append (attributes )
101134 request .audience .topic_audience_combinations .append (insights_info )
102135
103- location_info = client .get_type ("LocationInfo" )
136+ location_info : LocationInfo = client .get_type ("LocationInfo" )
104137 location_info .geo_target_constant = (
105138 googleads_service .geo_target_constant_path (location_id )
106139 )
107140 request .audience .country_locations .append (location_info )
108141
109142 request .customer_insights_group = custom_name
110- request .dimensions = (
111- "AFFINITY_USER_INTEREST" ,
112- "IN_MARKET_USER_INTEREST" ,
113- "YOUTUBE_CHANNEL" ,
114- )
115- response = audience_insights_service .generate_audience_composition_insights (
116- request = request
143+ request .dimensions = [
144+ client .enums .AudienceInsightsDimensionEnum .AFFINITY_USER_INTEREST ,
145+ client .enums .AudienceInsightsDimensionEnum .IN_MARKET_USER_INTEREST ,
146+ client .enums .AudienceInsightsDimensionEnum .YOUTUBE_CHANNEL ,
147+ ]
148+ response : GenerateAudienceCompositionInsightsResponse = (
149+ audience_insights_service .generate_audience_composition_insights (
150+ request = request
151+ )
117152 )
118153 print (response )
119154 # [END composition_insights]
120155
121156
122157# [START targeted_insights]
123158def generate_suggested_targeting_insights (
124- client ,
125- audience_insights_service ,
126- googleads_service ,
127- customer_id ,
128- location_id ,
129- custom_name ,
130- ):
131- """Returns a collection of targeting insights (e.g.targetable audiences)
159+ client : GoogleAdsClient ,
160+ audience_insights_service : AudienceInsightsServiceClient ,
161+ googleads_service : GoogleAdsServiceClient ,
162+ customer_id : str ,
163+ location_id : str ,
164+ custom_name : str ,
165+ ) -> None :
166+ """Returns a collection of targeting insights (e.g. target-able audiences)
132167 that are relevant to the requested audience.
133168
134169 Args:
@@ -140,30 +175,38 @@ def generate_suggested_targeting_insights(
140175 location_id: The location ID for the audience of interest.
141176 custom_name: custom defined name.
142177 """
143- request = client .get_type ("GenerateSuggestedTargetingInsightsRequest" )
178+ request : GenerateSuggestedTargetingInsightsRequest = client .get_type (
179+ "GenerateSuggestedTargetingInsightsRequest"
180+ )
144181
145182 request .customer_id = customer_id
146183 request .customer_insights_group = custom_name
147184
148- audience_definition = request .audience_definition
149- location_info = client .get_type ("LocationInfo" )
185+ audience_definition : Any = request .audience_definition
186+ location_info : LocationInfo = client .get_type ("LocationInfo" )
150187 location_info .geo_target_constant = (
151188 googleads_service .geo_target_constant_path (location_id )
152189 )
153190 audience_definition .audience .country_locations .append (location_info )
154191
155192 request .audience_definition = audience_definition
156- response = audience_insights_service .generate_suggested_targeting_insights (
157- request = request
193+ response : GenerateSuggestedTargetingInsightsResponse = (
194+ audience_insights_service .generate_suggested_targeting_insights (
195+ request = request
196+ )
158197 )
159198 print (response )
160199 # [END targeted_insights]
161200
162201
163202# [START insights_attributes]
164203def list_audience_insights_attributes (
165- client , audience_insights_service , customer_id , product_name , custom_name
166- ):
204+ client : GoogleAdsClient ,
205+ audience_insights_service : AudienceInsightsServiceClient ,
206+ customer_id : str ,
207+ product_name : str ,
208+ custom_name : str ,
209+ ) -> None :
167210 """Searches for audience attributes that can be used to generate insights.
168211
169212 Args:
@@ -174,25 +217,38 @@ def list_audience_insights_attributes(
174217 product_name: The brand/product for which insights are expected.
175218 custom_name: custom defined name.
176219 """
177- request = client .get_type ("ListAudienceInsightsAttributesRequest" )
220+ request : ListAudienceInsightsAttributesRequest = client .get_type (
221+ "ListAudienceInsightsAttributesRequest"
222+ )
178223
179224 request .customer_id = customer_id
180225 request .query_text = product_name
181- category_dimension = client .enums .AudienceInsightsDimensionEnum .CATEGORY
182- kg_dimension = client .enums .AudienceInsightsDimensionEnum .KNOWLEDGE_GRAPH
226+ category_dimension : AudienceInsightsDimensionEnum .AudienceInsightsDimension = (
227+ client .enums .AudienceInsightsDimensionEnum .CATEGORY
228+ )
229+ kg_dimension : AudienceInsightsDimensionEnum .AudienceInsightsDimension = (
230+ client .enums .AudienceInsightsDimensionEnum .KNOWLEDGE_GRAPH
231+ )
183232 request .dimensions = [category_dimension , kg_dimension ]
184233 request .customer_insights_group = custom_name
185- response = audience_insights_service .list_audience_insights_attributes (
186- request = request
234+ response : ListAudienceInsightsAttributesResponse = (
235+ audience_insights_service .list_audience_insights_attributes (
236+ request = request
237+ )
187238 )
188239 for attribute in response .attributes :
189- if attribute .dimension == 3 :
240+ if (
241+ attribute .dimension
242+ == client .enums .AudienceInsightsDimensionEnum .KNOWLEDGE_GRAPH
243+ ):
190244 print (attribute .attribute .entity .knowledge_graph_machine_id )
191245 # [END insights_attributes]
192246
193247
194248if __name__ == "__main__" :
195- parser = argparse .ArgumentParser (description = "Create audience insights." )
249+ parser : argparse .ArgumentParser = argparse .ArgumentParser (
250+ description = "Create audience insights."
251+ )
196252
197253 # The following argument(s) should be provided to run the example.
198254 parser .add_argument (
@@ -208,27 +264,29 @@ def list_audience_insights_attributes(
208264 "--custom_name" ,
209265 type = str ,
210266 required = True ,
211- help = "Custom name to indentify audiences" ,
267+ help = "Custom name to identify audiences" ,
212268 )
213269 parser .add_argument
214- args = parser .parse_args ()
270+ args : argparse . Namespace = parser .parse_args ()
215271
216272 # GoogleAdsClient will read the google-ads.yaml configuration file in the
217273 # home directory if none is specified.
218- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
274+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
275+ version = "v20"
276+ )
219277
220278 try :
221279 main (googleads_client , args .customer_id , args .custom_name )
222280 except GoogleAdsException as ex :
223281 print (
224- 'Request with ID "{}" failed with status "%s" and includes the '
225- "following errors:" . format ( ex .request_id , ex . error .code ().name )
282+ f 'Request with ID "{ ex . request_id } " failed with status '
283+ f'" { ex .error .code ().name } " and includes the following errors:'
226284 )
227285 for error in ex .failure .errors :
228- print ('\t Error with message "{}".' . format ( error . message ) )
286+ print (f '\t Error with message "{ error . message } ".' )
229287 if error .location :
230288 for field_path_element in error .location .field_path_elements :
231289 print (
232- "\t \t On field: {}" . format ( field_path_element .field_name )
290+ f "\t \t On field: { field_path_element .field_name } "
233291 )
234292 sys .exit (1 )
0 commit comments