Learn business growth with Google Analytics 4 Forums Google Analytics 4 Utilizing Advanced Functionality: Querying GA4 with Multiple Parameters and Filters using GA4 .NetCore API

  • Utilizing Advanced Functionality: Querying GA4 with Multiple Parameters and Filters using GA4 .NetCore API

    Posted by Joseph on 10 September 2022 at 2:09 am

    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!

    Emma replied 1 year, 2 months ago 2 Members · 1 Reply
  • 1 Reply
  • Emma

    Member
    13 June 2023 at 3:16 am

    To create a filter based on the event parameters (“client”, “whatever”, and “blabla”) you can use the concept of “AND” filtering, as you’ve thought. You create filters for each parameter using the Filter class with a StringFilter or whichever filter type suits your need, then use FilterExpression to combine them with “AND”. In GA4 API, for each parameter you want to add a filter for, it would involve defining a new Filter instance where each Filter.Expression uses a Filter.Types.StringFilter that matches your desired parameter. Finally, assign the filter combination to the RunReportRequest’s DimensionFilter property. In terms of dimensions, when asking “How do I add parameters of the given event to the Dimensions?”, you would add each parameter as a Dimension to your RunReportRequest. Consider the dimension names as “event_param.*paramName*” where *paramName* is “client”, “whatever”, and “blabla” respectively. The addition of parameters as Dimensions should permit the output to include these fields, hence providing an alternate way to filter the data at a later time if so desired.

Log in to reply.