在python中将过滤器应用于Google AnalyticsAPI

时间:2016-07-12 20:45:02

标签: python google-analytics-v4

我是向Google分析编写查询的新手,我有兴趣在下面的方法中添加过滤器。具体来说,要过滤掉位置,但是在运营商字段中除了“确认”之外的任何时候我都会收到错误。对于dimensionFilterClauses。

是否有该字段的有效运算符列表? 'NOT_EXACT','NOT_EQUALS',符号也没有!=,<>,= / =。他们似乎错了。具体的错误是: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"">所以似乎必须有一个可接受的运算符的枚举,我只是找不到它。

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()

2 个答案:

答案 0 :(得分:1)

也许这对您有帮助

     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()

答案 1 :(得分:0)

看来,根据这里: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#dimensionfilterclause

...导致这里: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#filterlogicaloperator

它应该是ANDOR。不确定为什么EXACT正在经历 - 似乎只是在API文档中没有正式的东西。如果未指定,则将其视为“或”。