Posts tagged with google-api-python-client

I'm using Google Ads API v11 to upload conversions and adjust conversions. I send hundreds of conversions each day and want to start sending batch requests instead.

I've followed Google's documentation and I upload/ adjust conversions exactly the way they stated. https://developers.google.com/google-ads/api/docs/conversions/upload-clicks https://developers.google.com/google-ads/api/docs/conversions/upload-adjustments

I could not find any good explanation or example on how to send batch requests: https://developers.google.com/google-ads/api/reference/rpc/v11/BatchJobService

Below is my code, an example of how I adjust hundreds of conversions. An explanation of how to do so with batch requests would be very appreciated.

# Adjust the conversion value of an existing conversion, via Google Ads API def adjust_offline_conversion(client,     customer_id,     conversion_action_id,     gclid,     conversion_date_time,     adjustment_date_time,     restatement_value,     adjustment_type='RESTATEMENT'):     # Check that gclid is valid string else exit the function     if type(gclid) is not str:         return None     # Check if datetime or string, if string make as datetime     if type(conversion_date_time) is str:         conversion_date_time = datetime.strptime(conversion_date_time, '%Y-%m-%d %H:%M:%S')     # Add 1 day forward to conversion time to avoid this error (as explained by Google: "The Offline Conversion cannot happen before the ad click. Add 1-2 days to your conversion time in your upload, or check that the time zone is properly set.")     to_datetime_plus_one = conversion_date_time + timedelta(days=1)     # If time is bigger than now, set as now (it will be enough to avoid the original google error, but to avoid a new error since google does not support future dates that are bigger than now)     to_datetime_plus_one = to_datetime_plus_one if to_datetime_plus_one < datetime.utcnow() else datetime.utcnow()     # We must convert datetime back to string + add time zone suffix (+00:00 or -00:00 this is utc) **in order to work with google ads api**     adjusted_string_date = to_datetime_plus_one.strftime('%Y-%m-%d %H:%M:%S') + "+00:00"     conversion_adjustment_type_enum = client.enums.ConversionAdjustmentTypeEnum     # Determine the adjustment type.     conversion_adjustment_type = conversion_adjustment_type_enum[adjustment_type].value     # Associates conversion adjustments with the existing conversion action.     # The GCLID should have been uploaded before with a conversion     conversion_adjustment = client.get_type("ConversionAdjustment")     conversion_action_service = client.get_service("ConversionActionService")     conversion_adjustment.conversion_action = (         conversion_action_service.conversion_action_path(             customer_id, conversion_action_id         )     )     conversion_adjustment.adjustment_type = conversion_adjustment_type     conversion_adjustment.adjustment_date_time = adjustment_date_time.strftime('%Y-%m-%d %H:%M:%S') + "+00:00"     # Set the Gclid Date     conversion_adjustment.gclid_date_time_pair.gclid = gclid     conversion_adjustment.gclid_date_time_pair.conversion_date_time = adjusted_string_date     # Sets adjusted value for adjustment type RESTATEMENT.     if conversion_adjustment_type == conversion_adjustment_type_enum.RESTATEMENT.value:         conversion_adjustment.restatement_value.adjusted_value = float(restatement_value)     conversion_adjustment_upload_service = client.get_service("ConversionAdjustmentUploadService")     request = client.get_type("UploadConversionAdjustmentsRequest")     request.customer_id = customer_id     request.conversion_adjustments = [conversion_adjustment]     request.partial_failure = True     response = (         conversion_adjustment_upload_service.upload_conversion_adjustments(             request=request,         )     )     conversion_adjustment_result = response.results[0]     print(         f"Uploaded conversion that occurred at "         f'"{conversion_adjustment_result.adjustment_date_time}" '         f"from Gclid "         f'"{conversion_adjustment_result.gclid_date_time_pair.gclid}"'         f' to "{conversion_adjustment_result.conversion_action}"'     ) # Iterate every row (subscriber) and call the "adjust conversion" function for it df.apply(lambda row: adjust_offline_conversion(client=client                                                    , customer_id=customer_id                                                    , conversion_action_id='xxxxxxx'                                                    , gclid=row['click_id']                                                    , conversion_date_time=row['subscription_time']                                                    , adjustment_date_time=datetime.utcnow()                                                    , restatement_value=row['revenue'])                                                    , axis=1) 

I'm trying to use Google Ads API using service account.
The following works:

from google.ads.googleads.client import GoogleAdsClient from google.oauth2.service_account import Credentials as ServiceAccountCreds oauth2_client = ServiceAccountCreds.from_service_account_file(     filename=key_file, subject=developer_account,     scopes=["https://www.googleapis.com/auth/adwords"] ) google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=oauth2_client,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_serice.search_stream(customer_id=self.client_customer_id, query =  query) 

I want to to do following with Identity Workload Federation(w/o key):

from google.auth import aws as google_auth_aws from google.ads.googleads.client import GoogleAdsClient credentials = google_auth_aws.Credentials.from_info(json_config_info)  # from workload identity federation credentials = credentials.with_scopes(["https://www.googleapis.com/auth/adwords"]) credentials = credentials.with_subject(self.developer_account) # Build the service object. self.google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=credentials,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_service.search_stream(customer_id =self.client_customer_id, query =  self.query) 

Which throws error: ERROR:'Credentials' object has no attribute 'with_subject'.

I've been trying to follow the examples and documentation for the python ad_manager library for the google ads API, but I haven't been able to complete a successful request. I currently have my developer token, client_id, client_secret, and refresh_token in my google ads YAML file, but I'm constantly getting the error "argument should be integer or bytes-like object, not 'str'" when calling the function WaitForReport following the example code below. I was wondering if anyone had any advice on how I could tackle this issue.

 import tempfile # Import appropriate modules from the client library. from googleads import ad_manager from googleads import errors def main(client):   # Initialize a DataDownloader.   report_downloader = client.GetDataDownloader(version='v202111')   # Create report job.   report_job = {       'reportQuery': {           'dimensions': ['COUNTRY_NAME', 'LINE_ITEM_ID', 'LINE_ITEM_NAME'],           'columns': ['UNIQUE_REACH_FREQUENCY', 'UNIQUE_REACH_IMPRESSIONS',                       'UNIQUE_REACH'],           'dateRangeType': 'REACH_LIFETIME'       }   }   try:     # Run the report and wait for it to finish.     report_job_id = report_downloader.WaitForReport(report_job)   except errors.AdManagerReportError as e:     print('Failed to generate report. Error was: %s' % e)   # Change to your preferred export format.   export_format = 'CSV_DUMP'   report_file = tempfile.NamedTemporaryFile(suffix='.csv.gz', delete=False)   # Download report data.   report_downloader.DownloadReportToFile(       report_job_id, export_format, report_file)   report_file.close()   # Display results.   print('Report job with id "%s" downloaded to:\n%s' % (       report_job_id, report_file.name)) if __name__ == '__main__':   # Initialize client object.   ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage()   main(ad_manager_client) 

Edit: Below is the stack trace:

Traceback (most recent call last):   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googleads/common.py", line 984, in MakeSoapRequest     return soap_service_method(   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/zeep/proxy.py", line 46, in __call__     return self._proxy._binding.send(   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 135, in send     return self.process_reply(client, operation_obj, response)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 229, in process_reply     return self.process_error(doc, operation)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 317, in process_error     raise Fault( zeep.exceptions.Fault: Unknown fault occured During handling of the above exception, another exception occurred: Traceback (most recent call last):   File "google_ads.py", line 72, in <module>     main(ad_manager_client)   File "google_ads.py", line 33, in main1     report_job_id = report_downloader.WaitForReport(report_job)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googleads/ad_manager.py", line 784, in WaitForReport     report_job_id = service.runReportJob(report_job)['id']   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googleads/common.py", line 989, in MakeSoapRequest     underlying_exception = e.detail.find( TypeError: argument should be integer or bytes-like object, not 'str' 

I have two issues:

I'm calling G-Ads API to bulk remove ads (code)

for error_detail in error_details:             # Retrieve an instance of the google_ads_failure class from the client             failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")             # Parse the string into a google_ads_failure message instance.             # To access class-only methods on the message we retrieve its type.             google_ads_failure = type(failure_message)             failure_object = google_ads_failure.deserialize(error_detail.value)             for error in failure_object.errors:                 # Construct and print a string that details which element in                 # the above ad_group_operations list failed (by index number)                 # as well as the error message and error code.                 print("A partial failure at index "                       f"{error.location.field_path_elements[0].index} occurred "                       f"\nError message: {error.message}\nError code: "                       f"{error.error_code}")                 index_array.append(error.location.field_path_elements[0].index)                 error_array.append({"error_message": error.message, "error_code": error.error_code}) 
  1. I get a partial error
  2. And the code fails to parse it (taken form the official website)

My terminal shows:

Partial failures occurred. Details will be shown below.

Traceback (most recent call last):   File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 426, in get_type     message_class = getattr(type_classes, name)   File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\v8\__init__.py", line 1753, in __getattr__     raise AttributeError(f"unknown type {name!r}.") AttributeError: unknown type 'google_ads_failure'. During handling of the above exception, another exception occurred: Traceback (most recent call last):   File "D:\projects\bowling\src\main.py", line 535, in <module>     main(args.top_id)   File "D:\projects\bowling\src\main.py", line 141, in main     removed_ads_count = remove_disapproved_ads_for_account(account)   File "D:\projects\bowling\src\main.py", line 206, in remove_disapproved_ads_for_account     remove_ads(ad_removal_operations, ads_to_remove_json, account_id)   File "D:\projects\bowling\src\main.py", line 300, in remove_ads     index_array, error_array = _print_results(response_chunk)   File "D:\projects\bowling\src\main.py", line 439, in _print_results     failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")   File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 428, in get_type     raise ValueError( ValueError: Specified type 'google_ads_failure' does not exist in Google Ads API v8