如何访问已在 google 分析中被授予权限的服务帐户的视图 ID?

How to access view ID for service account that has already been granted permission in google analytics?

我的任务是使用 python 从某个网站访问来自 google 分析的数据。 我已经创建了一个服务帐户。 我的上级(赋予此任务的人)已授予我的服务帐户读取和分析权限。 要提取数据,我需要视图 ID。 我以为我们可以从这里访问视图 ID: https://ga-dev-tools.web.app/account-explorer/

然而,帐户浏览器一直在不断加载。 对于已经具有读取和分析权限的服务帐户,我还能如何获取视图 ID 以及从何处获取?

使用服务帐户,您无法以交互方式(通过浏览器)登录 GA。

但是您可以在脚本或程序中使用管理 API 来列出您有权访问的所有帐户、属性和视图,as per the example given in the documemtation here:

# Note: This code assumes you have an authorized Analytics service object.
# See the View (Profile) Developer Guide for details.

# Example #1:
# Requests a list of views (profiles) for the authorized user.
try:
  profiles = analytics.management().profiles().list(
      accountId='12345',
      webPropertyId='UA-12345-1').execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

# Example #2:
# Retrieves views (profiles) for all properties of the user's account,
# using a wildcard '~all' as the webpropertyId.
profiles = analytics.management().profiles().list(accountId='12345',
                                                  webPropertyId='~all'
                                                 ).execute()

# Example #3:
# The results of the list method are stored in the profiles object.
# The following code shows how to iterate through them.
for profile in profiles.get('items', []):
  print 'Account ID                = %s' % profile.get('accountId')
  print 'Property ID           = %s' % profile.get('webPropertyId')
  print 'Internal Property ID  = %s' % profile.get('internalWebPropertyId')
  print 'View (Profile ID)         = %s' % profile.get('id')
  print 'View (Profile) Name       = %s' % profile.get('name')

  print 'Default Page = %s' %  profile.get('defaultPage')
  print 'Exclude Query Parameters        = %s' % profile.get(
      'excludeQueryParameters')
  print 'Site Search Category Parameters = %s' % profile.get(
      'siteSearchCategoryParameters')
  print 'Site Search Query Parameters    = %s' % profile.get(
      'siteSearchQueryParameters')

  print 'Currency = %s' % profile.get('currency')
  print 'Timezone = %s' % profile.get('timezone')
  print 'Created  = %s' % profile.get('created')
  print 'Updated  = %s' % profile.get('updated')
  print 'eCommerce Tracking = %s' % profile.get('eCommerceTracking')
  print 'Enhanced eCommerce Tracking = %s' % profile.get(
      'enhancedECommerceTracking')

API 仍然将“视图”称为配置文件,这是多年前向 Universal Analytics 过渡的遗留问题。显然这对不再有浏览量的 GA4 不起作用。