Is it possible to apply both 'and' and 'or' filters in a single request with Google Analytics run report API?

huangapple go评论75阅读模式
英文:

Is it possible to apply both 'and' and 'or' filters in a single request with Google Analytics run report API?

问题

如何在一个请求中同时使用“and”和“or”筛选表达式:Google Analytics 运行报告 API。

我有一个情景,需要根据维度或指标的条件进行结果筛选,条件是 A 或 B 且 C 或 D。其中 A、B、C 和 D 是度量或维度的组合条件。

根据 API 文档,expr 只能是以下之一:or、and。

如何根据给定条件获取结果。

如果可能的话,能否提供一个示例请求。

谢谢,期待回复。

您可以找到我尝试过的示例请求,但出现了异常:“message”: “Invalid value at ‘dimension_filter’ (oneof), oneof field ‘expr’ is already set. Cannot set ‘andGroup’”,在同一个请求中,我是否可以包括和,或条件用于指标过滤。

{
   "dateRanges":[
      {
         "startDate":"2019-01-01",
         "endDate":"2023-05-02",
         "name":"0"
      }
   ],
   "keepEmptyRows":"TRUE",
   "metrics":[
      {
         "name":"Conversions"
      },
      {
         "name":"EngagementRate"
      },
      {
         "name":"EngagedSessions"
      },
      {
         "name":"EventCount"
      },
      {
         "name":"NewUsers"
      },
      {
         "name":"TotalRevenue"
      },
      {
         "name":"TotalUsers"
      }
   ],
   "dimensions":[
      {
         "name":"Browser"
      }
   ],
   "dimensionFilter":{
      "orGroup":{
         "expressions":[
            {
               "filter":{
                  "fieldName":"Browser",
                  "stringFilter":{
                     "matchType":"CONTAINS",
                     "value":"moz"
                  }
               }
            },
            {
               "filter":{
                  "fieldName":"Browser",
                  "stringFilter":{
                     "matchType":"CONTAINS",
                     "value":"test"
                  }
               }
            }
         ]
      },
      "andGroup":{
         "expressions":[
            {
               "filter":{
                  "fieldName":"Browser",
                  "stringFilter":{
                     "matchType":"CONTAINS",
                     "value":"ed"
                  }
               }
            },
            {
               "filter":{
                  "fieldName":"Browser",
                  "stringFilter":{
                     "matchType":"CONTAINS",
                     "value":"ch"
                  }
               }
            }
         ]
      }
   },
   "offset":"0",
   "limit":"10000"
}
英文:

How to use and, or filter expression both in one request : Google Analytics run report API.

I have a one scenario where I have to filter the result on the basis of dimensions or metrics
where A or B and C or D. Here A ,B ,C and D are the conditions which are the combinations of metrics or dimensions.

as per API documentation expr can be only one of the following:
or, and.

how to get the result for given conditions.

could you please provide me sample request if it's possible

Thanks and would like hear back.

You can find the sample request which I have tried and getting the exception as "message": "Invalid value at 'dimension_filter' (oneof), oneof field 'expr' is already set. Cannot set 'andGroup'",
In same request if I can include and , or condition for metric filters as well.

{
"dateRanges":[
{
"startDate":"2019-01-01",
"endDate":"2023-05-02",
"name":"0"
}
],
"keepEmptyRows":"TRUE",
"metrics":[
{
"name":"Conversions"
},
{
"name":"EngagementRate"
},
{
"name":"EngagedSessions"
},
{
"name":"EventCount"
},
{
"name":"NewUsers"
},
{
"name":"TotalRevenue"
},
{
"name":"TotalUsers"
}
],
"dimensions":[
{
"name":"Browser"
}
],
"dimensionFilter":{
"orGroup":{
"expressions":[
{
"filter":{
"fieldName":"Browser",
"stringFilter":{
"matchType":"CONTAINS",
"value":"moz"
}
}
},
{
"filter":{
"fieldName":"Browser",
"stringFilter":{
"matchType":"CONTAINS",
"value":"test"
}
}
}
]
},
"andGroup":{
"expressions":[
{
"filter":{
"fieldName":"Browser",
"stringFilter":{
"matchType":"CONTAINS",
"value":"ed"
}
}
},
{
"filter":{
"fieldName":"Browser",
"stringFilter":{
"matchType":"CONTAINS",
"value":"ch"
}
}
}
]
}
},
"offset":"0",
"limit":"10000"
}

答案1

得分: 1

这是我为您翻译的内容:

这是我创建的一个Java和/或过滤器记住事件和维度的筛选应该是兼容的登录[这里][1]并与您的事件维度和指标进行互动以了解更多信息

当然您也可以更改日期范围

```java
GoogleCredentials credentials =
    GoogleCredentials.fromStream(new FileInputStream(credentialsJsonPath));

BetaAnalyticsDataSettings betaAnalyticsDataSettings =
    BetaAnalyticsDataSettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
        .build();

try (BetaAnalyticsDataClient analyticsData =
    BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {

    RunReportRequest request =
        RunReportRequest.newBuilder()
        .setProperty("properties/" + your propertyId)
        .addDateRanges(DateRange.newBuilder().setStartDate("365daysAgo").setEndDate("yesterday"))

        .addDimensions(Dimension.newBuilder().setName("date"))
        .addDimensions(Dimension.newBuilder().setName("eventName"))
        .addDimensions(Dimension.newBuilder().setName("customEvent:XXXXX")) // 替换XXXXX为您的自定义维度
        .addDimensions(Dimension.newBuilder().setName("fullPageUrl")) // 这不是一个自定义维度

        .addMetrics(Metric.newBuilder().setName("eventCount"))
        .addMetrics(Metric.newBuilder().setName("eventsPerSession"))
        .addMetrics(Metric.newBuilder().setName("eventValue"))

        .setDimensionFilter(FilterExpression.newBuilder()
                .setOrGroup(FilterExpressionList.newBuilder()
                        .addExpressions(FilterExpression.newBuilder()
                                .setFilter(Filter.newBuilder()
                                        .setFieldName("eventName")
                                        .setStringFilter(Filter.StringFilter.newBuilder()
                                                .setMatchType(Filter.StringFilter.MatchType.EXACT)
                                                .setValue("your 1st event to filter"))))  // 例如page_view
                        .addExpressions(FilterExpression.newBuilder()
                                .setFilter(Filter.newBuilder()
                                        .setFieldName("eventName")
                                        .setStringFilter(Filter.StringFilter.newBuilder()
                                                .setMatchType(Filter.StringFilter.MatchType.EXACT)
                                                .setValue("your 2nd event")))))
                                
                .setAndGroup(FilterExpressionList.newBuilder().addExpressions(FilterExpression.newBuilder()
                                .setFilter(Filter.newBuilder()
                                        .setFieldName("eventName")
                                        .setStringFilter(Filter.StringFilter.newBuilder()
                                                .setMatchType(Filter.StringFilter.MatchType.EXACT)
                                                .setValue("your 3rd event"))))))
        .setLimit(100000)
        .build();

    RunReportResponse response = analyticsData.runReport(request);
}

示例请求主体可以如下所示:

property: "properties/填写您的属性ID"
dimensions {
  name: "date"
}
dimensions {
  name: "eventName"
}
dimensions {
  name: "customEvent:category" // 我的自定义维度是类别,应该在"customEvent:"之后写入
}
dimensions {
  name: "customEvent:action" // 我的自定义维度是操作,应该在"customEvent:"之后写入
}
dimensions {
  name: "customEvent:label" // 我的自定义维度是标签,应该在"customEvent:"之后写入
}
metrics {
  name: "eventCount"
}
metrics {
  name: "eventsPerSession"
}
metrics {
  name: "eventValue"
}
date_ranges {
  start_date: "2023-05-22"
  end_date: "2023-05-23"
}
dimension_filter {
  or_group {
    expressions {
      filter {
        field_name: "eventName"
        string_filter {
          match_type: EXACT
          value: "Something"
        }
      }
    }
    expressions {
      filter {
        field_name: "eventName"
        string_filter {
          match_type: EXACT
          value: "myCustomEventName" // 作为示例,我写了我的自定义事件名称
        }
      }
    }
    expressions {
      and_group {
        expressions {
          filter {
            field_name: "eventName"
            string_filter {
              match_type: EXACT
              value: "SOMETHING"
            }
          }
        }
      }
    }
  }
}

希望这对您有所帮助!如果您有任何其他问题,请随时提出。

英文:

This is a java and / or filter for what I created,Remember filtering of events and dimensions should be in a compatible way. Log in Here and play with your events, dimensions and metrics to find out.

of course, you can change date range as well.

`GoogleCredentials credentials =
GoogleCredentials.fromStream(new FileInputStream(credentialsJsonPath));
BetaAnalyticsDataSettings betaAnalyticsDataSettings =
BetaAnalyticsDataSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();    
try (BetaAnalyticsDataClient analyticsData =
BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
RunReportRequest request =
RunReportRequest.newBuilder()
.setProperty("properties/" + your propertyId)
.addDateRanges(DateRange.newBuilder().setStartDate("365daysAgo").setEndDate("yesterday"))
.addDimensions(Dimension.newBuilder().setName("date")) 
.addDimensions(Dimension.newBuilder().setName("eventName"))
.addDimensions(Dimension.newBuilder().setName("customEvent:XXXXX")) // instead of XXXXX write your custom dimension
.addDimensions(Dimension.newBuilder().setName("fullPageUrl")) // this is not a customised dimension
.addMetrics(Metric.newBuilder().setName("eventCount"))
.addMetrics(Metric.newBuilder().setName("eventsPerSession"))
.addMetrics(Metric.newBuilder().setName("eventValue"))
.setDimensionFilter(FilterExpression.newBuilder()
.setOrGroup(FilterExpressionList.newBuilder()
.addExpressions(FilterExpression.newBuilder()
.setFilter(Filter.newBuilder()
.setFieldName("eventName")
.setStringFilter(Filter.StringFilter.newBuilder()
.setMatchType(Filter.StringFilter.MatchType.EXACT)
.setValue("your 1st event to filter"))))  // for example page_view
.addExpressions(FilterExpression.newBuilder()
.setFilter(Filter.newBuilder()
.setFieldName("eventName")
.setStringFilter(Filter.StringFilter.newBuilder()
.setMatchType(Filter.StringFilter.MatchType.EXACT)
.setValue("your 2nd event")))))
.setAndGroup(FilterExpressionList.newBuilder().addExpressions(FilterExpression.newBuilder()
.setFilter(Filter.newBuilder()
.setFieldName("eventName")
.setStringFilter(Filter.StringFilter.newBuilder()
.setMatchType(Filter.StringFilter.MatchType.EXACT)
.setValue("your 3rd event"))))))
.setLimit(100000)
.build();
RunReportResponse response = analyticsData.runReport(request);`

The Request Body as an example could be like this:

property: "properties/write your propertId"
dimensions {
name: "date"
}
dimensions {
name: "eventName"
}
dimensions {
name: "customEvent:category" // my custom dimensions is category that should be written after "customEvent:" 
}
dimensions {
name: "customEvent:action" // my custom dimensions is action that should be written after "customEvent:" 
}
dimensions {
name: "customEvent:label" // my custom dimensions is label that should be written after "customEvent:" 
}
metrics {
name: "eventCount"
}
metrics {
name: "eventsPerSession"
}
metrics {
name: "eventValue"
}
date_ranges {
start_date: "2023-05-22" 
end_date: "2023-05-23"
}
dimension_filter {
or_group {
expressions {
filter {
field_name: "eventName"
string_filter {
match_type: EXACT
value: "Something"  
}
}
}
expressions {
filter {
field_name: "eventName"
string_filter {
match_type: EXACT
value: "myCustomEventName " // as an example I wrote my customised event name
}
}
}
expressions {
and_group {
expressions {
filter {
field_name: "eventName"
string_filter {
match_type: EXACT
value: "SOMETHING"   
}
}
}
}
}
}
}

huangapple
  • 本文由 发表于 2023年5月22日 13:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定