如何在 google analytics api v4 中获取视图(配置文件)列表

How to get a list of views (profiles) in google analytics api v4

我正在尝试将我的脚本从使用 google 分析 API 的版本 3 升级到版本 4。

在版本 3 中,我可以从 api 中获取帐户、属性和视图的列表(参见 API reference for version 3). However, the API reference for version 4 似乎没有显示相同的内容。

我现在如何获得这些列表?

TLDR:您可以像往常一样获取视图列表。

Analytics Reporting API V4 is a stand alone API for querying an Analytics View for data. There is not V4 management API, only the Analytics Management API V3。这两个 API 设计为一起使用。

要在 Python 中同时加载 V3 和 V4 库:

from apiclient.discovery import build;

analytics = build('analytics', 'v3', http=http)
analyticsReporting = build('analyticsreporting','v4', http=http)

列出某个用户的所有观点的最佳方式是调用 accountsummaries.list() -- 有关详细信息,请参阅 method reference docs

account_summaries = analytics.management().accountSummaries().list().execute()

解析响应得到感兴趣的viewId,调用V4API:

response = analyticsreporting.reports().batchGet(
  body={
    "reportRequests":[
    {
      "viewId": viewId,
      "dateRanges":[
        {
          "startDate":"2015-06-15",
          "endDate":"2015-06-30"
        }],
      "metrics":[
        {
          "expression":"ga:sessions"
        }],
      "dimensions": [
        {
          "name":"ga:browser"
        }]
      }]
  }
).execute()