返回的Google Analytics分析API用户对此个人资料没有足够的权限

问题描述

我已经按照以下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的第一步很困惑。

enter image description here

解决方法

Google Analytics Data API v1是GA4属性的新API。请参见devsite: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属性没有视图,因此无法在这些属性中创建视图。