Sitesh Roy
Sitesh Roy

Reputation: 497

Google Analytics Data API - not able to set empty Filters in request in C#.NET

I'm using the Google Analytics Data API v1 beta to fetch GA4 properties' data in C#.NET.
And I'm initializing the filters for it like this:

FilterExpression dimensionFilterExpression = new FilterExpression();
FilterExpression metricFilterExpression = new FilterExpression();

which I'll later fill if any filters are provided from frontend something like this:

dimensionFilterExpression.Filter = new Filter { FieldName = field_name, InListFilter = new Filter.Types.InListFilter { CaseSensitive = case_sensitive } };

And then I would simply create a request using RunReportRequest with the required parameters

request = new RunReportRequest
          {
                Property = "properties/" + property_id,
                Dimensions = { dimensionsList },
                Metrics = { metricsList },
                DateRanges = { new AnalyticsDataApi.DateRange { StartDate = start_date, EndDate = end_date } },
                DimensionFilter = dimensionFilterExpression,
                MetricFilter = metricFilterExpression
          };

but when the filters are empty I get an exception

StatusCode="InvalidArgument", Detail="FilterExpression requires an expression field"

and same goes for OrderBys MetricFilter as well
how can we handle the situation of no filters or no orderby since then would just have to write all combinations of if else for each case which would not be feasible

Upvotes: 1

Views: 907

Answers (0)

Related Questions