简体   繁体   中英

applying filters to Google Analytics API in python

I am brand new to writing queries to Google analytics and am interested in adding a filter to the method below. Specifically, to filter out locations, but I keep getting an error anytime there is something other than 'EXACT' in the operator field. for the dimensionFilterClauses.

is there a list of valid operators for that field? 'NOT_ EXACT', 'NOT_EQUALS', nor do the symbols !=, <>, =/=. they seems to error out. the specific error is: 400 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Invalid value at 'report_requests[0].dimension_filter_clauses[0].filters[0].operator' (TYPE_ENUM), "NOT_EQUAL""> so it seems like there must be an enumeration of acceptable operators, I just can't find it.

def get_report(analytics):
'''Use the Analytics Service Object to query the Analytics Reporting API V4.'''
return analytics.reports().batchGet(
  body={
    'reportRequests': [
    {
      'viewId': VIEW_ID,
      'dateRanges': [{'startDate': '2016-07-01', 'endDate': 'today'}],
      'metrics': [{'expression': 'ga:pageviews'}],
      'dimensions': [{'name': 'ga:country'}, {'name': 'ga:city'}],
      'metricFilterClauses': [{
      'filters': [{
          "metricName": "ga:pageviews",
          "operator": "GREATER_THAN",
          "comparisonValue": "1000"
      }]
      }],
      'dimensionFilterClauses': [
        {
          'filters': [
            {
              "dimensionName": "ga:country",
              "operator": "EXACT",
              "expressions": ["United States"]
            }
          ]
        }
      ]
    }]
  }
).execute()

It seems, according to here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#dimensionfilterclause

...which leads to here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#filterlogicaloperator

That it should be either AND or OR . Not sure why EXACT is going through--just something not officially in the API docs, it seems. If unspecified, it's treated as 'OR'.

Maybe this helps you

     def get_report(analytics):
'''Use the Analytics Service Object to query the Analytics Reporting API V4.'''
return analytics.reports().batchGet(
  body={
    'reportRequests': [
    {
      'viewId': VIEW_ID,
      'dateRanges': [{'startDate': '2019-07-01', 'endDate': '2020-04-01'}],
      'metrics': [{'expression': 'ga:pageviews'}],
      'dimensions': [{'name': 'ga:country'}, {'name': 'ga:city'}],
      "dimensionFilterClauses":[
        {
           "operator":"AND",
           "filters":[
              {
                 "dimensionName":"ga:pageviews",
                 "operator":"NUMERIC_GREATER_THAN",
                 "expressions":[
                    1000
                 ]
              },
              {
                "dimensionName":"ga:country",
                "operator":"EXACT",
                "expressions":[
                   "United States"
                ]
             }
           ]
        }
     ],
    }]
  }
).execute()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM