Python 查询30天活动用户的Google Analytics API
当我用python查询Google Analytics时,我很难获取30天活跃用户的数据。我的代码如下。我的问题是我需要指定一个日期作为维度。“必须至少将ga:nthDay、ga:date或ga:day中的一个指定为查询此度量的维度。”我只是不确定此度量的正确语法是什么Python 查询30天活动用户的Google Analytics API,python,api,google-analytics,active-users,Python,Api,Google Analytics,Active Users,当我用python查询Google Analytics时,我很难获取30天活跃用户的数据。我的代码如下。我的问题是我需要指定一个日期作为维度。“必须至少将ga:nthDay、ga:date或ga:day中的一个指定为查询此度量的维度。”我只是不确定此度量的正确语法是什么 from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build import ht
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
import httplib2
#create service credentials
#this is where you'll need your json key
#replace "keys/key.json" with the path to your own json key
key_file_location = 'maypath'
credentials =
ServiceAccountCredentials.from_json_keyfile_name(key_file_location,
['https://www.googleapis.com/auth/analytics.readonly'])
# create a service object you'll later on to create reports
http = credentials.authorize(httplib2.Http())
service = build('analytics', 'v4', http=http,
discoveryServiceUrl=
('https://analyticsreporting.googleapis.com/$discovery/rest'))
response = service.reports().batchGet(
body={
'reportRequests': [
{
'viewId': 'ga:thisismyid',
'dateRanges': [{'startDate': '2017-10-01', 'endDate': '2017-10-01'}],
'metrics': [{'expression': 'ga:sessions'},
{'expression': 'ga:bounceRate'},
{'expression': 'ga:avgSessionDuration'},
{'expression': 'ga:users'},
{'expression': 'ga:newUsers'},
{'expression': 'ga:sessionsPerUser'},
{'expression': 'ga:30dayUsers'}],
'dimensions': [{"name": "ga:pagePath"},
{"name": "ga:city"}],
'orderBys': [{"fieldName": "ga:sessions", "sortOrder": "DESCENDING"}],
'pageSize': 10
}]
}
).execute()
因此,在这个例子中,我要求在一个日期上提供一系列指标,包括30天的活跃用户。实际上,我遇到了这个错误:“所选维度和度量不能一起查询”,这大概是因为我没有将ga:date或ga:day指定为我的维度之一。我尝试过几种方法,但我的语法必须关闭
那么,我如何将ga:day指定为一个维度,以获取在我提取数据的日期结束的30天用户数?注意,如果我在上面的代码中简单地去掉了对ga:30dayUsers的请求,它就可以正常工作 这是一个关于批处理的简单案例,我参考了
正文={
“报告请求”:[
{
“视图ID”:视图ID,
“日期范围”:[{'startDate':'2018-01-11',
“结束日期”:“2018-01-18”},
'metrics':[{'expression':'ga:28dayUsers'}],
#'metrics':[{'expression':'ga:sessions'}]
'dimensions':[{'name':'ga:date'}]
}]
}