【发布时间】: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