Google 报告 API 过滤器同时包含 'and' 和 'or' 运算符

Google Reporting API filter contains both 'and' and 'or' operators

所以我正在尝试使用 google 分析 API v4 将关于 Google 分析的自定义报告转换为 python。在此报告中,过滤器如下:

(cond1 OR cond2 OR cond3 .. ) AND (condn OR condX ..)

目前我正在使用 'dimensionFilterClauses',但这只允许将过滤器与一个运算符或另一个运算符连接:

"dimensionFilterClauses": [
        {
          "operator": 'AND',
          "filters": [
            {
              "dimensionName": "ga:browser",
              "operator": "EXACT",
              "expressions": ["Chrome"]
            }
          ]
        } 

编辑:

我做了更多的挖掘,结果发现我需要使用分段。但我也遇到了另一个问题,因为它们只允许 'OR' 过滤器连接。

到目前为止,这就是我想出的(这是样本中的代码),当我使用两个不同的段时,最终结果是这两个段可以用一个额外的列来区分。如何在同一细分中使用不同的过滤器?

"segments":[
  {
    "dynamicSegment":
    {
      "name":"Safari",
      "sessionSegment":
      {
        "segmentFilters":[
        {
          "simpleSegment":
          {
            "orFiltersForSegment":[
            {
              "segmentFilterClauses":[
              {
                "dimensionFilter":
                {
                  "dimensionName":"ga:browser",
                  "expressions":["Safari"],
                  "operator":"EXACT"
                }
              }]
            }]
          }
        }]
      }
    }
  },
  {
    "dynamicSegment":
    {
      "name":"United States",
      "sessionSegment":
      {
        "segmentFilters":[
        {
          "simpleSegment":
          {
            "orFiltersForSegment":[
            {
              "segmentFilterClauses":[
              {
                "dimensionFilter":
                {
                  "dimensionName":"ga:country",
                  "expressions":["United States"],
                  "operator":"EXACT"
                }
              }]
            }]
          }
        }]
      }
    }
  }]

我终于明白了。这是来自 API 文档:

segmentFilters[]: A segment is defined by a set of segment filters which are combined together with a logical AND operation

我所要做的就是通过在 segmentFilters 列表中添加 OR 过滤器将两个段合并为一个段:

segments = [
{
    "dynamicSegment":
    {
        "name":"Combined",
        "sessionSegment":
        {
            "segmentFilters":[
            {
                "simpleSegment":
                {
                    "orFiltersForSegment":[
                    {
                        "segmentFilterClauses":[
                        {
                            "dimensionFilter":
                            {
                                "dimensionName":"ga:browser",
                                "expressions":["whatever you need 1"],
                                "operator":"EXACT"
                            }
                        }, {    # OR 
                            "dimensionFilter":
                            {
                                "dimensionName":"ga:browser",
                                "expressions":[" or whatever you need 2"],
                                "operator":"EXACT"
                            }
                        }]
                    }, {   # AND 
                    "segmentFilters":[
                    {
                    "simpleSegment":
                    {
                        "orFiltersForSegment":[
                        {
                            "segmentFilterClauses":[
                            {
                                "dimensionFilter":
                                {
                                    "dimensionName":"ga:country",
                                    "expressions":["whatever you need as need 3"],
                                    "operator":"EXACT"
                                }, {   # OR 
                                "dimensionFilter":
                                {
                                    "dimensionName":"ga:country",
                                    "expressions":["or whatever you need as need 4"],
                                    "operator":"EXACT"
                                }
                            }]
                        }]
                    }
                }]]
            }
        }]
    }
}
}]