python - 使用 GOOGLE Analytics API - 指标/维度

标签 python google-analytics google-analytics-api google-analytics-firebase

目前,我正在尝试使用 Google Analytics API 并没有成功,因为总是出现错误 - “选定的维度和指标不能一起查询。”

作为一个假设应该很容易修复和检查指标和维度,但即使我使用 DEVELOPMENT TOOL它仍然显示此错误。

python 代码:

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


SCOPES = ['https://www.googleapis.com/auth/analytics']
KEY_FILE_LOCATION = 'key.json'
VIEW_ID = 'REPORT_ID'



def initialize_analyticsreporting():

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

  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': 
          [
            {'startDate': '7daysAgo', 'endDate': 'today'}
          ],

          'metrics': [
            {'expression': 'ga:transactions'},
          ],
          'dimensions': 
          [
            {'name': 'ga:productName'}
          ]
        }]
      }
  ).execute()


def main():
  analytics = initialize_analyticsreporting()
  response = get_report(analytics)
  with open('analytics_data.json', "w+") as op:
    op.write(json.dumps(response))  

if __name__ == '__main__':
  main()

例如,如果使用基于指标/维度的 Google Python Example Code
      'metrics': [
        {'expression': 'sessions'},
      ],
      'dimensions': 
      [
        {'name': 'ga:country'}
      ]

它返回适当的结果,但如果我需要 ga:transactionsga:productName它返回上面的错误。

想法:
可以假设即使使用正确的 View_ID(报告 ID)它仍然无法连接到适当的报告?

有人可以帮忙吗?

最佳答案

我认为您收到的错误消息是正确的。您只是试图获得不允许的指标和维度组合。您可以在 Google Analytics Query Explorer 中尝试,你会得到同样的结果。

使用带有工作 View ID 的查询设置:
enter image description here

如果您想查看至少包含您的产品的交易数量,我建议使用 Unique Purchase 作为指标(ga:uniquePurchases)。虽然我的测试 View 没有电子商务数据,但您可以看到,查询已成功运行:
enter image description here

您可以使用 Query Explorer setting link 在您自己的 View 中检查此查询。 .

关于python - 使用 GOOGLE Analytics API - 指标/维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51609607/

相关文章:

Python "FileHandler"到 Azure Blob 存储 - 没有这样的事情吗

python - 如何添加列表的特定部分?

javascript - 谷歌分析页面跟踪在 angularjs 应用程序中无法正常工作

php - 什么是好的PHP分析脚本?

google-analytics - 嘎 :landingPagePath without request

python - Python 2.4 中的相对增量

python - 如何获取文件夹中最大的文件,更改它们的名称并保存到不同的文件夹

android - Google Analytics - Android 应用跳出率

python - invalid_client : The OAuth client was not found. Python

android - Analytics v4 未捕获异常和混淆器