2

I am trying to call Google Analytics Data API (GA4) using the Java client library and applying a dimension filter. This is the call which is working if I don't use the setDimensionFilter call:

RunReportRequest request =
    RunReportRequest.newBuilder()
        .setProperty(propertyId)
        .addDimensions(com.google.analytics.data.v1beta.Dimension.newBuilder().setName("pageLocation"))
        .addMetrics(com.google.analytics.data.v1beta.Metric.newBuilder().setName("screenPageViews"))
        .addMetrics(com.google.analytics.data.v1beta.Metric.newBuilder().setName("activeUsers"))
//        .setDimensionFilter(FilterExpression.newBuilder().setFilter(Filter.newBuilder().setStringFilter(
//             Filter.StringFilter.newBuilder()
//               .setMatchType(Filter.StringFilter.MatchType.FULL_REGEXP)
//               .setField(Descriptors.FieldDescriptor, "pageLocation")
//               .setValue("MY_REGEXP")
//               .build())))
        .addDateRanges(com.google.analytics.data.v1beta.DateRange.newBuilder()
        .setStartDate(startDate.toStringYYYYMMDDWithDashes())
        .setEndDate(endDate.toStringYYYYMMDDWithDashes()))
        .setKeepEmptyRows(true)
        .build();

I don't know how to use setDimensionFilter. If the usage which is commented in the previous code is correct, then the only thing missing is the call to setField. I don't know how to generate the Descriptors.FieldDescriptor instance (or even its meaning).

I have reviewed the client library javadoc, and also the code samples (which are really simple and unfortunately do not show any usage of setDimensionFilter).

Óscar
  • 650
  • 1
  • 4
  • 16
  • check [filter expression](https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/FilterExpression) – Linda Lawton - DaImTo Mar 29 '22 at 17:35
  • I am already using [FilterExpression](https://googleapis.dev/java/google-analytics-data/latest/com/google/analytics/data/v1beta/FilterExpression.html) from the Java API, but I don't know how to configure a dimension filter using it. – Óscar Mar 29 '22 at 17:40
  • You should check the source code for the library [CheckCompatibilityRequest.java#L1978](https://github.com/googleapis/java-analytics-data/blob/01e462d38b387b6e09fb7c96bc561d1e5c1a3b3d/proto-google-analytics-data-v1beta/src/main/java/com/google/analytics/data/v1beta/CheckCompatibilityRequest.java#L1978) you should be able to figure out how to set it from there. – Linda Lawton - DaImTo Mar 30 '22 at 06:32
  • 2
    I opened an issue for you requesting that they consider adding more documentation https://github.com/googleapis/java-analytics-data/issues/463 I was unable to find enough information in the docs and code to help figure out how to set this. – Linda Lawton - DaImTo Mar 30 '22 at 06:41
  • Not to much help i cant install java right now so i cant test it. I could try over lunch with .net it might be close. – Linda Lawton - DaImTo Mar 30 '22 at 06:51
  • That would be really helpful! – Óscar Mar 30 '22 at 08:39

1 Answers1

3

The Descriptors.FieldDescriptor isn't part of the GA4 Data API and is an internal functionality of the protobuf framework

If you are trying to call this filter on a field with the name 'pageLocation' instead of using setField, I think you can do something like this

RunReportRequest request =
    RunReportRequest.newBuilder()
        .setProperty("properties/" + propertyId)
        .addDimensions(com.google.analytics.data.v1beta.Dimension.newBuilder().setName("pageLocation"))
        .addMetrics(com.google.analytics.data.v1beta.Metric.newBuilder().setName("screenPageViews"))
        .addMetrics(com.google.analytics.data.v1beta.Metric.newBuilder().setName("activeUsers"))
        .setDimensionFilter(FilterExpression.newBuilder()
            .setFilter(Filter.newBuilder()
                .setFieldName("pageLocation")
                .setStringFilter(Filter.StringFilter.newBuilder()
                    .setMatchType(Filter.StringFilter.MatchType.FULL_REGEXP)
                    .setValue("MY_REGEXP"))))
        .addDateRanges(com.google.analytics.data.v1beta.DateRange.newBuilder()
            .setStartDate("2020-03-31")
            .setEndDate("2021-03-31"))
        .build();

Also, if you want an additional example of how to use setDimensionFilter, here is another code example that might help

RunReportRequest request =
          RunReportRequest.newBuilder()
              .setProperty("properties/" + propertyId)
              .addDimensions(Dimension.newBuilder().setName("city"))
              .addMetrics(Metric.newBuilder().setName("activeUsers"))
              .addDateRanges(DateRange.newBuilder().setStartDate("2020-03-31").setEndDate("today"))
              .setDimensionFilter(FilterExpression.newBuilder()
                  .setAndGroup(FilterExpressionList.newBuilder()
                      .addExpressions(FilterExpression.newBuilder()
                          .setFilter(Filter.newBuilder()
                              .setFieldName("platform")
                              .setStringFilter(Filter.StringFilter.newBuilder()
                                  .setMatchType(Filter.StringFilter.MatchType.EXACT)
                                  .setValue("Android"))))
                      .addExpressions(FilterExpression.newBuilder()
                          .setFilter(Filter.newBuilder()
                              .setFieldName("eventName")
                              .setStringFilter(Filter.StringFilter.newBuilder()
                                  .setMatchType(Filter.StringFilter.MatchType.EXACT)
                                  .setValue("in_app_purchase"))))))
              .setMetricFilter(FilterExpression.newBuilder()
                  .setFilter(Filter.newBuilder()
                      .setFieldName("sessions")
                      .setNumericFilter(Filter.NumericFilter.newBuilder()
                          .setOperation(Filter.NumericFilter.Operation.GREATER_THAN)
                          .setValue(NumericValue.newBuilder()
                              .setInt64Value(1000)))))
              .build(); 
anweshan
  • 46
  • 1