import os, csv
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def main(client, data):
"""Imports offline call conversion values for calls related to your ads.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The client customer ID string.
conversion_action_id: The ID of the conversion action to upload to.
caller_id: The caller ID from which this call was placed. Caller ID is
expected to be in E.164 format with preceding '+' sign,
e.g. '+16502531234'.
call_start_date_time: The date and time at which the call occurred. The
format is 'yyyy-mm-dd hh:mm:ss+|-hh:mm',
e.g. '2021-01-01 12:32:45-08:00'.
conversion_date_time: The the date and time of the conversion (should be
after the click time). The format is 'yyyy-mm-dd hh:mm:ss+|-hh:mm',
e.g. '2021-01-01 12:32:45-08:00'.
conversion_value: The conversion value in the desired currency.
"""
# Get the ConversionUploadService client.
conversion_upload_service = client.get_service(
"ConversionUploadService")
call_conversions = []
for item in data:
customer_id = item[0]
conversion_action_id = item[1]
caller_id = item[2]
call_start_date_time = item[3]
conversion_date_time = item[4]
conversion_value = item[5]
# Create a call conversion in USD currency.
call_conversion = client.get_type(
"CallConversion")
call_conversion.conversion_action = client.get_service(
"ConversionActionService"
).conversion_action_path(customer_id, conversion_action_id)
call_conversion.caller_id = caller_id
call_conversion.call_start_date_time = call_start_date_time
call_conversion.conversion_date_time = conversion_date_time
call_conversion.conversion_value = conversion_value
call_conversion.currency_code =
"USD"
call_conversions.append(call_conversion)
# Issue a request to upload the call conversion.
request = client.get_type(
"UploadCallConversionsRequest")
request.customer_id = customer_id
request.conversions = call_conversions
request.partial_failure = True
upload_call_conversions_response = (
conversion_upload_service.upload_call_conversions(request=request)
)
# Print any partial errors returned.
if upload_call_conversions_response.partial_failure_error:
print(
"Partial error ocurred: "
f"'{upload_call_conversions_response.partial_failure_error.message}
'"
)
# Print the result if valid.
uploaded_call_conversion = upload_call_conversions_response.results[0]
if uploaded_call_conversion.call_start_date_time:
print(
"Uploaded call conversion that occurred at "
f"'{uploaded_call_conversion.call_start_date_time}
' "
f"for caller ID '{uploaded_call_conversion.caller_id}
' "
"to the conversion action with resource name "
f"'{uploaded_call_conversion.conversion_action}
'."
)
if __name__ ==
"__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
application_path = os.path.abspath(os.path.dirname(__file__))
googleads_client = GoogleAdsClient.load_from_storage(version=
"v7",
path=os.path.join(application_path,
'google-ads.yaml'))
# data = list(csv.reader(open(os.path.join(application_path, 'offline-call-conversions.csv'))))
data = [
[
'27548034--',
# Customer ID
'593803131',
# conversion_action_id - This had to be an "import" action
'+186093842--',
# caller_id
'2021-05-04 19:00:00-04:00',
# call_start_time
'2021-05-05 19:32:55-04:00',
# conversion_time, optional
50.45
# conversion_value, optional
]
]
main(googleads_client, data)