0

I am trying to pull Google analytics data using python google-api-client. But I need to mention the list of dimensions and metrics in the body of the request 'dimensions':[{'name' : 'ga:date'}]. But is there any way to get the full list of dimensions and metrics associated with a view ID. Can i get that list by API call?

sample_request = {
    'viewId': 'xxxxxx',
    'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
    'metrics' : [{'expression' : 'ga:users'}],
    'dimensions':[{'name' : 'ga:date'}]
    }

response = api_client.reports().batchGet(
                      body={
                    'reportRequests': sample_request
                            }).execute()

2 Answers 2

0

Here you can find the list of all the metrics and dimensions that can be queried for any view in Analytics: https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

2
  • So does any given view have all the dimensions and metrics mentioned in the explorer?.
    – Nithin Sai
    Nov 25, 2019 at 9:05
  • All metrics and dimensions can be queried. If they have no values ​​and are queried they will not return a result. If I query ga:transactionsPerUser (transactions per user) in a non-ecommerce view I will get 0. Try this tool: ga-dev-tools.appspot.com/query-explorer Nov 25, 2019 at 23:31
0

There is an google analytics metadata api which provides an up-to-date list of all metrics and dimensions available in GA, if you really require a programmatic response.

Here's my example (using requests- there is a separate python metadata api that you can use, but as it doesn't require api keys in my experience it's easier to use requests- the reference for this & also how to use the python client library is here):

import requests

resp = requests.get("https://www.googleapis.com/analytics/v3/metadata/ga/columns?pp=1")

print(resp.json())

Unfortunately, the metadata API is limited in that you can't use it to identify which metrics and dimensions can be queried together in an Analytics API call. As mentioned by Michele you can use the dimensions and metrics explorer to explore which combinations of metrics and dimensions are possible to query by clicking the checkbox for the metric you are interested in, and seeing if this greys out the dimension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.