3838import argparse
3939from datetime import datetime , timedelta
4040import sys
41- from typing import Any , Dict , List , Tuple
41+ from typing import Dict , List , Union
4242from uuid import uuid4
4343
44- from examples .utils .example_helpers import get_image_bytes_from_url
4544from google .api_core import protobuf_helpers
45+
46+ from examples .utils .example_helpers import get_image_bytes_from_url
4647from google .ads .googleads .client import GoogleAdsClient
4748from google .ads .googleads .errors import GoogleAdsException
4849from 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- )
7050from google .ads .googleads .v20 .enums .types .conversion_action_category import (
7151 ConversionActionCategoryEnum ,
7252)
7353from 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)
8259from google .ads .googleads .v20 .resources .types .asset import Asset
8360from google .ads .googleads .v20 .resources .types .asset_group import AssetGroup
11289from 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- )
11892from google .ads .googleads .v20 .services .services .campaign_service import (
11993 CampaignServiceClient ,
12094)
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]
925901def 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(
966958def 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