Skip to content

Commit 0c84c9a

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
Fix: Correct enum import paths in examples/planning files
This change corrects the import paths for enums in the Python files under the `examples/planning/` directory. The previous import paths were incorrect, and this commit fixes them to use the `google.ads.googleads.v20.enums.types` path. The following files were modified: - `examples/planning/forecast_reach.py` - `examples/planning/generate_forecast_metrics.py` - `examples/planning/generate_historical_metrics.py` - `examples/planning/generate_keyword_ideas.py`
1 parent 19e2c22 commit 0c84c9a

5 files changed

Lines changed: 238 additions & 60 deletions

File tree

examples/planning/forecast_reach.py

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,34 @@
2020

2121
from google.ads.googleads.client import GoogleAdsClient
2222
from 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

2447
ONE_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,\tId,\tParentCountryId")
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]
98134
def 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]
173223
def 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)

examples/planning/generate_forecast_metrics.py

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,44 @@
2424

2525
from google.ads.googleads.client import GoogleAdsClient
2626
from google.ads.googleads.errors import GoogleAdsException
27+
from google.ads.googleads.v20.enums.types.keyword_plan_network import (
28+
KeywordPlanNetworkEnum,
29+
)
30+
from google.ads.googleads.v20.enums.types.keyword_match_type import (
31+
KeywordMatchTypeEnum,
32+
)
33+
from google.ads.googleads.v20.services.services.google_ads_service.client import (
34+
GoogleAdsServiceClient,
35+
)
36+
from google.ads.googleads.v20.services.services.keyword_plan_idea_service.client import (
37+
KeywordPlanIdeaServiceClient,
38+
)
39+
from google.ads.googleads.v20.services.types.keyword_plan_idea_service import (
40+
CampaignToForecast,
41+
CriterionBidModifier,
42+
ForecastAdGroup,
43+
BiddableKeyword,
44+
KeywordInfo,
45+
GenerateKeywordForecastMetricsRequest,
46+
GenerateKeywordForecastMetricsResponse,
47+
)
2748

2849

2950
# [START generate_forecast_metrics]
30-
def main(client, customer_id):
51+
def main(client: GoogleAdsClient, customer_id: str):
3152
"""The main method that creates all necessary entities for the example.
3253
3354
Args:
3455
client: an initialized GoogleAdsClient instance.
3556
customer_id: a client customer ID.
3657
"""
37-
campaign_to_forecast = create_campaign_to_forecast(client)
58+
campaign_to_forecast: CampaignToForecast = create_campaign_to_forecast(
59+
client
60+
)
3861
generate_forecast_metrics(client, customer_id, campaign_to_forecast)
3962

4063

41-
def create_campaign_to_forecast(client):
64+
def create_campaign_to_forecast(client: GoogleAdsClient) -> CampaignToForecast:
4265
"""Creates the campaign to forecast.
4366
4467
A campaign to forecast lets you try out various configurations and keywords
@@ -53,9 +76,13 @@ def create_campaign_to_forecast(client):
5376
Returns:
5477
An CampaignToForecast instance.
5578
"""
56-
googleads_service = client.get_service("GoogleAdsService")
79+
googleads_service: GoogleAdsServiceClient = client.get_service(
80+
"GoogleAdsService"
81+
)
5782
# Create a campaign to forecast.
58-
campaign_to_forecast = client.get_type("CampaignToForecast")
83+
campaign_to_forecast: CampaignToForecast = client.get_type(
84+
"CampaignToForecast"
85+
)
5986
campaign_to_forecast.keyword_plan_network = (
6087
client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH
6188
)
@@ -67,7 +94,9 @@ def create_campaign_to_forecast(client):
6794

6895
# For the list of geo target IDs, see:
6996
# https://developers.google.com/google-ads/api/reference/data/geotargets
70-
criterion_bid_modifier = client.get_type("CriterionBidModifier")
97+
criterion_bid_modifier: CriterionBidModifier = client.get_type(
98+
"CriterionBidModifier"
99+
)
71100
# Geo target constant 2840 is for USA.
72101
criterion_bid_modifier.geo_target_constant = (
73102
googleads_service.geo_target_constant_path("2840")
@@ -83,24 +112,24 @@ def create_campaign_to_forecast(client):
83112

84113
# Create forecast ad groups based on themes such as creative relevance,
85114
# product category, or cost per click.
86-
forecast_ad_group = client.get_type("ForecastAdGroup")
115+
forecast_ad_group: ForecastAdGroup = client.get_type("ForecastAdGroup")
87116

88117
# Create and configure three BiddableKeyword instances.
89-
biddable_keyword_1 = client.get_type("BiddableKeyword")
118+
biddable_keyword_1: BiddableKeyword = client.get_type("BiddableKeyword")
90119
biddable_keyword_1.max_cpc_bid_micros = 2500000
91120
biddable_keyword_1.keyword.text = "mars cruise"
92121
biddable_keyword_1.keyword.match_type = (
93122
client.enums.KeywordMatchTypeEnum.BROAD
94123
)
95124

96-
biddable_keyword_2 = client.get_type("BiddableKeyword")
125+
biddable_keyword_2: BiddableKeyword = client.get_type("BiddableKeyword")
97126
biddable_keyword_2.max_cpc_bid_micros = 1500000
98127
biddable_keyword_2.keyword.text = "cheap cruise"
99128
biddable_keyword_2.keyword.match_type = (
100129
client.enums.KeywordMatchTypeEnum.PHRASE
101130
)
102131

103-
biddable_keyword_3 = client.get_type("BiddableKeyword")
132+
biddable_keyword_3: BiddableKeyword = client.get_type("BiddableKeyword")
104133
biddable_keyword_3.max_cpc_bid_micros = 1990000
105134
biddable_keyword_3.keyword.text = "cheap cruise"
106135
biddable_keyword_3.keyword.match_type = (
@@ -114,7 +143,7 @@ def create_campaign_to_forecast(client):
114143

115144
# Create and configure a negative keyword, then add it to the forecast ad
116145
# group.
117-
negative_keyword = client.get_type("KeywordInfo")
146+
negative_keyword: KeywordInfo = client.get_type("KeywordInfo")
118147
negative_keyword.text = "moon walk"
119148
negative_keyword.match_type = client.enums.KeywordMatchTypeEnum.BROAD
120149
forecast_ad_group.negative_keywords.append(negative_keyword)
@@ -124,16 +153,24 @@ def create_campaign_to_forecast(client):
124153
return campaign_to_forecast
125154

126155

127-
def generate_forecast_metrics(client, customer_id, campaign_to_forecast):
156+
def generate_forecast_metrics(
157+
client: GoogleAdsClient,
158+
customer_id: str,
159+
campaign_to_forecast: CampaignToForecast,
160+
):
128161
"""Generates forecast metrics and prints the results.
129162
130163
Args:
131164
client: an initialized GoogleAdsClient instance.
132165
customer_id: a client customer ID.
133166
campaign_to_forecast: a CampaignToForecast to generate metrics for.
134167
"""
135-
keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService")
136-
request = client.get_type("GenerateKeywordForecastMetricsRequest")
168+
keyword_plan_idea_service: KeywordPlanIdeaServiceClient = client.get_service(
169+
"KeywordPlanIdeaService"
170+
)
171+
request: GenerateKeywordForecastMetricsRequest = client.get_type(
172+
"GenerateKeywordForecastMetricsRequest"
173+
)
137174
request.customer_id = customer_id
138175
request.campaign = campaign_to_forecast
139176
# Set the forecast range. Repeat forecasts with different horizons to get a
@@ -145,8 +182,10 @@ def generate_forecast_metrics(client, customer_id, campaign_to_forecast):
145182
thirty_days_from_now = datetime.now() + timedelta(days=30)
146183
request.forecast_period.end_date = thirty_days_from_now.strftime("%Y-%m-%d")
147184

148-
response = keyword_plan_idea_service.generate_keyword_forecast_metrics(
149-
request=request
185+
response: GenerateKeywordForecastMetricsResponse = (
186+
keyword_plan_idea_service.generate_keyword_forecast_metrics(
187+
request=request
188+
)
150189
)
151190

152191
metrics = response.campaign_forecast_metrics

0 commit comments

Comments
 (0)