Search code examples
google-analytics-apigoogle-analytics-firebase

AnalyticsReporting V4: Can I combine a DynamicSegment and a segmentId as logical AND?


I'm trying to migrate a few queries from Core Reporting V3 to V4 (AnalyticsReporting V4 in PHP to be specific). In one case I'd like to combine a custom segment with one of the "predefined" segments.

Expected result:

I receive a response where BOTH segments are applied to the response (logical AND)

Actual result:

I receive a response where each row is listed individually for each segment.

My question

How do I apply BOTH segments to receive ONE row per result? In this case - get all users that came via Tablet or Desktop and have had more than three sessions.

In V3 I did this by combining multiple user::condition:somethingSomething.

I guess one way would be to first get the segment definition of the predefined segment that I need, and then use it to build a SegmentDimensionFilter - but it would be nice to avoid that.

Here is my (test) query, with segmentId: 'gaid::-15' being "Tablet and Desktop Traffic". In my real query I'm using a SequenceSegment, but the response is the same here:

+"dateRanges": array:1 [▼
    0 => {#542 ▼
      +"endDate": "2016-05-18"
      +"startDate": "2016-04-18"
    }
  ]
  +"dimensions": array:1 [▼
    0 => {#545 ▼
      +"name": "ga:segment"
    }
  ]
  +"metrics": array:1 [▼
    0 => {#546 ▼
      +"expression": "ga:users"
    }
  ]
  +"segments": array:2 [▼
    0 => {#548 ▼
      +"dynamicSegment": {#549 ▼
        +"name": "AK: Loyal Visitors"
        +"userSegment": {#553 ▼
          +"segmentFilters": array:1 [▼
            0 => {#556 ▼
              +"simpleSegment": {#560 ▼
                +"orFiltersForSegment": {#563 ▼
                  +"segmentFilterClauses": array:1 [▼
                    0 => {#566 ▼
                      +"dimensionFilter": {#570 ▼
                        +"dimensionName": "ga:sessionCount"
                        +"expressions": "3"
                        +"operator": "NUMERIC_GREATER_THAN"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
    1 => {#543 ▼
      +"segmentId": "gaid::-15"
    }
  ]
  +"orderBys": array:1 [▼
    0 => {#547 ▼
      +"fieldName": "ga:users"
      +"sortOrder": "DESCENDING"
    }
  ]

And here is the response, notice how each segment is listed individually as its own dimension:

"columnHeader" => array:2 [▼
  "dimensions" => array:1 [▼
    0 => "ga:segment"
  ]
  "metricHeader" => array:1 [▼
    "metricHeaderEntries" => array:1 [▼
      0 => array:2 [▼
        "name" => "ga:users"
        "type" => "INTEGER"
      ]
    ]
  ]
]
"data" => array:6 [▼
  "rows" => array:2 [▼
    0 => array:2 [▼
      "dimensions" => array:1 [▼
        0 => "Tablet and Desktop Traffic"
      ]
      "metrics" => array:1 [▼
        0 => array:1 [▼
          "values" => array:1 [▼
            0 => "74309"
          ]
        ]
      ]
    ]
    1 => array:2 [▼
      "dimensions" => array:1 [▼
        0 => "AK: Loyal Visitors"
      ]
      "metrics" => array:1 [▼
        0 => array:1 [▼
          "values" => array:1 [▼
            0 => "10740"
          ]
        ]
      ]
    ]
  ]
  "totals" => array:1 [▼
    0 => array:1 [▼
      "values" => array:1 [▼
        0 => "85049"
      ]
    ]
  ]
  "rowCount" => 2

Would appreciate any help on this! Please let me know if I should clarify something.


Solution

  • Multiple Segments

    As your query results prove an Analytics Reporting API V4 ReportRequest can have multiple segments, and each segment shows up in the ga:segment dimension in the results.

    Logical AND segment filters

    To answer your question No you cannot OR/AND together a segment id with your own clauses, because a given segment Id can be composed up of several individual filter clauses.

    In fact the segment Id you requested has two segmentFilterClauses:

    gaid::-5 == sessions::condition::ga:deviceCategory==tablet,ga:deviceCategory==desktop
    

    But all is not lost

    As you surmised you will need to create your own set of segment Filters:

    {
      "reportRequests": 
      [
        {
          "viewId": "XXXX",
          "dimensions": 
          [
            {
              "name": "ga:segment"
            }
          ],
          "metrics": 
          [
            {
              "expression": "ga:users"
            }
          ],
          "segments": 
          [
            {
              "dynamicSegment": 
              {
                "userSegment": 
                {
                  "segmentFilters": 
                  [
                    {
                      "simpleSegment": 
                      {
                        "orFiltersForSegment": 
                        [
                          {
                            "segmentFilterClauses": 
                            [
                              {
                                "dimensionFilter": 
                                {
                                  "dimensionName": "ga:sessionCount",
                                  "operator": "NUMERIC_GREATER_THAN",
                                  "expressions": 
                                  ["3"
                                  ]
                                }
                              }
                            ]
                          }
                        ]
                      }
                    },
                    {
                      "simpleSegment": 
                      {
                        "orFiltersForSegment": 
                        [
                          {
                            "segmentFilterClauses": 
                            [
                              {
                                "dimensionFilter": 
                                {
                                  "dimensionName": "ga:deviceCategory",
                                  "expressions": 
                                  ["tablet","desktop"
                                  ],
                                  "operator": "IN_LIST"
                                }
                              }
                            ]
                          }
                        ]
                      }
                    }
                  ]
                },
                "name": "AK: Loyal mobile users"
              }
            }
          ]
        }
      ]
    }
    

    Checkout the above example in the API Explorer

    Segment Id is backwards compatible

    On top of being able to logically AND or OR segment Filters, the Analytics Reporting API V4 is backwards compatible with the V3 segment syntax:

    {
      "reportRequests": 
      [
        {
          "viewId": "XXXX",
          "dimensions": 
          [
            {
              "name": "ga:segment"
            }
          ],
          "metrics": 
          [
            {
              "expression": "ga:users"
            }
          ],
          "segments": 
          [
            {
              "segmentId": "users::condition::ga:deviceCategory==tablet,ga:deviceCategory==desktop,ga:sessionCount>3"
            }
          ]
        }
      ]
    }