Search code examples
google-analyticsgoogle-apigoogle-analytics-api

Is it possible to manipulate the dimension name within the Google Analytics API response in the query itself


Here is a sample query that I created to fetch Google Analytics data:

response = service.reports().batchGet(
body={
    'reportRequests': [
        {
            'viewId': 'xxxx',  
            'dateRanges': [{'startDate': '2021-01-14', 'endDate': '2021-01-15'}],
            'metrics': [
                {'expression': 'ga:pageViews'},
                {'expression': 'ga:sessions'},
                {'expression': 'ga:itemRevenue'},
                {'expression': 'ga:hits'},
                {'expression': 'ga:sessionDuration'},
                ],
            # Get Pages
            'dimensions': [
                {"name": "ga:clientId"},
                {"name": "ga:pagePath"},
                {"name": "ga:dateHourMinute"},
                {"name": "ga:shoppingStage"},
                {"name": "ga:source"},
                {"name": "ga:campaign"},
                ],
            # Filter by condition
            "filtersExpression": "ga:clientId==yyyy.zzzz",
            'orderBys': [{"fieldName": "ga:dateHourMinute", "sortOrder": "DESCENDING"}],
            'pageSize': 500
        }]
    }
    ).execute()

Sample response:

{'dimensions': ['yyyy.zzzz',
                '/products/pants-green?variant=456456456',
                '202101142347',
                'ALL_VISITS',
                'newsletter',
                '2021_01-pre-sale',
                '282'],
 'metrics': [{'values': ['0',
                         '0',
                         '0.0',
                         '1',
                         '0.0']}]},

Is it possible to define alternate naming for the dimensions in the response within the query itself, e.g.

  • strip the variant part from the page path with regex,
  • change the wording for "ga:shoppingStage" from ALL_VISITS to something else?

Or is this something which needs to be done in post-processing?


Solution

  • The dimensions and metrics are standard within Google analytics. The response returned to you from the API is simply the name of the dimensions and metrics from the API.

    Even if you have your own custom dimensiosn and metrics set up the API is still just going to return it with the name ga:dimensionXX

    If you want to change the names your going to have to do that locally after the data is returned to you.