Using the nextLink attribute to get the next page of results

I am using the python client API Google API to load some data from Google Analytics. I basically copied one of my exams and modified it to do exactly what I need.

I took this piece of code from examples:

request = service.data().ga().get( ids=ids, start_date=str(start_date), end_date=str(end_date), dimensions=','.join(dimensions), filters=filters, sort="ga:date", metrics=','.join(metrics)) 

Then add it to the batch object and execute it as soon as it collects 10 queries. This all works well, but the problem is that some of these queries return "nextLink". Now I could just create a new query object (with the code above) with a different starting index, but is it not the best?

Is there a way to simply parse nextLink in a new request object?

+4
source share
3 answers

I use this approach:

 firstRun = True params = {'ids':'ga:00000001', 'start_date':'2013-07-01', 'end_date':'2013-07-31', 'metrics':'ga:visits', 'dimensions':'ga:source', 'sort':'-ga:visits', 'start_index':1, 'max_results':10000} while firstRun == True or result.get('nextLink'): if firstRun == False: params['start_index'] = int(params['start_index']) + int(params['max_results']) result = service.data().ga().get(**params).execute() firstRun = False 
+6
source

I could not find a way to parse the nextLink object and execute a request with it, but this was my solution and works great:

 max_results = 10000 params = { 'ids': 'ga:' + profile_id, 'start_date': start_date, 'end_date': end_date, 'metrics': ','.join(metrics), 'dimensions': ','.join(dimensions), 'start_index': 1, 'max_results': max_results } has_more = True while has_more: results = service.data().ga().get(**params).execute() #do something with results params['start_index'] = int(params['start_index']) + int(params['max_results']) has_more = results.get('nextLink') 
+5
source

Why can't we just do:

 params = {'ids': profile_id, 'start_date': start_date, 'end_date': end_date, 'metrics': metrics, 'dimensions': dimensions, 'sort': sort, 'start_index': 1, 'max_results': 1} dummy_call = service.data().ga().get(**params).execute() # just to find out the totalResults number params['max_results'] = dummy_call[u'totalResults'] # replace max_result with the totalResults number all_rows = service.data().ga().get(**params).execute()[u'rows'] 

???

0
source

All Articles