使用 Google Analytics API 获取每天的结果,而不是日期范围内的总计

Get a result for each day, instead of total in date range, with Google Analytics API

在执行 Google 分析 API 查询时:

res = service.data().ga().get(ids='ga:152373812', start_date='7daysAgo', end_date='today',
          metrics='ga:users', segment='gaid::7yMBa3f7RimTf2SFtRQaRh').execute()

pprint(res)

我得到此日期范围(7 天前到今天)的用户总数

...
u'rows': [[u'100']]
...

如何获取每天的结果? 例如类似于:

{'2018-07-06': 7,
 ...
 '2018-07-12': 17,
 '2018-07-13': 5}

报告全文如下:

{u'columnHeaders': [{u'columnType': u'METRIC',
                     u'dataType': u'INTEGER',
                     u'name': u'ga:users'}],
 u'containsSampledData': False,
 u'id': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:...&metrics=ga:users&segment=gaid::...&start-date=7daysAgo&end-date=today',
 u'itemsPerPage': 1000,
 u'kind': u'analytics#gaData',
 u'profileInfo': {u'accountId': u'...',
                  u'internalWebPropertyId': u'...',
                  u'profileId': u'...',
                  u'profileName': u'...',
                  u'tableId': u'ga:...',
                  u'webPropertyId': u'...'},
 u'query': {u'end-date': u'today',
            u'ids': u'ga:...',
            u'max-results': 1000,
            u'metrics': [u'ga:users'],
            u'segment': u'gaid::...',
            u'start-date': u'7daysAgo',
            u'start-index': 1},
 u'rows': [[u'100']],
 u'selfLink': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:...&metrics=ga:users&segment=gaid::...&start-date=7daysAgo&end-date=today',
 u'totalResults': 1,
 u'totalsForAllResults': {u'ga:users': u'100'}}

解决方法是添加一个ga:date维度:

res = service.data().ga().get(..., dimensions='ga:date').execute()

最终给出:

...
u'rows': [[u'20180620', u'9'],
          [u'20180621', u'10'],
          ...
          [u'20180630', u'7']]
...