【问题标题】:Google Analytics API returned User does not have sufficient permissions for this profileGoogle Analytics API 返回 用户对此配置文件没有足够的权限
【发布时间】:2021-01-20 20:21:57
【问题描述】:

我已经按照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()

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

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

【问题讨论】:

  • 该错误表示您没有授予服务帐户访问 Google Analytics 帐户的权限,请转到管理部分并将其添加到帐户级别下。如果您没有视图,那么我建议您创建一个。据我所知,您不能再创建应用程序了。应用分析通常使用 Firebase 分析。
  • 我已将我的服务帐户访问权限添加到 Google Analytics(管理员 - 在帐户用户管理和资产用户管理下)。您最近使用过 Goggle Analytics 吗? Firebase Analytics 目前与 Google Analytics 相同。我将我的应用程序连接到 fireabse,然后将其集成到 google 或 firebase 分析。如何创建视图?
  • console.firebase.google.com/u/0/project/d analytics.google.com/analytics/web
  • 什么意思>>据我所知你不能再只创建应用程序了?
  • 谷歌分析报告 API 让您可以访问存储在 analytics.google.com 上的数据。我不知道有任何 api 可以让您访问存储在 firebase.google.com 中的数据。我认为您正在查看错误的 api 和错误的网站,这就是您找不到数据的原因。

标签: google-analytics google-analytics-api


【解决方案1】:

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. 选择“在 Google Analytics 中查看您的数据”选项。
  3. 在 Google Analytics 中,选择“管理员”和“属性设置”。
  4. 将显示一个数字“属性 ID”。数据 API v1 接受此 GA4 报告请求中的属性 ID。

App + Web 媒体资源最近重命名为 GA4 媒体资源。见博文:https://blog.google/products/marketingplatform/analytics/new_google_analytics/

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2021-12-03
    • 2018-08-08
    • 2021-07-09
    • 1970-01-01
    • 2021-07-10
    • 2015-11-02
    相关资源
    最近更新 更多