I am downloading some data from Google Analytics using Google API v4. I am getting data, and I am trying to use the pageToken
parameter to request the next page when pageSize is exceeded. However, my pagination function, which should pass the new pageToken
into a new request, enters a loop where it performs endlessly the same, first request (given that this line: print(response['reports'][0]['nextPageToken'])
prints always the max value of pagesize, which is the value nextPageToken
takes with the very first request).
The query should produce ~8000 results/rows.
What I tried was to create a variable for the pageToken
parameter in the request and making this variable to take the nextPageToken
value in the new request made by the recursive function:
pageTokenVariable = "whatever"
sample_request = {
'viewId': '1234',
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = 1),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
},
'dimensions': [
{'name': 'ga:date'},
{'name': 'ga:eventlabel'}
],
'metrics': [
{'expression': 'ga:users'},
{'expression': 'ga:totalevents'}
],
'pageToken':pageTokenVariable,
'pageSize': 1000
}
# pagination function
def main(client, pageTokenVariable):
response = client.reports().batchGet(
body={
'reportRequests':sample_request
}).execute()
if 'nextPageToken' in response['reports'][0]:
print(response['reports'][0]['nextPageToken']) #trying to debug
pageTokenVariable = response['reports'][0]['nextPageToken']
response = main(client, pageTokenVariable)
return(response)
Nonetheless, it does not work as intended. What am I missing?