2222
2323import argparse
2424import sys
25+ from typing import Optional
2526
2627from google .ads .googleads .client import GoogleAdsClient
2728from google .ads .googleads .errors import GoogleAdsException
29+ from google .ads .googleads .v20 .services .services .conversion_action_service import (
30+ ConversionActionServiceClient ,
31+ )
32+ from google .ads .googleads .v20 .services .services .conversion_upload_service import (
33+ ConversionUploadServiceClient ,
34+ )
35+ from google .ads .googleads .v20 .services .types .conversion_upload_service import (
36+ ClickConversion ,
37+ ClickConversionResult ,
38+ CustomVariable ,
39+ UploadClickConversionsRequest ,
40+ UploadClickConversionsResponse ,
41+ )
2842
2943
3044# [START upload_offline_conversion]
3145def main (
32- client ,
33- customer_id ,
34- conversion_action_id ,
35- gclid ,
36- conversion_date_time ,
37- conversion_value ,
38- conversion_custom_variable_id ,
39- conversion_custom_variable_value ,
40- gbraid ,
41- wbraid ,
42- order_id ,
43- ad_user_data_consent ,
44- ):
46+ client : GoogleAdsClient ,
47+ customer_id : str ,
48+ conversion_action_id : str ,
49+ gclid : Optional [ str ] ,
50+ conversion_date_time : str ,
51+ conversion_value : str ,
52+ conversion_custom_variable_id : Optional [ str ] ,
53+ conversion_custom_variable_value : Optional [ str ] ,
54+ gbraid : Optional [ str ] ,
55+ wbraid : Optional [ str ] ,
56+ order_id : Optional [ str ] ,
57+ ad_user_data_consent : Optional [ str ] ,
58+ ) -> None :
4559 """Creates a click conversion with a default currency of USD.
4660
4761 Args:
@@ -65,9 +79,13 @@ def main(
6579 order_id: The order ID for the click conversion.
6680 ad_user_data_consent: The ad user data consent for the click.
6781 """
68- click_conversion = client .get_type ("ClickConversion" )
69- conversion_upload_service = client .get_service ("ConversionUploadService" )
70- conversion_action_service = client .get_service ("ConversionActionService" )
82+ click_conversion : ClickConversion = client .get_type ("ClickConversion" )
83+ conversion_upload_service : ConversionUploadServiceClient = (
84+ client .get_service ("ConversionUploadService" )
85+ )
86+ conversion_action_service : ConversionActionServiceClient = (
87+ client .get_service ("ConversionActionService" )
88+ )
7189 click_conversion .conversion_action = (
7290 conversion_action_service .conversion_action_path (
7391 customer_id , conversion_action_id
@@ -87,7 +105,9 @@ def main(
87105 click_conversion .currency_code = "USD"
88106
89107 if conversion_custom_variable_id and conversion_custom_variable_value :
90- conversion_custom_variable = client .get_type ("CustomVariable" )
108+ conversion_custom_variable : CustomVariable = client .get_type (
109+ "CustomVariable"
110+ )
91111 conversion_custom_variable .conversion_custom_variable = (
92112 conversion_upload_service .conversion_custom_variable_path (
93113 customer_id , conversion_custom_variable_id
@@ -114,16 +134,20 @@ def main(
114134 # multiple conversions to upload, it's most efficient to upload them in a
115135 # single request. See the following for per-request limits for reference:
116136 # https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service
117- request = client .get_type ("UploadClickConversionsRequest" )
137+ request : UploadClickConversionsRequest = client .get_type (
138+ "UploadClickConversionsRequest"
139+ )
118140 request .customer_id = customer_id
119141 request .conversions .append (click_conversion )
120142 request .partial_failure = True
121- conversion_upload_response = (
143+ conversion_upload_response : UploadClickConversionsResponse = (
122144 conversion_upload_service .upload_click_conversions (
123145 request = request ,
124146 )
125147 )
126- uploaded_click_conversion = conversion_upload_response .results [0 ]
148+ uploaded_click_conversion : ClickConversionResult = (
149+ conversion_upload_response .results [0 ]
150+ )
127151 print (
128152 f"Uploaded conversion that occurred at "
129153 f'"{ uploaded_click_conversion .conversion_date_time } " from '
@@ -136,9 +160,11 @@ def main(
136160if __name__ == "__main__" :
137161 # GoogleAdsClient will read the google-ads.yaml configuration file in the
138162 # home directory if none is specified.
139- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
163+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
164+ version = "v20"
165+ )
140166
141- parser = argparse .ArgumentParser (
167+ parser : argparse . ArgumentParser = argparse .ArgumentParser (
142168 description = "Uploads an offline conversion."
143169 )
144170 # The following argument(s) should be provided to run the example.
@@ -224,10 +250,14 @@ def main(
224250 "-s" ,
225251 "--ad_user_data_consent" ,
226252 type = str ,
227- choices = [e .name for e in GoogleAdsClient .load_from_storage (version = "v20" ).enums .ConsentStatusEnum ],
253+ choices = [
254+ e .name
255+ for e in googleads_client .enums .ConsentStatusEnum
256+ if e .name not in ("UNSPECIFIED" , "UNKNOWN" )
257+ ],
228258 help = ("The ad user data consent for the click." ),
229259 )
230- args = parser .parse_args ()
260+ args : argparse . Namespace = parser .parse_args ()
231261
232262 try :
233263 main (
0 commit comments