要求的10个尺寸; Google Analytics Core Reporting API4仅允许9

时间:2019-02-14 06:02:45

标签: python google-api google-analytics-api google-api-python-client

我一直试图调用Google Analytics(分析)核心报告v4来获取基本流量数据,但是我从无法从api获取默认渠道分组/渠道分组。但是,可以根据其metric and dimension explorer使用尺寸。

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd 

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = r"event-source.json"
VIEW_ID = 'xxxxxxx'

def initialize_analyticsreporting():
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

    analytics = build('analyticsreporting', 'v4', credentials=credentials)
    return analytics


def get_report(analytics):

    return analytics.reports().batchGet(
        body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'pageSize': 10000,
          'dateRanges':  [{'startDate':'7daysago' , 'endDate': 'yesterday'}],
          'metrics': [{'expression': 'ga:adCost'}],
          'dimensions': [{'name':'ga:year'},{'name':'ga:month'},{'name':'ga:date'},{'name':'ga:channelGrouping'},
                         {'name':'ga:source'},{'name':'ga:medium'},{'name':'ga:campaign'},{'name':'ga:adGroup'},
                         {'name':'ga:keyword'},{'name':'ga:adContent'}],
          'orderBys': [{"fieldName": "ga:date","sortOrder": "ASCENDING"}]
        }]
      }
    ).execute()

def print_response(response):
    lst=[]
    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:
        dic={}
        dimensions = row.get('dimensions', [])
        dateRangeValues = row.get('metrics', [])

        for header, dimension in zip(dimensionHeaders, dimensions):
            dic[header] = dimension

        for i, values in enumerate(dateRangeValues):
            for metric, value in zip(metricHeaders, values.get('values')):
            #set int as int, float a float
                if ',' in value or ',' in value:
                    dic[metric.get('name')] = float(value)
                else:
                    dic[metric.get('name')] = int(value)

        lst.append(dic)

    df=pd.DataFrame(lst)
    return(df)     

这是我收到的错误消息:

  

HttpError:https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json返回“请求的10个维度;仅允许9个。“>

遇到相同错误的人都能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

  

要求的10个尺寸;只允许9个。

准确地表示您要发送10个维度,一次只能发送9个到api调用。删除以下尺寸之一。我可以建议删除{'name':'ga:year'}和{'name':'ga:month'},而您已经在请求您确实不需要的日期。

'dimensions': [{'name':'ga:year'},{'name':'ga:month'},{'name':'ga:date'},{'name':'ga:channelGrouping'},
                     {'name':'ga:source'},{'name':'ga:medium'},{'name':'ga:campaign'},{'name':'ga:adGroup'},
                     {'name':'ga:keyword'},{'name':'ga:adContent'}],