Marco Fumagalli
Marco Fumagalli

Reputation: 2477

Filter event category from Google Analytics

I'm trying to export data from GA into our enterprises DB using python. I need to sent a request to GA API with information I want to extract. This is the reqyest body:

body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': queryDate.strftime("%Y-%m-%d"), 'endDate': queryDate.strftime("%Y-%m-%d")}],
          'metrics': [{'expression': 'ga:totalEvents',
                       'expression':'ga:uniqueEvents'}

                      ],
          'dimensions': [{'name': 'ga:date'},{'name': 'ga:eventCategory'}, # event
                         {'name':'ga:dimension1'}, #userid
                         {'name':'ga:dimension3'}, #sessionid
                         {'name': 'ga:dimension4'},#timestamp
                         {'name':'ga:dimension7'}, #country
                         {'name': 'ga:eventAction'} #azione

          ],

        'dimension_filter_clauses': [
      {
          'operator': 'AND',
          'filters': [
              {
                  'dimensionName': 'ga:eventCategory',
                  'not': False,
                  "operator": 'enum(IN_LIST)',
                  'expressions':[

                    "Category1",
                    "Category2"

                  ],
                  'caseSensitive': False
              }],


            'filters': [
            {
     'dimensionName': 'ga:dimension7',
     'not':True,
     'expressions':['ES'],
     'caseSensitive': False

            }
            ]


      }

          ]

      ,
    'pageToken': offset,
    'pageSize': 5


      }]}

I want to filter only for category specified under IN_LIST operators but it seems that the filter is not working... What I'm doing wrong?

UPDATE

The problem seems to be the second filter clause

        'filters': [
        {
 'dimensionName': 'ga:dimension7',
 'not':True,
 'expressions':['ES'],
 'caseSensitive': False

        }

Upvotes: 1

Views: 1344

Answers (1)

Marco Fumagalli
Marco Fumagalli

Reputation: 2477

I'm gonna answer myself: it wasn't clear from the documentation how to specify multiple filters. This is working:

  'dimensionFilterClauses': [
  {
      'operator': 'AND',
      'filters': [
          {
              'dimensionName': 'ga:eventCategory',
              'not': False ,
              'operator': 'IN_LIST',
              'expressions':[

                  "Category1"
              ],
              'caseSensitive': False
          },
          {
              'dimensionName': 'ga:deviceCategory',
              'not': False,
              'expressions': [
                  deviceQuery
              ],
              'caseSensitive': False
          }]
  }

      ]

Upvotes: 2

Related Questions