Requested 10 dimensions; only 9 are allowed on Google Analytics Core Reporting API4

Question:

I have been trying to call Google Analytics core reporting v4 to get basic traffic data, but I could never get default channel grouping/channel grouping from the API. However, the dimension is available according to their 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)     

Here is the error message I got:

HttpError: <HttpError 400 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Requested 10 dimensions; only 9 are allowed.">

Has anyone who got the same error been able to solve this?

Asked By: 高怡甄

||

Answers:

Requested 10 dimensions; only 9 are allowed.

means exactly that you are sending 10 dimensions you are only allowed to send 9 to an api call at a time. Remove one of the dimensions below. May i suggest removing {‘name’:’ga:year’} and {‘name’:’ga:month’} you are already requesting date you dont really need those.

'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'}],
Answered By: DaImTo