title: How to add custom filters to Google Reporting API V4 Python
body: <p>I've currently got a Python script which outputs a chunk of data based on Date, City, Latitude &amp; Longitude.</p>
  
  <p>I've tried to implement ga:pagePath in order to filter this data and only show results on '/example'. However, it does not accept this request in the dimensions part of the code.</p>
  
  <p>How would I implement this as a custom filter in order to refine the data dump the API produces.</p>
  
  <p>Also, how would I modify the code at the bottom of the script which formats the data to include this new filter? </p>
  
  <p>Are there any documents out there which detail how to add filters into the api v4 via python, ideally in a similar fashion to how the Query Explorer adds in filters</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import httplib2 as lib2
  from oauth2client import client
  
  
  from googleapiclient.discovery import build as google_build
  
  
  import pandas
  import numpy
  import json
  from datetime import datetime, timedelta
  
  access_token = "ya29.GlsMBiqWA-CNAqOvGwDimYyzG9svM_kkjxTIyMEPWXDFrsYwOcJwWUWTWUrT9hD0prWp5UU4CI6ZHLeIyh2k-QsfToLWrmPNBmO9mg7OGAozGFlDGNniaZ6qiK7o"
  refresh_token = "1/H6jExOHvrGS6-TEbDG1EQDIy1_CGFq-UDbVqI_Du9P0"
  client_id = "61721534450-4rsaodi636nvb02pmg9k5ck4nkk5epgq.apps.googleusercontent.com"
  client_secret = "pORiELmRxdWotyDhllOAFvcO"
  
  
  token_uri = 'https://accounts.google.com/o/oauth2/token'
  
  
  token_expiry = datetime.now() - timedelta(days = 1)
  
  
  user_agent = 'my-user-agent/1.0'
  
  
  credentials = client.GoogleCredentials(access_token=access_token, refresh_token=refresh_token,
                                         client_id=client_id, client_secret=client_secret,
                                         token_uri=token_uri, token_expiry=token_expiry,
                                         user_agent=user_agent)
  
  
  http = lib2.Http()
  
  
  authorized = credentials.authorize(http)
  
  
  api_name = 'analyticsreporting'
  api_version = 'v4'
  
  
  api_client = google_build(serviceName=api_name, version=api_version, http=authorized)
  sample_request = {
        'viewId': '6690355',
        'dateRanges': {
            'startDate': datetime.strftime(datetime.now() - timedelta(days = 30),'%Y-%m-%d'),
            'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
        },
        'dimensions': [{'name': 'ga:date'},{'name': 'ga:city'}, {'name': 'ga:latitude'},{'name': 'ga:longitude'},{'name': 'ga:pagePath'}],
        'metrics': [{'expression': 'ga:sessions'}]
      }
  
  response = api_client.reports().batchGet(
        body={
          'reportRequests': sample_request
        }).execute()
  print(response)
  
  def prase_response(report):
  
      """Parses and prints the Analytics Reporting API V4 response"""
  
      result_list = []
  
  
      data_csv = []
      data_csv2 = []
  
  
      header_row = []
  
  
      columnHeader = report.get('columnHeader', {})
      metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
      dimensionHeaders = columnHeader.get('dimensions', [])
  
  
      for dheader in dimensionHeaders:
          header_row.append(dheader)
      for mheader in metricHeaders:
          header_row.append(mheader['name'])
  
  
      rows = report.get('data', {}).get('rows', [])
      for row in rows:
          row_temp = []
          dimensions = row.get('dimensions', [])
          metrics = row.get('metrics', [])
          for d in dimensions:
              row_temp.append(d)
          for m in metrics[0]['values']:
              row_temp.append(m)
              data_csv.append(row_temp)
  
  
          if len(metrics) == 2:
              row_temp2 = []
              for d in dimensions:
                  row_temp2.append(d)
              for m in metrics[1]['values']:
                  row_temp2.append(m)
              data_csv2.append(row_temp2)
  
  
      result_df = pandas.DataFrame(data_csv, columns=header_row)
      result_list.append(result_df)
      if data_csv2 != []:
          result_list.append(pandas.DataFrame(data_csv2, columns=header_row))
  
      return result_list
  
  response_data = response.get('reports', [])[0]
  print(prase_response(response_data)[0])
  
  prase_response(response_data)[0].to_csv('example.csv')
  </code></pre>