google-analytics-api - 分析报告 V4 : Can I combine a DynamicSegment and a segmentId as logical AND?

标签 google-analytics-api google-analytics-firebase

我正在尝试将一些查询从 Core Reporting V3 迁移到 V4(具体来说是 PHP 中的 AnalyticsReporting V4)。在一种情况下,我想将自定义分割与“预定义”分割之一结合起来。

预期结果:

我收到一个响应,其中两个段都应用于该响应(逻辑与)

实际结果:

我收到一条回复,其中每行都针对每个分割单独列出。

我的问题

如何应用两个段来为每个结果接收一行?在本例中 - 获取通过平板电脑或台式机访问并且进行了三个以上 session 的所有用户。

在 V3 中,我通过组合多个 user::condition:somethingSomething 来做到这一点。

我想一种方法是首先获取我需要的预定义段的段定义,然后使用它来构建 SegmentDimensionFilter - 但最好避免这种情况。

这是我的(测试)查询,segmentId: 'gaid::-15' 是“平板电脑和桌面流量”。在我的真实查询中,我使用的是 SequenceSegment,但这里的响应是相同的:

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

这是响应,请注意每个分割如何作为其自己的维度单独列出:

"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

如果有任何帮助,我们将不胜感激!如果我需要澄清一些事情,请告诉我。

最佳答案

多个片段

正如您的查询结果所证明的那样,Analytics Reporting API V4 ReportRequest 可以具有多个分段,并且每个分段都显示在结果中的 ga:segment 维度中。

逻辑与段过滤器

回答您的问题不,您不能将分段 ID 与您自己的子句或/与组合在一起,因为给定的分段 ID 可以由多个单独的过滤器子句组成。

事实上,您请求的段 ID 有两个段过滤子句:

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

但一切并没有失去

正如您猜测的那样,您将需要创建自己的一组分段过滤器:

{
  "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

分段 ID 向后兼容

除了能够对过滤器进行逻辑ANDOR分段之外,Analytics Reporting API V4向后兼容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"
        }
      ]
    }
  ]
}

关于google-analytics-api - 分析报告 V4 : Can I combine a DynamicSegment and a segmentId as logical AND?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37331799/

相关文章:

google-analytics - 谷歌分析。同一应用程序的不同平台。跟踪代码相同还是不同?

google-analytics - 无法从 Google Measurement Protocol (Google Analytics v4) 接收事件

android - 如何从用户点击推荐链接并从Playstore打开预安装应用程序的 Intent 中获取 "referrer"

python - 在 python 中将过滤器应用于 Google Analytics API

javascript - "Uncaught SyntaxError: Unexpected token <"尝试使用 Google 的核心报告 API 时

r - R:Validate()中的错误:所有GA查询必须具有标题参数

google-analytics - GoogleAnalytics 自己的数据

php - 获取分析 View (配置文件)的目标列表

ios - Google IOS 分析 V3 未在仪表板上注册

google-analytics - 移动 Google Analytics(分析)虚拟屏幕 View