gpt4 book ai didi

python - 查询30天活跃用户的Google Analytics(分析)API

转载 作者:行者123 更新时间:2023-12-03 16:47:23 25 4
gpt4 key购买 nike

在python中查询Google Analytics(分析)时,我无法获取30天活跃用户的数据。我的代码如下。我的问题是我需要指定日期或日期作为维度。 “必须将ga:nthDay,ga:date或ga:day中的至少一个指定为维度才能查询该指标。”我只是不确定什么是正确的语法。

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的请求,它就可以正常工作。

最佳答案

这是一个简单的批处理案例,我引用了GA API V4 Dimensions&Metrics



body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '2018-01-11',
'endDate': '2018-01-18'}],
'metrics': [{'expression': 'ga:28dayUsers'}],
# 'metrics': [{'expression': 'ga:sessions'}]
'dimensions': [{'name': 'ga:date'}]
}]
}

关于python - 查询30天活跃用户的Google Analytics(分析)API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com