我正在运行Google Analytics API脚本,我想用它来导出会话数据。
现在我只是打印数据:
def get_segment_sessions(service, profile_id, segment_id):
return service.data().ga().get(
ids='ga:' + profile_id,
segment='gaid::' + segment_id,
start_date='7daysAgo',
end_date='yesterday',
metrics='ga:sessions',
dimensions='ga:date').execute()
def print_results(results):
# Print data nicely for the user.
if results:
# print 'View (Profile): %s' % results.get('profileInfo').get('profileName')
# print 'Date: %s' % results.get('date') + ' Total Sessions: %s' % results.get('rows')[0][0]
output = []
for header in results.get('columnHeaders'):
output.append('%30s' % header.get('name'))
print(''.join(output))
if results.get('rows', []):
for row in results.get('rows'):
output = []
for cell in row:
output.append('%30s' % cell)
print(''.join(output))
看起来像:
ga:date ga:sessions
20170414 2235
20170415 2203
20170416 2257
20170417 2159
20170418 1921
20170419 2095
20170420 764
如何在JSON中导出它以在S3 / Redshift中使用?