gpt4 book ai didi

python - 使用 userActivity.search 使用 python 从谷歌分析中获取用户数据

转载 作者:行者123 更新时间:2023-12-04 04:21:17 28 4
gpt4 key购买 nike

我正在尝试使用以下 google python api 从 google analytics 获取用户数据:

https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

在所示示例中,“get_report”函数如下所示:

def get_report(analytics):
"""Queries the Analytics Reporting API V4.

Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()

此语法运行良好,但为了获取用户数据,我认为我必须遵守以下手册:

https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/userActivity/search

为了让它更清晰,我想从这里运行示例:

https://developers.google.com/analytics/devguides/reporting/core/v4/user-reporting

这个例子是这样的:

{
"viewId": "9999999",
"user": {
"type": "CLIENT_ID",
"userId": "1034600000.76425000000"
},
"dateRange": {
"startDate": "2018-01-01",
"endDate": "2018-12-31",
}
}

但是这个例子并没有说明如何从上面定义函数“get_report”。据我了解,“get_reports”中使用的参数“analysis”有一个名为“userActivity”的方法或类。但是根据上面的第二个链接,它还应该有一个名为“搜索”的方法,但它没有!那么如何获取/访问“搜索”方法?是否必须在初始化类“userActivity”时进行初始化?

目前我的语法是这样的:

def get_report(analytics):
"""Queries the Analytics Reporting API V4.

Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.userActivity().search(
{
"viewId": VIEW_ID,
"user": {
"type": "CLIENT_ID",
"userId": "310383817.1547668323"
},
"dateRange": {
"startDate": "2019-01-30",
"endDate": "2019-02-01",
}
}
).execute()

但是这种语法有效!错误消息说:

method() 接受 1 个位置参数,但给出了 2 个

感谢您的帮助!

最佳答案

我刚刚找到了答案......
您只需将搜索方法的参数放入名为 body 的变量中。它应该看起来像这样:

def get_report(analytics):
"""Queries the Analytics Reporting API V4.

Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.userActivity().search(
body = {
"viewId": VIEW_ID,
"user": {
"type": "CLIENT_ID",
"userId": "310383817.1547668323"
},
"dateRange": {
"startDate": "2019-01-30",
"endDate": "2019-02-01",
}
}
).execute()

关于python - 使用 userActivity.search 使用 python 从谷歌分析中获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249688/

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