Skip to content

Commit b40d97a

Browse files
committed
Update remarketing examples with annotations
Change-Id: I937683a77ae322f36e6a0f32f4acefa64dff8a78
1 parent 5ca57ce commit b40d97a

15 files changed

+856
-397
lines changed

examples/remarketing/add_conversion_action.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@
1818
import argparse
1919
import sys
2020
import uuid
21-
from typing import Any
2221

2322
from google.ads.googleads.client import GoogleAdsClient
2423
from google.ads.googleads.errors import GoogleAdsException
25-
from google.ads.googleads.v20.services.types.conversion_action_service import (
26-
ConversionActionOperation,
27-
ConversionActionServiceClient,
28-
)
2924
from google.ads.googleads.v20.resources.types.conversion_action import (
3025
ConversionAction,
3126
)
27+
from google.ads.googleads.v20.services.services.conversion_action_service import (
28+
ConversionActionServiceClient,
29+
)
30+
from google.ads.googleads.v20.services.types.conversion_action_service import (
31+
ConversionActionOperation,
32+
MutateConversionActionsResponse,
33+
)
3234

3335

3436
# [START add_conversion_action]
3537
def main(client: GoogleAdsClient, customer_id: str) -> None:
36-
conversion_action_service: ConversionActionServiceClient = client.get_service(
37-
"ConversionActionService"
38+
conversion_action_service: ConversionActionServiceClient = (
39+
client.get_service("ConversionActionService")
3840
)
3941

4042
# Create the operation.
@@ -49,18 +51,24 @@ def main(client: GoogleAdsClient, customer_id: str) -> None:
4951
# already exists with the specified conversion_action_name, the create
5052
# operation will fail with a ConversionActionError.DUPLICATE_NAME error.
5153
conversion_action.name = f"Earth to Mars Cruises Conversion {uuid.uuid4()}"
52-
conversion_action.type_ = client.enums.ConversionActionTypeEnum.UPLOAD_CLICKS
53-
conversion_action.category = client.enums.ConversionActionCategoryEnum.DEFAULT
54+
conversion_action.type_ = (
55+
client.enums.ConversionActionTypeEnum.UPLOAD_CLICKS
56+
)
57+
conversion_action.category = (
58+
client.enums.ConversionActionCategoryEnum.DEFAULT
59+
)
5460
conversion_action.status = client.enums.ConversionActionStatusEnum.ENABLED
5561
conversion_action.view_through_lookback_window_days = 15
5662

5763
# Create a value settings object.
58-
value_settings: Any = conversion_action.value_settings
64+
value_settings: ConversionAction.ValueSettings = (
65+
conversion_action.value_settings
66+
)
5967
value_settings.default_value = 15.0
6068
value_settings.always_use_default_value = True
6169

6270
# Add the conversion action.
63-
conversion_action_response: Any = (
71+
conversion_action_response: MutateConversionActionsResponse = (
6472
conversion_action_service.mutate_conversion_actions(
6573
customer_id=customer_id,
6674
operations=[conversion_action_operation],

examples/remarketing/add_conversion_based_user_list.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@
2222
import argparse
2323
import sys
2424
from uuid import uuid4
25-
from typing import List, Any
25+
from typing import List
2626

2727
from google.ads.googleads.client import GoogleAdsClient
2828
from google.ads.googleads.errors import GoogleAdsException
2929
from google.ads.googleads.v20.common.types.user_lists import UserListActionInfo
3030
from google.ads.googleads.v20.resources.types.user_list import UserList
31-
from google.ads.googleads.v20.services.types.conversion_action_service import (
31+
from google.ads.googleads.v20.services.services.conversion_action_service import (
3232
ConversionActionServiceClient,
3333
)
34+
from google.ads.googleads.v20.services.services.user_list_service import (
35+
UserListServiceClient,
36+
)
3437
from google.ads.googleads.v20.services.types.user_list_service import (
3538
UserListOperation,
36-
UserListServiceClient,
39+
MutateUserListsResponse,
3740
)
3841

3942

@@ -55,8 +58,8 @@ def main(
5558
user_list_service: UserListServiceClient = client.get_service(
5659
"UserListService"
5760
)
58-
conversion_action_service: ConversionActionServiceClient = client.get_service(
59-
"ConversionActionService"
61+
conversion_action_service: ConversionActionServiceClient = (
62+
client.get_service("ConversionActionService")
6063
)
6164

6265
# Create a list of UserListActionInfo objects for the given conversion
@@ -75,7 +78,9 @@ def main(
7578
user_list_action_info_list.append(user_list_action_info)
7679

7780
# Create a UserListOperation and populate the UserList.
78-
user_list_operation: UserListOperation = client.get_type("UserListOperation")
81+
user_list_operation: UserListOperation = client.get_type(
82+
"UserListOperation"
83+
)
7984
user_list: UserList = user_list_operation.create
8085
user_list.name = f"Example BasicUserList #{uuid4()}"
8186
user_list.description = (
@@ -87,7 +92,7 @@ def main(
8792
user_list.basic_user_list.actions.extend(user_list_action_info_list)
8893

8994
# Issue a mutate request to add the user list, then print the results.
90-
response: Any = user_list_service.mutate_user_lists(
95+
response: MutateUserListsResponse = user_list_service.mutate_user_lists(
9196
customer_id=customer_id, operations=[user_list_operation]
9297
)
9398
print(

examples/remarketing/add_custom_audience.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import argparse
2424
import sys
2525
from uuid import uuid4
26-
from typing import Any
2726

2827
from google.ads.googleads.client import GoogleAdsClient
2928
from google.ads.googleads.errors import GoogleAdsException
@@ -36,6 +35,9 @@
3635
)
3736
from google.ads.googleads.v20.services.types.custom_audience_service import (
3837
CustomAudienceOperation,
38+
MutateCustomAudiencesResponse,
39+
)
40+
from google.ads.googleads.v20.services.services.custom_audience_service import (
3941
CustomAudienceServiceClient,
4042
)
4143

@@ -99,7 +101,7 @@ def main(client: GoogleAdsClient, customer_id: str) -> None:
99101
)
100102

101103
# Add the custom audience.
102-
custom_audience_response: Any = (
104+
custom_audience_response: MutateCustomAudiencesResponse = (
103105
custom_audience_service.mutate_custom_audiences(
104106
customer_id=customer_id, operations=[custom_audience_operation]
105107
)

examples/remarketing/add_customer_match_user_list.py

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,49 @@
3030
import hashlib
3131
import sys
3232
import uuid
33-
from typing import List, Dict, Any, Optional
33+
from typing import List, Dict, Optional, Union, Iterable
34+
35+
from google.protobuf.any_pb2 import Any
36+
from google.rpc import status_pb2
3437

3538
from google.ads.googleads.client import GoogleAdsClient
3639
from google.ads.googleads.errors import GoogleAdsException
37-
from google.ads.googleads.v20.services.types.google_ads_service import (
40+
from google.ads.googleads.v20.services.services.google_ads_service import (
3841
GoogleAdsServiceClient,
42+
)
43+
from google.ads.googleads.v20.services.services.user_list_service import (
44+
UserListServiceClient,
45+
)
46+
from google.ads.googleads.v20.services.services.offline_user_data_job_service import (
47+
OfflineUserDataJobServiceClient,
48+
)
49+
from google.ads.googleads.v20.services.types.google_ads_service import (
3950
SearchGoogleAdsStreamResponse,
4051
)
4152
from google.ads.googleads.v20.services.types.user_list_service import (
4253
UserListOperation,
43-
UserListServiceClient,
54+
MutateUserListsResponse,
4455
)
4556
from google.ads.googleads.v20.resources.types.user_list import UserList
4657
from google.ads.googleads.v20.services.types.offline_user_data_job_service import (
58+
AddOfflineUserDataJobOperationsRequest,
4759
OfflineUserDataJobOperation,
48-
OfflineUserDataJobServiceClient,
4960
AddOfflineUserDataJobOperationsResponse,
61+
CreateOfflineUserDataJobResponse,
5062
)
5163
from google.ads.googleads.v20.resources.types.offline_user_data_job import (
5264
OfflineUserDataJob,
5365
)
66+
from google.ads.googleads.v20.common.types.criteria import (
67+
AddressInfo,
68+
)
5469
from google.ads.googleads.v20.common.types.offline_user_data import (
5570
UserData,
5671
UserIdentifier,
57-
AddressInfo,
72+
)
73+
from google.ads.googleads.v20.errors.types.errors import (
74+
GoogleAdsFailure,
75+
GoogleAdsError,
5876
)
5977

6078

@@ -151,8 +169,10 @@ def create_customer_match_user_list(
151169
# Sets the membership life span to 30 days.
152170
user_list.membership_life_span = 30
153171

154-
response: Any = user_list_service_client.mutate_user_lists(
155-
customer_id=customer_id, operations=[user_list_operation]
172+
response: MutateUserListsResponse = (
173+
user_list_service_client.mutate_user_lists(
174+
customer_id=customer_id, operations=[user_list_operation]
175+
)
156176
)
157177
user_list_resource_name: str = response.results[0].resource_name
158178
print(
@@ -227,10 +247,10 @@ def add_users_to_customer_match_user_list(
227247
]
228248

229249
# Issues a request to create an offline user data job.
230-
create_offline_user_data_job_response: Any = (
231-
offline_user_data_job_service_client.create_offline_user_data_job(
232-
customer_id=customer_id, job=offline_user_data_job
233-
)
250+
create_offline_user_data_job_response: (
251+
CreateOfflineUserDataJobResponse
252+
) = offline_user_data_job_service_client.create_offline_user_data_job(
253+
customer_id=customer_id, job=offline_user_data_job
234254
)
235255
offline_user_data_job_resource_name = (
236256
create_offline_user_data_job_response.resource_name
@@ -249,34 +269,45 @@ def add_users_to_customer_match_user_list(
249269
# https://developers.google.com/google-ads/api/docs/remarketing/audience-types/customer-match#customer_match_considerations
250270
# and https://developers.google.com/google-ads/api/docs/best-practices/quotas#user_data
251271
# for more information on the per-request limits.
252-
request: Any = client.get_type("AddOfflineUserDataJobOperationsRequest")
272+
request: AddOfflineUserDataJobOperationsRequest = client.get_type(
273+
"AddOfflineUserDataJobOperationsRequest"
274+
)
253275
request.resource_name = offline_user_data_job_resource_name
254276
request.operations = build_offline_user_data_job_operations(client)
255277
request.enable_partial_failure = True
256278

257279
# Issues a request to add the operations to the offline user data job.
258-
response: AddOfflineUserDataJobOperationsResponse = offline_user_data_job_service_client.add_offline_user_data_job_operations(
259-
request=request
280+
response: AddOfflineUserDataJobOperationsResponse = (
281+
offline_user_data_job_service_client.add_offline_user_data_job_operations(
282+
request=request
283+
)
260284
)
261285

262286
# Prints the status message if any partial failure error is returned.
263287
# Note: the details of each partial failure error are not printed here.
264288
# Refer to the error_handling/handle_partial_failure.py example to learn
265289
# more.
266290
# Extracts the partial failure from the response status.
267-
partial_failure: Any = getattr(response, "partial_failure_error", None)
291+
partial_failure: Union[status_pb2.Status, None] = getattr(
292+
response, "partial_failure_error", None
293+
)
268294
if getattr(partial_failure, "code", None) != 0:
269-
error_details: Any = getattr(partial_failure, "details", [])
295+
error_details: Iterable[Any, None] = getattr(
296+
partial_failure, "details", []
297+
)
270298
for error_detail in error_details:
271-
failure_message: Any = client.get_type("GoogleAdsFailure")
299+
failure_message: GoogleAdsFailure = client.get_type(
300+
"GoogleAdsFailure"
301+
)
272302
# Retrieve the class definition of the GoogleAdsFailure instance
273303
# in order to use the "deserialize" class method to parse the
274304
# error_detail string into a protobuf message object.
275-
failure_object: Any = type(failure_message).deserialize(
276-
error_detail.value
277-
)
305+
failure_object: GoogleAdsFailure = type(
306+
failure_message
307+
).deserialize(error_detail.value)
308+
errors: Iterable[GoogleAdsError] = failure_object.errors
278309

279-
for error in failure_object.errors:
310+
for error in errors:
280311
print(
281312
"A partial failure at index "
282313
f"{error.location.field_path_elements[0].index} occurred.\n"
@@ -634,10 +665,18 @@ def normalize_and_hash(s: str, remove_all_whitespace: bool) -> str:
634665
# used for help text, so it's fine if it fails.
635666
try:
636667
consent_status_enum_names = [
637-
e.name for e in GoogleAdsClient.load_from_storage(version="v20").enums.ConsentStatusEnum
668+
e.name
669+
for e in GoogleAdsClient.load_from_storage(
670+
version="v20"
671+
).enums.ConsentStatusEnum
638672
]
639673
except Exception:
640-
consent_status_enum_names = ["UNSPECIFIED", "UNKNOWN", "GRANTED", "DENIED"]
674+
consent_status_enum_names = [
675+
"UNSPECIFIED",
676+
"UNKNOWN",
677+
"GRANTED",
678+
"DENIED",
679+
]
641680
parser.add_argument(
642681
"-d",
643682
"--ad_user_data_consent",

0 commit comments

Comments
 (0)