Can anyone please help with error handling & for loop in my GA reporting API python script?
What I would like it to do is attempt requesting data from the API an n number of times (5 times), if there is an error while pulling the data (generally "Service Unavailable"), log it (log_progress function), but keep trying for n number of times; eventually, if the number of attempts reaches the maximum amount and the API is still returning an error, run a send_email function (which will notify me that some data was not downloaded) and move on with the code to the next item (there is a wider for loop in the script which loops through different GA views/days).
for n in range(0, 5):
try: #GA API request
api_request = {
'viewId': viewId,
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = i),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now() - timedelta(days = i),'%Y-%m-%d')
},
'dimensions': [{'name': 'ga:date'},{'name': 'ga:countryIsoCode'}],
'metrics': [{'expression': 'ga:sessions'}],
"samplingLevel": "LARGE",
"pageSize": 100000 }
response = api_client.reports().batchGet(
body={
'reportRequests': api_request
}).execute()
except HttpError as error:
log_progress('errorLog.txt' , error.resp.reason + " - code will try again")
pass
Unfortunately tesing this script is made more complicated by the randomness of GA errors which rarely seem to happen while I'm running the script manually.
One way of doing so is by creating a while
loop and assume failure from the start. Only when the attempt number hits your maximum, you can evoke the send_email()
module in the except
block.
success = False
max_attempts = 5
attempts = 0
while not success and attempts < max_attempts:
attempts += 1
try: #GA API request
api_request = {
'viewId': viewId,
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = i),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now() - timedelta(days = i),'%Y-%m-%d')
},
'dimensions': [{'name': 'ga:date'},{'name': 'ga:countryIsoCode'}],
'metrics': [{'expression': 'ga:sessions'}],
"samplingLevel": "LARGE",
"pageSize": 100000 }
response = api_client.reports().batchGet(
body={
'reportRequests': api_request
}).execute()
success = True
except HttpError as error:
log_progress('errorLog.txt' , error.resp.reason + " - code will try again")
if attempts == max_attempts:
sent_email()
In this scenario, if there was no success after 5 attempts the program will stop trying and will continue executing the rest of the logic.