Search code examples
python-3.xgoogle-analytics-apigoogle-analytics-firebase

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


so I'm trying to a convert a custom report on Google analytics to python using the google analytics API v4. In this report the filter is as follow:

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

Currently I am using the 'dimensionFilterClauses', but this only allows concatenating the filters with one operator or the other:

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

EDIT:

I did some more digging and it turned out i need to use segments. But I hit another snag with them as well, as they only allow for 'OR' filter concatenation.

So far, this is what I came up with (this is the code from samples), when I use two different segments, the end result is the two segments which can be differentiated with an extra column. How do I use different filters in the same segment?

"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"
                }
              }]
            }]
          }
        }]
      }
    }
  }]

Solution

  • I finally figured it out. This is from the API documentation:

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

    All I have to do is combine the two segments into just one segment by adding the OR filters in the segmentFilters list:

    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"
                                    }
                                }]
                            }]
                        }
                    }]]
                }
            }]
        }
    }
    }]