2020
2121from google .ads .googleads .client import GoogleAdsClient
2222from google .ads .googleads .errors import GoogleAdsException
23+ from google .ads .googleads .v20 .enums .types .reach_plan_age_range import (
24+ ReachPlanAgeRangeEnum ,
25+ )
26+ from google .ads .googleads .v20 .enums .types .gender_type import GenderTypeEnum
27+ from google .ads .googleads .v20 .enums .types .device import DeviceEnum
28+ from google .ads .googleads .v20 .common .types .criteria import (
29+ GenderInfo ,
30+ DeviceInfo ,
31+ )
32+ from google .ads .googleads .v20 .services .services .reach_plan_service .client import (
33+ ReachPlanServiceClient ,
34+ )
35+ from google .ads .googleads .v20 .services .types .reach_plan_service import (
36+ ListPlannableLocationsResponse ,
37+ PlannableLocation ,
38+ ListPlannableProductsResponse ,
39+ ProductMetadata ,
40+ GenerateReachForecastRequest ,
41+ GenerateReachForecastResponse ,
42+ ReachForecast ,
43+ PlannedProductReachForecast ,
44+ PlannedProduct ,
45+ )
2346
2447ONE_MILLION = 1.0e6
2548
2649
27- def main (client , customer_id ):
50+ def main (client : GoogleAdsClient , customer_id : str ):
2851 """The main method that creates all necessary entities for the example.
2952
3053 Args:
@@ -43,60 +66,77 @@ def main(client, customer_id):
4366 forecast_manual_mix (client , customer_id , location_id , currency_code , budget )
4467
4568
46- def show_plannable_locations (client ):
69+ def show_plannable_locations (client : GoogleAdsClient ):
4770 """Shows map of plannable locations to their IDs.
4871
4972 Args:
5073 client: an initialized GoogleAdsClient instance.
5174 """
52- reach_plan_service = client .get_service ("ReachPlanService" )
53- response = reach_plan_service .list_plannable_locations ()
75+ reach_plan_service : ReachPlanServiceClient = client .get_service (
76+ "ReachPlanService"
77+ )
78+ response : ListPlannableLocationsResponse = (
79+ reach_plan_service .list_plannable_locations ()
80+ )
5481
5582 print ("Plannable Locations" )
5683 print ("Name,\t Id,\t ParentCountryId" )
84+ location : PlannableLocation
5785 for location in response .plannable_locations :
5886 print (
5987 f"'{ location .name } ',\t { location .id } ,\t { location .parent_country_id } "
6088 )
6189
6290
6391# [START forecast_reach_2]
64- def show_plannable_products (client , location_id ):
92+ def show_plannable_products (client : GoogleAdsClient , location_id : str ):
6593 """Lists plannable products for a given location.
6694
6795 Args:
6896 client: an initialized GoogleAdsClient instance.
6997 location_id: The location ID to plan for.
7098 """
71- reach_plan_service = client .get_service ("ReachPlanService" )
72- response = reach_plan_service .list_plannable_products (
73- plannable_location_id = location_id
99+ reach_plan_service : ReachPlanServiceClient = client .get_service (
100+ "ReachPlanService"
101+ )
102+ response : ListPlannableProductsResponse = (
103+ reach_plan_service .list_plannable_products (
104+ plannable_location_id = location_id
105+ )
74106 )
75107 print (f"Plannable Products for Location ID { location_id } " )
76108
109+ product_metadata : ProductMetadata
77110 for product_metadata in response .product_metadata :
78111 print (
79112 f"{ product_metadata .plannable_product_code } : "
80113 f"{ product_metadata .plannable_product_name } "
81114 )
82115
83116 print ("Age Ranges:" )
117+ age_range : ReachPlanAgeRangeEnum
84118 for age_range in product_metadata .plannable_targeting .age_ranges :
85119 print (f"\t - { age_range .name } " )
86120
87121 print ("Genders:" )
122+ gender : GenderInfo
88123 for gender in product_metadata .plannable_targeting .genders :
89124 print (f"\t - { gender .type_ .name } " )
90125
91126 print ("Devices:" )
127+ device : DeviceInfo
92128 for device in product_metadata .plannable_targeting .devices :
93129 print (f"\t - { device .type_ .name } " )
94130 # [END forecast_reach_2]
95131
96132
97133# [START forecast_reach]
98134def request_reach_curve (
99- client , customer_id , product_mix , location_id , currency_code
135+ client : GoogleAdsClient ,
136+ customer_id : str ,
137+ product_mix : list [PlannedProduct ],
138+ location_id : str ,
139+ currency_code : str ,
100140):
101141 """Creates a sample request for a given product mix.
102142
@@ -109,7 +149,9 @@ def request_reach_curve(
109149 """
110150 # See the docs for defaults and valid ranges:
111151 # https://developers.google.com/google-ads/api/reference/rpc/latest/GenerateReachForecastRequest
112- request = client .get_type ("GenerateReachForecastRequest" )
152+ request : GenerateReachForecastRequest = client .get_type (
153+ "GenerateReachForecastRequest"
154+ )
113155 request .customer_id = customer_id
114156 # Valid durations are between 1 and 90 days.
115157 request .campaign_duration .duration_in_days = 28
@@ -124,33 +166,41 @@ def request_reach_curve(
124166 )
125167
126168 # Add gender targeting to the request.
169+ gender_type : GenderTypeEnum
127170 for gender_type in [
128171 client .enums .GenderTypeEnum .FEMALE ,
129172 client .enums .GenderTypeEnum .MALE ,
130173 ]:
131- gender = client .get_type ("GenderInfo" )
174+ gender : GenderInfo = client .get_type ("GenderInfo" )
132175 gender .type_ = gender_type
133176 request .targeting .genders .append (gender )
134177
135178 # Add device targeting to the request.
179+ device_type : DeviceEnum
136180 for device_type in [
137181 client .enums .DeviceEnum .DESKTOP ,
138182 client .enums .DeviceEnum .MOBILE ,
139183 client .enums .DeviceEnum .TABLET ,
140184 ]:
141- device = client .get_type ("DeviceInfo" )
185+ device : DeviceInfo = client .get_type ("DeviceInfo" )
142186 device .type_ = device_type
143187 request .targeting .devices .append (device )
144188
145- reach_plan_service = client .get_service ("ReachPlanService" )
146- response = reach_plan_service .generate_reach_forecast (request = request )
189+ reach_plan_service : ReachPlanServiceClient = client .get_service (
190+ "ReachPlanService"
191+ )
192+ response : GenerateReachForecastResponse = (
193+ reach_plan_service .generate_reach_forecast (request = request )
194+ )
147195
148196 print (
149197 "Currency, Cost, On-Target Reach, On-Target Imprs, Total Reach,"
150198 " Total Imprs, Products"
151199 )
200+ point : ReachForecast
152201 for point in response .reach_curve .reach_forecasts :
153202 product_splits = []
203+ p : PlannedProductReachForecast
154204 for p in point .planned_product_reach_forecasts :
155205 product_splits .append (
156206 {p .plannable_product_code : p .cost_micros / ONE_MILLION }
@@ -171,27 +221,32 @@ def request_reach_curve(
171221
172222# [START forecast_reach_3]
173223def forecast_manual_mix (
174- client , customer_id , location_id , currency_code , budget
224+ client : GoogleAdsClient ,
225+ customer_id : str ,
226+ location_id : str ,
227+ currency_code : str ,
228+ budget : int ,
175229):
176230 """Pulls a forecast for product mix created manually.
177231
178232 Args:
179233 client: an initialized GoogleAdsClient instance.
180234 customer_id: The customer ID for the reach forecast.
181- product_mix: The product mix for the reach forecast.
182235 location_id: The location ID to plan for.
183236 currency_code: Three-character ISO 4217 currency code.
184237 budget: Budget to allocate to the plan.
185238 """
186- product_mix = []
239+ product_mix : list [ PlannedProduct ] = []
187240 trueview_allocation = 0.15
188241 bumper_allocation = 1 - trueview_allocation
189242 product_splits = [
190243 ("TRUEVIEW_IN_STREAM" , trueview_allocation ),
191244 ("BUMPER" , bumper_allocation ),
192245 ]
246+ product : str
247+ split : float
193248 for product , split in product_splits :
194- planned_product = client .get_type ("PlannedProduct" )
249+ planned_product : PlannedProduct = client .get_type ("PlannedProduct" )
195250 planned_product .plannable_product_code = product
196251 planned_product .budget_micros = math .trunc (budget * ONE_MILLION * split )
197252 product_mix .append (planned_product )
0 commit comments