Skip to content

Commit 328351a

Browse files
committed
Update annotations in advanced_operations examples.
Change-Id: Iaa2528d2f11a9ad01f07807daba6102fd7694828
1 parent 26f705c commit 328351a

17 files changed

+1710
-677
lines changed

examples/advanced_operations/add_ad_customizer.py

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,36 @@
2121

2222
import argparse
2323
import sys
24-
from typing import Any
2524
from uuid import uuid4
2625

2726
from google.ads.googleads.client import GoogleAdsClient
2827
from google.ads.googleads.errors import GoogleAdsException
28+
from google.ads.googleads.v21.common.types import AdTextAsset
29+
from google.ads.googleads.v21.resources.types import (
30+
AdGroupAd,
31+
AdGroupCustomizer,
32+
CustomizerAttribute,
33+
)
34+
from google.ads.googleads.v21.services.services.ad_group_ad_service import (
35+
AdGroupAdServiceClient,
36+
)
37+
from google.ads.googleads.v21.services.services.ad_group_customizer_service import (
38+
AdGroupCustomizerServiceClient,
39+
)
40+
from google.ads.googleads.v21.services.services.customizer_attribute_service import (
41+
CustomizerAttributeServiceClient,
42+
)
43+
from google.ads.googleads.v21.services.services.google_ads_service import (
44+
GoogleAdsServiceClient,
45+
)
46+
from google.ads.googleads.v21.services.types import (
47+
AdGroupAdOperation,
48+
AdGroupCustomizerOperation,
49+
CustomizerAttributeOperation,
50+
MutateAdGroupAdsResponse,
51+
MutateAdGroupCustomizersResponse,
52+
MutateCustomizerAttributesResponse,
53+
)
2954

3055

3156
def main(client: GoogleAdsClient, customer_id: str, ad_group_id: str) -> None:
@@ -75,19 +100,23 @@ def create_text_customizer_attribute(
75100
Returns:
76101
a resource name for a text customizer attribute.
77102
"""
78-
customizer_attribute_service: Any = client.get_service(
79-
"CustomizerAttributeService"
103+
customizer_attribute_service: CustomizerAttributeServiceClient = (
104+
client.get_service("CustomizerAttributeService")
80105
)
81106

82107
# Creates a text customizer attribute. The customizer attribute name is
83108
# arbitrary and will be used as a placeholder in the ad text fields.
84-
operation: Any = client.get_type("CustomizerAttributeOperation")
85-
text_attribute: Any = operation.create
109+
operation: CustomizerAttributeOperation = client.get_type(
110+
"CustomizerAttributeOperation"
111+
)
112+
text_attribute: CustomizerAttribute = operation.create
86113
text_attribute.name = customizer_name
87114
text_attribute.type_ = client.enums.CustomizerAttributeTypeEnum.TEXT
88115

89-
response: Any = customizer_attribute_service.mutate_customizer_attributes(
90-
customer_id=customer_id, operations=[operation]
116+
response: MutateCustomizerAttributesResponse = (
117+
customizer_attribute_service.mutate_customizer_attributes(
118+
customer_id=customer_id, operations=[operation]
119+
)
91120
)
92121

93122
resource_name: str = response.results[0].resource_name
@@ -112,19 +141,23 @@ def create_price_customizer_attribute(
112141
Returns:
113142
a resource name for a text customizer attribute.
114143
"""
115-
customizer_attribute_service: Any = client.get_service(
116-
"CustomizerAttributeService"
144+
customizer_attribute_service: CustomizerAttributeServiceClient = (
145+
client.get_service("CustomizerAttributeService")
117146
)
118147

119148
# Creates a price customizer attribute. The customizer attribute name is
120149
# arbitrary and will be used as a placeholder in the ad text fields.
121-
operation: Any = client.get_type("CustomizerAttributeOperation")
122-
price_attribute: Any = operation.create
150+
operation: CustomizerAttributeOperation = client.get_type(
151+
"CustomizerAttributeOperation"
152+
)
153+
price_attribute: CustomizerAttribute = operation.create
123154
price_attribute.name = customizer_name
124155
price_attribute.type_ = client.enums.CustomizerAttributeTypeEnum.PRICE
125156

126-
response: Any = customizer_attribute_service.mutate_customizer_attributes(
127-
customer_id=customer_id, operations=[operation]
157+
response: MutateCustomizerAttributesResponse = (
158+
customizer_attribute_service.mutate_customizer_attributes(
159+
customer_id=customer_id, operations=[operation]
160+
)
128161
)
129162

130163
resource_name: str = response.results[0].resource_name
@@ -155,15 +188,19 @@ def link_customizer_attributes(
155188
text_customizer_resource_name: the resource name of the text customizer attribute.
156189
price_customizer_resource_name: the resource name of the price customizer attribute.
157190
"""
158-
googleads_service: Any = client.get_service("GoogleAdsService")
159-
ad_group_customizer_service: Any = client.get_service(
160-
"AdGroupCustomizerService"
191+
googleads_service: GoogleAdsServiceClient = client.get_service(
192+
"GoogleAdsService"
193+
)
194+
ad_group_customizer_service: AdGroupCustomizerServiceClient = (
195+
client.get_service("AdGroupCustomizerService")
161196
)
162197

163198
# Binds the text attribute customizer to a specific ad group to make sure
164199
# it will only be used to customize ads inside that ad group.
165-
mars_operation: Any = client.get_type("AdGroupCustomizerOperation")
166-
mars_customizer: Any = mars_operation.create
200+
mars_operation: AdGroupCustomizerOperation = client.get_type(
201+
"AdGroupCustomizerOperation"
202+
)
203+
mars_customizer: AdGroupCustomizer = mars_operation.create
167204
mars_customizer.customizer_attribute = text_customizer_resource_name
168205
mars_customizer.value.type_ = client.enums.CustomizerAttributeTypeEnum.TEXT
169206
mars_customizer.value.string_value = "Mars"
@@ -173,8 +210,10 @@ def link_customizer_attributes(
173210

174211
# Binds the price attribute customizer to a specific ad group to make sure
175212
# it will only be used to customize ads inside that ad group.
176-
price_operation: Any = client.get_type("AdGroupCustomizerOperation")
177-
price_customizer: Any = price_operation.create
213+
price_operation: AdGroupCustomizerOperation = client.get_type(
214+
"AdGroupCustomizerOperation"
215+
)
216+
price_customizer: AdGroupCustomizer = price_operation.create
178217
price_customizer.customizer_attribute = price_customizer_resource_name
179218
price_customizer.value.type_ = (
180219
client.enums.CustomizerAttributeTypeEnum.PRICE
@@ -184,8 +223,11 @@ def link_customizer_attributes(
184223
customer_id, ad_group_id
185224
)
186225

187-
response: Any = ad_group_customizer_service.mutate_ad_group_customizers(
188-
customer_id=customer_id, operations=[mars_operation, price_operation]
226+
response: MutateAdGroupCustomizersResponse = (
227+
ad_group_customizer_service.mutate_ad_group_customizers(
228+
customer_id=customer_id,
229+
operations=[mars_operation, price_operation],
230+
)
189231
)
190232

191233
for result in response.results:
@@ -215,41 +257,45 @@ def create_ad_with_customizations(
215257
text_customizer_name: name of the text customizer.
216258
price_customizer_name: name of the price customizer.
217259
"""
218-
googleads_service: Any = client.get_service("GoogleAdsService")
219-
ad_group_ad_service: Any = client.get_service("AdGroupAdService")
260+
googleads_service: GoogleAdsServiceClient = client.get_service(
261+
"GoogleAdsService"
262+
)
263+
ad_group_ad_service: AdGroupAdServiceClient = client.get_service(
264+
"AdGroupAdService"
265+
)
220266

221267
# Creates a responsive search ad using the attribute customizer names as
222268
# placeholders and default values to be used in case there are no attribute
223269
# customizer values.
224-
operation: Any = client.get_type("AdGroupAdOperation")
225-
ad_group_ad: Any = operation.create
270+
operation: AdGroupAdOperation = client.get_type("AdGroupAdOperation")
271+
ad_group_ad: AdGroupAd = operation.create
226272
ad_group_ad.ad.final_urls.append("https://www.example.com")
227273
ad_group_ad.ad_group = googleads_service.ad_group_path(
228274
customer_id, ad_group_id
229275
)
230276

231-
headline_1: Any = client.get_type("AdTextAsset")
277+
headline_1: AdTextAsset = client.get_type("AdTextAsset")
232278
headline_1.text = (
233279
f"Luxury cruise to {{CUSTOMIZER.{text_customizer_name}:Venus}}"
234280
)
235281
headline_1.pinned_field = client.enums.ServedAssetFieldTypeEnum.HEADLINE_1
236282

237-
headline_2: Any = client.get_type("AdTextAsset")
283+
headline_2: AdTextAsset = client.get_type("AdTextAsset")
238284
headline_2.text = f"Only {{CUSTOMIZER.{price_customizer_name}:10.0€}}"
239285

240-
headline_3: Any = client.get_type("AdTextAsset")
286+
headline_3: AdTextAsset = client.get_type("AdTextAsset")
241287
headline_3.text = f"Cruise to {{CUSTOMIZER.{text_customizer_name}:Venus}} for {{CUSTOMIZER.{price_customizer_name}:10.0€}}"
242288

243289
ad_group_ad.ad.responsive_search_ad.headlines.extend(
244290
[headline_1, headline_2, headline_3]
245291
)
246292

247-
description_1: Any = client.get_type("AdTextAsset")
293+
description_1: AdTextAsset = client.get_type("AdTextAsset")
248294
description_1.text = (
249295
f"Tickets are only {{CUSTOMIZER.{price_customizer_name}:10.0€}}!"
250296
)
251297

252-
description_2: Any = client.get_type("AdTextAsset")
298+
description_2: AdTextAsset = client.get_type("AdTextAsset")
253299
description_2.text = (
254300
f"Buy your tickets to {{CUSTOMIZER.{text_customizer_name}:Venus}} now!"
255301
)
@@ -258,8 +304,10 @@ def create_ad_with_customizations(
258304
[description_1, description_2]
259305
)
260306

261-
response: Any = ad_group_ad_service.mutate_ad_group_ads(
262-
customer_id=customer_id, operations=[operation]
307+
response: MutateAdGroupAdsResponse = (
308+
ad_group_ad_service.mutate_ad_group_ads(
309+
customer_id=customer_id, operations=[operation]
310+
)
263311
)
264312
resource_name: str = response.results[0].resource_name
265313
print(f"Added an ad with resource name '{resource_name}'")
@@ -293,17 +341,9 @@ def create_ad_with_customizations(
293341

294342
# GoogleAdsClient will read the google-ads.yaml configuration file in the
295343
# home directory if none is specified.
296-
<<<<<<< HEAD
297-
googleads_client = GoogleAdsClient.load_from_storage(version="v21")
298-
=======
299-
<<<<<<< HEAD
300-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
301-
=======
302344
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
303-
version="v19"
345+
version="v21"
304346
)
305-
>>>>>>> 83bf66141 (Add type hints to files in examples/advanced_operations)
306-
>>>>>>> f8ca36136 (Add type hints to files in examples/advanced_operations)
307347

308348
try:
309349
main(googleads_client, args.customer_id, args.ad_group_id)

examples/advanced_operations/add_ad_group_bid_modifier.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@
2020

2121
import argparse
2222
import sys
23-
from typing import Any
2423

2524
from google.ads.googleads.client import GoogleAdsClient
2625
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v21.enums.types.device import DeviceEnum
27+
from google.ads.googleads.v21.resources.types.ad_group_bid_modifier import (
28+
AdGroupBidModifier,
29+
)
30+
from google.ads.googleads.v21.services.services.ad_group_bid_modifier_service import (
31+
AdGroupBidModifierServiceClient,
32+
)
33+
from google.ads.googleads.v21.services.services.ad_group_service import (
34+
AdGroupServiceClient,
35+
)
36+
from google.ads.googleads.v21.services.types.ad_group_bid_modifier_service import (
37+
AdGroupBidModifierOperation,
38+
MutateAdGroupBidModifiersResponse,
39+
)
2740

2841

2942
# [START add_ad_group_bid_modifier]
@@ -33,15 +46,21 @@ def main(
3346
ad_group_id: str,
3447
bid_modifier_value: float,
3548
) -> None:
36-
ad_group_service: Any = client.get_service("AdGroupService")
37-
ad_group_bm_service: Any = client.get_service("AdGroupBidModifierService")
49+
ad_group_service: AdGroupServiceClient = client.get_service(
50+
"AdGroupService"
51+
)
52+
ad_group_bm_service: AdGroupBidModifierServiceClient = client.get_service(
53+
"AdGroupBidModifierService"
54+
)
3855

3956
# Create ad group bid modifier for mobile devices with the specified ad
4057
# group ID and bid modifier value.
41-
ad_group_bid_modifier_operation: Any = client.get_type(
42-
"AdGroupBidModifierOperation"
58+
ad_group_bid_modifier_operation: AdGroupBidModifierOperation = (
59+
client.get_type("AdGroupBidModifierOperation")
60+
)
61+
ad_group_bid_modifier: AdGroupBidModifier = (
62+
ad_group_bid_modifier_operation.create
4363
)
44-
ad_group_bid_modifier: Any = ad_group_bid_modifier_operation.create
4564

4665
# Set the ad group.
4766
ad_group_bid_modifier.ad_group = ad_group_service.ad_group_path(
@@ -52,11 +71,11 @@ def main(
5271
ad_group_bid_modifier.bid_modifier = bid_modifier_value
5372

5473
# Sets the device.
55-
device_enum: Any = client.enums.DeviceEnum
74+
device_enum: DeviceEnum = client.enums.DeviceEnum
5675
ad_group_bid_modifier.device.type_ = device_enum.MOBILE
5776

5877
# Add the ad group bid modifier.
59-
ad_group_bm_response: Any = (
78+
ad_group_bm_response: MutateAdGroupBidModifiersResponse = (
6079
ad_group_bm_service.mutate_ad_group_bid_modifiers(
6180
customer_id=customer_id,
6281
operations=[ad_group_bid_modifier_operation],

0 commit comments

Comments
 (0)