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

标签 python google-apis-explorer

我正在尝试使用以下 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/

相关文章:

python - 从 python 运行 shell 脚本

api - 如何获取博客 API 帖子/搜索的下一页结果?

google-api - API 发现服务仅在 BigQuery 中返回错误

python - 将html数据解析成python列表进行操作

python - 计算数据框中两列中作为相反对存在的唯一值的数量?

google-compute-engine - Google Cloud Platform - 如何按标签过滤instance.list?

javascript - 使用 Google Drive Rest API 更新文件名

youtube-api - YouTube 数据 API 'youtube#channel' 查询 : the 'brandingSettings.image' is no longer available as of 11-11-2020?

c++ - C++/Qt 应用程序中的 PySide DLL 加载冲突

python - 如何动态地将数据从列表添加到数据库(PostgreSQL)