Skip to content

Commit 719fefe

Browse files
committed
Fix annotations in shopping ads examples
Change-Id: I836105786063f23d40941a8f8f1986d3d48e2253
1 parent 2eb243a commit 719fefe

7 files changed

Lines changed: 99 additions & 155 deletions

examples/shopping_ads/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
# Fully type-hinted

examples/shopping_ads/add_listing_scope.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import argparse
3131
import sys
32-
from typing import Any, List
32+
from typing import List
3333

3434
from google.ads.googleads.client import GoogleAdsClient
3535
from google.ads.googleads.errors import GoogleAdsException
@@ -51,14 +51,13 @@
5151
from google.ads.googleads.v20.services.services.campaign_service import (
5252
CampaignServiceClient,
5353
)
54-
from google.ads.googleads.v20.services.types.campaign_criterion_operation import (
54+
from google.ads.googleads.v20.services.types.campaign_criterion_service import (
5555
CampaignCriterionOperation,
56+
MutateCampaignCriteriaResponse,
5657
)
5758

5859

59-
def main(
60-
client: GoogleAdsClient, customer_id: str, campaign_id: str
61-
) -> None:
60+
def main(client: GoogleAdsClient, customer_id: str, campaign_id: str) -> None:
6261
campaign_service: CampaignServiceClient = client.get_service(
6362
"CampaignService"
6463
)
@@ -121,7 +120,7 @@ def main(
121120
client.get_service("CampaignCriterionService")
122121
)
123122

124-
campaign_criterion_response: Any = (
123+
campaign_criterion_response: MutateCampaignCriteriaResponse = (
125124
campaign_criterion_service.mutate_campaign_criteria(
126125
customer_id=customer_id, operations=[campaign_criterion_operation]
127126
)

examples/shopping_ads/add_performance_max_product_listing_group_tree.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@
2525

2626
import argparse
2727
import sys
28-
from typing import Any, Dict, List, Optional
28+
from typing import Dict, List, Optional
2929

3030
from google.ads.googleads.client import GoogleAdsClient
3131
from google.ads.googleads.errors import GoogleAdsException
32-
from google.ads.googleads.v20.common.types.listing_group_filter_dimension import (
32+
from google.ads.googleads.v20.resources.types.asset_group_listing_group_filter import (
3333
ListingGroupFilterDimension,
3434
)
35-
from google.ads.googleads.v20.enums.types.listing_group_filter_product_condition import (
36-
ListingGroupFilterProductConditionEnum,
37-
)
38-
from google.ads.googleads.v20.enums.types.listing_group_filter_type import (
39-
ListingGroupFilterTypeEnum,
40-
)
41-
from google.ads.googleads.v20.enums.types.listing_group_filter_listing_source import (
42-
ListingGroupFilterListingSourceEnum,
43-
)
4435
from google.ads.googleads.v20.resources.types.asset_group_listing_group_filter import (
4536
AssetGroupListingGroupFilter,
4637
)
@@ -52,7 +43,7 @@
5243
SearchGoogleAdsRequest,
5344
SearchGoogleAdsResponse,
5445
)
55-
from google.ads.googleads.v20.services.types.mutate_operation import (
46+
from google.ads.googleads.v20.services.types.google_ads_service import (
5647
MutateOperation,
5748
)
5849

@@ -95,9 +86,9 @@ def __init__(
9586
# node and any parent to child edges in the tree.
9687
for listing_group_filter_node in listing_group_filters:
9788
resource_name: str = listing_group_filter_node.resource_name
98-
parent_resource_name: Optional[
99-
str
100-
] = listing_group_filter_node.parent_listing_group_filter
89+
parent_resource_name: Optional[str] = (
90+
listing_group_filter_node.parent_listing_group_filter
91+
)
10192

10293
# When the node has no parent, it means it's the root node.
10394
if not parent_resource_name:
@@ -423,10 +414,10 @@ def main(
423414

424415
if replace_existing_tree:
425416
# Retrieve a list of existing AssetGroupListingGroupFilters
426-
existing_listing_group_filters: List[
427-
AssetGroupListingGroupFilter
428-
] = get_all_existing_listing_group_filter_assets_in_asset_group(
429-
client, customer_id, asset_group_resource_name
417+
existing_listing_group_filters: List[AssetGroupListingGroupFilter] = (
418+
get_all_existing_listing_group_filter_assets_in_asset_group(
419+
client, customer_id, asset_group_resource_name
420+
)
430421
)
431422

432423
# If present, create MutateOperations to remove each
@@ -578,7 +569,9 @@ def get_all_existing_listing_group_filter_assets_in_asset_group(
578569
googleads_service: GoogleAdsServiceClient = client.get_service(
579570
"GoogleAdsService"
580571
)
581-
response: SearchGoogleAdsResponse = googleads_service.search(request=request)
572+
response: SearchGoogleAdsResponse = googleads_service.search(
573+
request=request
574+
)
582575

583576
return [
584577
result.asset_group_listing_group_filter

examples/shopping_ads/add_performance_max_retail_campaign.py

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,23 @@
3838
import argparse
3939
from datetime import datetime, timedelta
4040
import sys
41-
from typing import Any, Dict, List, Tuple
41+
from typing import Dict, List, Union
4242
from uuid import uuid4
4343

44-
from examples.utils.example_helpers import get_image_bytes_from_url
4544
from google.api_core import protobuf_helpers
45+
46+
from examples.utils.example_helpers import get_image_bytes_from_url
4647
from google.ads.googleads.client import GoogleAdsClient
4748
from google.ads.googleads.errors import GoogleAdsException
4849
from google.ads.googleads.util import convert_snake_case_to_upper_case
49-
from google.ads.googleads.v20.common.types.criteria import LanguageInfo
50-
from google.ads.googleads.v20.common.types.asset_types import (
51-
ImageAsset,
52-
TextAsset,
53-
)
54-
from google.ads.googleads.v20.enums.types.advertising_channel_type import (
55-
AdvertisingChannelTypeEnum,
56-
)
57-
from google.ads.googleads.v20.enums.types.asset_field_type import (
58-
AssetFieldTypeEnum,
59-
)
60-
from google.ads.googleads.v20.enums.types.asset_group_status import (
61-
AssetGroupStatusEnum,
62-
)
63-
from google.ads.googleads.v20.enums.types.asset_type import AssetTypeEnum
64-
from google.ads.googleads.v20.enums.types.budget_delivery_method import (
65-
BudgetDeliveryMethodEnum,
66-
)
67-
from google.ads.googleads.v20.enums.types.campaign_status import (
68-
CampaignStatusEnum,
69-
)
7050
from google.ads.googleads.v20.enums.types.conversion_action_category import (
7151
ConversionActionCategoryEnum,
7252
)
7353
from google.ads.googleads.v20.enums.types.conversion_origin import (
7454
ConversionOriginEnum,
7555
)
76-
from google.ads.googleads.v20.enums.types.listing_group_filter_listing_source import (
77-
ListingGroupFilterListingSourceEnum,
78-
)
79-
from google.ads.googleads.v20.enums.types.listing_group_filter_type import (
80-
ListingGroupFilterTypeEnum,
56+
from google.ads.googleads.v20.enums.types.asset_field_type import (
57+
AssetFieldTypeEnum,
8158
)
8259
from google.ads.googleads.v20.resources.types.asset import Asset
8360
from google.ads.googleads.v20.resources.types.asset_group import AssetGroup
@@ -112,9 +89,6 @@
11289
from google.ads.googleads.v20.services.services.campaign_conversion_goal_service import (
11390
CampaignConversionGoalServiceClient,
11491
)
115-
from google.ads.googleads.v20.services.services.campaign_criterion_service import (
116-
CampaignCriterionServiceClient,
117-
)
11892
from google.ads.googleads.v20.services.services.campaign_service import (
11993
CampaignServiceClient,
12094
)
@@ -130,7 +104,7 @@
130104
SearchGoogleAdsRequest,
131105
SearchGoogleAdsResponse,
132106
)
133-
from google.ads.googleads.v20.services.types.mutate_operation import (
107+
from google.ads.googleads.v20.services.types.google_ads_service import (
134108
MutateOperation,
135109
)
136110

@@ -176,7 +150,13 @@ def main(
176150
# This campaign will override the customer conversion goals.
177151
# Retrieve the current list of customer conversion goals.
178152
customer_conversion_goals: List[
179-
Dict[str, Any]
153+
Dict[
154+
str,
155+
Union[
156+
ConversionActionCategoryEnum.ConversionActionCategory,
157+
ConversionOriginEnum.ConversionOrigin,
158+
],
159+
]
180160
] = get_customer_conversion_goals(client, customer_id)
181161

182162
# Performance Max campaigns require that repeated assets such as headlines
@@ -185,9 +165,7 @@ def main(
185165
# https://developers.google.com/google-ads/api/docs/performance-max/assets
186166
#
187167
# Create the headlines.
188-
headline_asset_resource_names: List[
189-
str
190-
] = create_multiple_text_assets(
168+
headline_asset_resource_names: List[str] = create_multiple_text_assets(
191169
client,
192170
customer_id,
193171
[
@@ -197,9 +175,7 @@ def main(
197175
],
198176
)
199177
# Create the descriptions.
200-
description_asset_resource_names: List[
201-
str
202-
] = create_multiple_text_assets(
178+
description_asset_resource_names: List[str] = create_multiple_text_assets(
203179
client,
204180
customer_id,
205181
[
@@ -229,11 +205,11 @@ def main(
229205
brand_guidelines_enabled,
230206
)
231207
)
232-
campaign_criterion_operations: List[
233-
MutateOperation
234-
] = create_campaign_criterion_operations(
235-
client,
236-
customer_id,
208+
campaign_criterion_operations: List[MutateOperation] = (
209+
create_campaign_criterion_operations(
210+
client,
211+
customer_id,
212+
)
237213
)
238214
asset_group_operation: MutateOperation = create_asset_group_operation(
239215
client, customer_id, final_url
@@ -250,12 +226,12 @@ def main(
250226
brand_guidelines_enabled,
251227
)
252228
)
253-
conversion_goal_operations: List[
254-
MutateOperation
255-
] = create_conversion_goal_operations(
256-
client,
257-
customer_id,
258-
customer_conversion_goals,
229+
conversion_goal_operations: List[MutateOperation] = (
230+
create_conversion_goal_operations(
231+
client,
232+
customer_id,
233+
customer_conversion_goals,
234+
)
259235
)
260236

261237
# Send the operations in a single Mutate request.
@@ -313,8 +289,10 @@ def create_campaign_budget_operation(
313289
campaign_budget_service: CampaignBudgetServiceClient = client.get_service(
314290
"CampaignBudgetService"
315291
)
316-
campaign_budget.resource_name = campaign_budget_service.campaign_budget_path(
317-
customer_id, _BUDGET_TEMPORARY_ID
292+
campaign_budget.resource_name = (
293+
campaign_budget_service.campaign_budget_path(
294+
customer_id, _BUDGET_TEMPORARY_ID
295+
)
318296
)
319297

320298
return mutate_operation
@@ -565,9 +543,6 @@ def create_asset_group_operation(
565543
Returns:
566544
a MutateOperation that creates a new asset group.
567545
"""
568-
googleads_service: GoogleAdsServiceClient = client.get_service(
569-
"GoogleAdsService"
570-
)
571546
campaign_service: CampaignServiceClient = client.get_service(
572547
"CampaignService"
573548
)
@@ -913,9 +888,10 @@ def sorter(operation: MutateOperation) -> bool:
913888
True if the MutateOperation creates an asset group asset.
914889
"""
915890
# Check if the oneof field 'asset_group_asset_operation' is set.
916-
return operation.asset_group_asset_operation != type(
891+
return (
917892
operation.asset_group_asset_operation
918-
)()
893+
!= type(operation.asset_group_asset_operation)()
894+
)
919895

920896
return sorted(operations, key=sorter)
921897
# [END add_performance_max_retail_campaign_12]
@@ -924,7 +900,15 @@ def sorter(operation: MutateOperation) -> bool:
924900
# [START add_performance_max_retail_campaign_9]
925901
def get_customer_conversion_goals(
926902
client: GoogleAdsClient, customer_id: str
927-
) -> List[Dict[str, Any]]:
903+
) -> List[
904+
Dict[
905+
str,
906+
Union[
907+
ConversionActionCategoryEnum.ConversionActionCategory,
908+
ConversionOriginEnum.ConversionOrigin,
909+
],
910+
]
911+
]:
928912
"""Retrieves the list of customer conversion goals.
929913
930914
Args:
@@ -936,7 +920,15 @@ def get_customer_conversion_goals(
936920
conversion goals.
937921
"""
938922
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
939-
customer_conversion_goals: List[Dict[str, Any]] = []
923+
customer_conversion_goals: List[
924+
Dict[
925+
str,
926+
Union[
927+
ConversionActionCategoryEnum.ConversionActionCategory,
928+
ConversionOriginEnum.ConversionOrigin,
929+
],
930+
]
931+
] = []
940932
query: str = """
941933
SELECT
942934
customer_conversion_goal.category,
@@ -966,7 +958,15 @@ def get_customer_conversion_goals(
966958
def create_conversion_goal_operations(
967959
client: GoogleAdsClient,
968960
customer_id: str,
969-
customer_conversion_goals: List[Dict[str, Any]],
961+
customer_conversion_goals: List[
962+
Dict[
963+
str,
964+
Union[
965+
ConversionActionCategoryEnum.ConversionActionCategory,
966+
ConversionOriginEnum.ConversionOrigin,
967+
],
968+
]
969+
],
970970
) -> List[MutateOperation]:
971971
"""Creates a list of MutateOperations that override customer conversion goals.
972972
@@ -993,12 +993,12 @@ def create_conversion_goal_operations(
993993
mutate_operation.campaign_conversion_goal_operation.update
994994
)
995995

996-
category_enum_value: ConversionActionCategoryEnum.ConversionActionCategory = customer_goal_dict[
997-
"category"
998-
]
999-
origin_enum_value: ConversionOriginEnum.ConversionOrigin = customer_goal_dict[
1000-
"origin"
1001-
]
996+
category_enum_value: (
997+
ConversionActionCategoryEnum.ConversionActionCategory
998+
) = customer_goal_dict["category"]
999+
origin_enum_value: ConversionOriginEnum.ConversionOrigin = (
1000+
customer_goal_dict["origin"]
1001+
)
10021002

10031003
campaign_conversion_goal.resource_name = (
10041004
campaign_conversion_goal_service.campaign_conversion_goal_path(
@@ -1019,8 +1019,7 @@ def create_conversion_goal_operations(
10191019
if (
10201020
category_enum_value
10211021
== client.enums.ConversionActionCategoryEnum.PURCHASE
1022-
and origin_enum_value
1023-
== client.enums.ConversionOriginEnum.WEBSITE
1022+
and origin_enum_value == client.enums.ConversionOriginEnum.WEBSITE
10241023
):
10251024
biddable = True
10261025
else:

0 commit comments

Comments
 (0)