lwcj
lwcj

Reputation: 49

How to call secondary dimension in Google Analytics API

Currently trying to get some reports using Google Analytics API using Python.

I am able to get a set of results right now for Users filtered on Campaign just fine using this.

dump.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
            
          'dateRanges': [
                          {
                              'startDate': '7daysAgo', 
                              'endDate': 'yesterday'
                          },
                         {
                             'startDate':  '14daysAgo',
                             'endDate': '8daysAgo'
                         }
                        ],
            
          'metrics': [{'expression': 'ga:users'},],
          'dimensions': [{'name': 'ga:campaign'}],
          'samplingLevel':'LARGE'
            
        }]
      }
  ).execute()

But I would like to get a report on a secondary dimension (i.e. the view I get when I select this option on the actual Google Analytics) and I'm stuck on adding the secondary dimension. I think ga:acquisitionSource is the name of the field I'm trying to add.

the view I want to get

Upvotes: 2

Views: 504

Answers (1)

Brett
Brett

Reputation: 1289

The Dimensions and Metrics explorer connects dimension names as displayed in the Google Analytics UI and their corresponding API name. The "Source" dimension's API name is "ga:source".

Requesting multiple dimensions is similar to the following:

dump.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
            
          'dateRanges': [
                          {
                              'startDate': '7daysAgo', 
                              'endDate': 'yesterday'
                          },
                         {
                             'startDate':  '14daysAgo',
                             'endDate': '8daysAgo'
                         }
                        ],
            
          'metrics': [{'expression': 'ga:users'},],
          'dimensions': [{'name': 'ga:campaign'},{'name': 'ga:source'}],
          'samplingLevel':'LARGE'
            
        }]
      }
  ).execute()

Upvotes: 2

Related Questions