googleanalytics API将数据导出到JSON,格式可以放入S3/Redshi

2024-05-04 20:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个googleanalytics 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))

看起来像:

^{pr2}$

如何将其导出为JSON格式以在S3/Redshift中使用?在


Tags: 数据inidforoutputgetdatedef