使用Google Analytics(分析)API每天获取结果,而不是日期范围内的总计 [英] Get a result for each day, instead of total in date range, with Google Analytics API

查看:85
本文介绍了使用Google Analytics(分析)API每天获取结果,而不是日期范围内的总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行Google Analytics(分析)API查询时:

When doing a Google Analytics API query:

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

pprint(res)

我获得了该日期范围(7天前到今天)的用户总金额:

I get the total sum of users for this date range (7 days ago to today):

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

如何获取每天的结果?例如像这样:

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


这是完整的报告:


Here is the full report:

{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'}}

推荐答案

解决方案是添加

The solution is to add a ga:date dimension:

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

最终会给出:

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

这篇关于使用Google Analytics(分析)API每天获取结果,而不是日期范围内的总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆