【问题标题】:Google Analytics core reporting API, fetch and dumpGoogle Analytics 核心报告 API,获取和转储
【发布时间】:2020-03-20 17:56:41
【问题描述】:

我正在尝试使用 python 在 lambda 函数中编写一个 google 分析连接器,以获取和存储 Google Core Reporting API 提供的所有指标和维度值。截至目前,我可以从 api 查询各个指标/维度值,但不确定如何将所有数据转储为 json,因为它只返回我要求的值。

"""Hello Analytics Reporting API V4."""

import argparse

from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.
VIEW_ID = 'xxxxxxx'


def initialize_analyticsreporting():
  """Initializes the analyticsreporting service object.

  Returns:
    analytics an authorized analyticsreporting service object.
  """
  # Parse command-line arguments.
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args([])

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(
      CLIENT_SECRETS_PATH, scope=SCOPES,
      message=tools.message_if_missing(CLIENT_SECRETS_PATH))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage('analyticsreporting.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=httplib2.Http())

  # Build the service object.
  analytics = build('analyticsreporting', 'v4', http=http)

  return analytics

def get_report(analytics):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  return analytics.reports().batchGet(
      body={
        "reportRequests": [
        {
          "viewId": VIEW_ID,
          "metrics": []

        }]
      }
  ).execute()


def print_response(response):
  """Parses and prints the 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', [])
    rows = report.get('data', {}).get('rows', [])

    for row in 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()

用于获取数据的现有代码 sn-p 及其产生的当前输出

Date range (0)
ga:visits: 6

我试图获取 Google Analytics 提供的所有 500 多个指标,而不是这个。

【问题讨论】:

  • 关于这一点:“不确定如何将所有数据转储为 json,因为它只返回我要求的值”您能否编辑您的问题以包含当前输出以及示例想要的输出?

标签: python api google-analytics aws-lambda


【解决方案1】:

截至目前,我可以查询各个指标/维度值 来自api 但不确定如何将所有数据转储为json 返回我要求的值

是的,这就是 API 的工作原理:您需要查询特定的维度和指标,而您只会得到您所要求的。

我正在尝试获取 Google Analytics(分析)提供的所有 500 多个指标。

开箱即用您不能:GA API 限制您一次查询 7 个维度 + 10 个指标(请参阅下面的 v3 文档,同样适用于 v4):

https://developers.google.com/analytics/devguides/reporting/core/v3/reference#largeDataResults
“在任何一个 API 请求中最多允许 7 个维度和 10 个指标”

解决方法是使用自定义维度作为标识符,例如用户 ID + 会话 ID,您可以通过它唯一标识每个会话,从而运行多个 API 查询以收集更多维度/指标,以及然后根据该自定义维度重新聚合数据。

这里有一个更详细解释的库:
https://github.com/aiqui/ga-download

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 2018-07-04
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多