-
Utilizing Advanced Functionality: Querying GA4 with Multiple Parameters and Filters using GA4 .NetCore API
Hey, so I’ve been dipping my toes into GA4 and its .netcore API for a few months now. With a little guidance from a stackoverflow mate, I managed to build a .netcore request towards GA4 using their API/SDK. It was alright – a simple code that queries the number of visitors to a Property for a specific time frame.
But now, I’m tackling something a bit bigger, and I’m stumbling a bit. There’s not a lot of help in the documentation, so I’m reaching out. I’ve got an example of how I previously created the RunReportRequest:
`
var request = new RunReportRequest
{
Property = “properties/” + “*********”,
Dimensions = { new Dimension { Name = “date” }, },
Metrics = { new Metric { Name = “totalUsers” }, },
MetricAggregations = { MetricAggregation.Total },
DateRanges = { new DateRange { StartDate = “2022-08-01”, EndDate = “today”},},
};
`
What I need now is a little bit more complex. I need to query for a specific event – “click_sponsored”, let’s say – over a fixed time period. The metrics I’m chasing should cover total clicks or the total occurrences of that event. But I also need to filter results based on three parameters: “client”, “whatever”, and “blabla”.
So what I’m hoping to see is a result that shows the count of that event happening – say, over the last month – and this needs to be filtered by those parameters. I think I should just get one row: “click_sponsored”: 1005, which would direct me to 1005 clicks of the link (that match those parameters) for the last month.
When it comes to creating my RunReportRequest, how should I do it?
After tinkering a little, here’s what I’ve come up with:
`
Filter.Types.StringFilter stringFilter = new Filter.Types.StringFilter
{
Value = “click_sponsored”
};Filter filter = new Filter(filterClient)
{
FieldName = “eventName”,
StringFilter = stringFilter,
};FilterExpression filterExpression = new FilterExpression
{
Filter = filter,
};var request = new RunReportRequest
{
Property = “properties/” + “********”,
Dimensions = { new Dimension { Name = “eventName” }, },
Metrics = { new Metric { Name = “eventCount” }, },
DateRanges = { new DateRange { StartDate = “2022-08-01”, EndDate = “today”}, },
DimensionFilter = filterExpression,
};
`
This seems to spit out all the “click_sponsored” occurrences (event count) for that period. But check out this image:
https://i.stack.imgur.com/qPvf9.png
The result I get programmatically matches the ~4400. But on the right, there’s an events count for the “last 30 minutes”, and there we’ve got parameters. I’d really like to filter by these parameters.
Thinking about it, I could:
1. Create a filter by these parameters – I’m guessing it would be an “AND” filter, because I need an intersection based on three parameters.
2. Or maybe include the parameters in the result and filter through this result by myself afterwards.So now I’m wondering, “How do I add parameters of the given event to the Dimensions?”
Any help would be much appreciated!
Log in to reply.