Google Analytics API 返回的用户对此配置文件没有足够的权限

Google Analytics API returned User does not have sufficient permissions for this profile

我有 运行 代码遵循以下说明 https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'path/to/keyfile'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_analyticsreporting():
  "Initializes an Analytics Reporting API V4 service object.

  Returns:
    An authorized Analytics Reporting API V4 service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


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()


def print_response(response):
  """Parses and prints the Analytics Reporting API V4 response.

  Args:
    response: An Analytics Reporting API V4 response.
  """
  for report in response.get('reports', []):
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

    for row in report.get('data', {}).get('rows', []):
      dimensions = row.get('dimensions', [])
      dateRangeValues = row.get('metrics', [])

      for header, dimension in zip(dimensionHeaders, dimensions):
        print(header + ': ', dimension)

      for i, values in enumerate(dateRangeValues):
        print('Date range:', str(i))
        for metricHeader, value in zip(metricHeaders, values.get('values')):
          print(metricHeader.get('name') + ':', value)


def main():
  analytics = initialize_analyticsreporting()
  response = get_report(analytics)
  print_response(response)

if __name__ == '__main__':
  main()

实际上,我在管理设置中找不到查看 ID。它只是帐户和 属性。是不是因为我的 属性 是 App + Web 而且还是 Beta?但是,当我尝试仅使用 App 创建新的 属性 时,它会自动更改为 App + Web。

在尝试 Google Analytics API 的第一步中很困惑。

Google Analytics Data API v1 是 GA4 媒体资源的新 API。查看开发站点:https://developers.google.com/analytics/trusted-testing/analytics-data。 Data API v1 在报告请求中接受数字 GA4 属性 标识符。

Firebase 展示项目。 Firebase 中显示的项目 ID 和编号不是 GA4 属性 标识符。要查找您的 GA4 属性 标识符:

  1. 导航到 Firebase Analytics 仪表板,
  2. select 选项“在 Google Analytics 中查看您的数据”。
  3. 在 Google Analytics 中,select“管理员”和“属性 设置”。
  4. 将显示数字“属性 ID”。数据 API v1 接受这个 GA4 属性 报告请求中的 ID。

应用 + 网络媒体资源最近已重命名为 GA4 媒体资源。查看博客 post:https://blog.google/products/marketingplatform/analytics/new_google_analytics/

Google Analytics API v3 (https://developers.google.com/analytics/devguides/reporting/core/v3) and Google Analytics Reporting API v4 (https://developers.google.com/analytics/devguides/reporting/core/v4) 接受报告请求中的数字视图标识符。

GA4 属性没有视图,并且无法在这些属性中创建视图。