Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to track custom event occurrences with specific parameters in GA4 using the API?

  • How to track custom event occurrences with specific parameters in GA4 using the API?

    Posted by Sawyer on 2 June 2023 at 9:08 pm

    So, I’ve created this custom event called “butt”, right? And I’ve been using the API to check how often the event is triggered, but only when a particular “userId” parameter matches one I provide.

    So, when I record the event, it looks like this:

    `javascript
    gtag(‘event’, ‘butt’, {
    userId: ‘VD5894237597126308723423’
    });
    `
    To count that event, I’ve been checking the API like so:

    `javascript
    const [response] = await analyticsDataClient.runReport({
    property: ‘properties/’+process.env.GA4ID,
    dateRanges: [{
    startDate: ‘2020-03-31’,
    endDate: ‘today’,
    }],
    dimensions: [
    {name: ‘eventName’},
    ],
    dimensionFilter: {
    filter: {
    fieldName: ‘eventName’,
    stringFilter: {value: ‘butt’}
    }
    },
    metrics: [
    {name: ‘eventCount’}
    ],
    });
    `
    The result I usually get is { value: 'butt', oneValue: 'value' } { value: '1', oneValue: 'value' }, but I’m not sure if it’s accurate, especially since I’ve triggered the event multiple times but the number doesn’t seem to change.

    Now, here’s where the problem kicks in. I want to restrict the count to only events where the “userId” parameter matches the value ‘VD5894237597126308723423’. I’m not entirely sure how to go about this.

    I tried using a metricFilter like this:

    `javascript
    metricFilter: {
    filter: {
    fieldName: ‘customEvent:userId?’,
    stringFilter: {value: ‘894237597126308723423’}
    }
    },
    `
    But all I get is an error saying, StringFilter cannot be used as metric filter. I need some guidance on the right way to do this. Any ideas?

    Carter replied 1 year ago 3 Members · 2 Replies
  • 2 Replies
  • William

    Member
    16 June 2023 at 8:17 am

    You need to use dimensionFilter again, rather than metricFilter, to filter by the “userId”. The “userId” is a dimension (a characteristic of your users), not a metric (a measurement of their behavior). Also, make sure the fieldName value is correct for your user ID.

  • Carter

    Member
    6 July 2023 at 4:48 pm

    Your approach is accurate, but you’ve encountered a problem because stringFilter cannot be used with metricFilter. Instead, you should continue to use dimensionFilter. When setting up the event, consider the “userId” as a parameter to your “butt” event, which means it considers to be a part of dimension, not metric.

    Here’s how you should filter events where the “userId” parameter matches a certain value:

    `javascript
    const [response] = await analyticsDataClient.runReport({
    property: ‘properties/’+process.env.GA4ID,
    dateRanges: [{
    startDate: ‘2020-03-31’,
    endDate: ‘today’,
    }],
    dimensions: [
    {name: ‘eventName’},
    {name: ‘customEvent:userId’}
    ],
    dimensionFilter: {
    andGroup: {
    filters: [
    {
    filter: {
    fieldName: ‘eventName’,
    stringFilter: {value: ‘butt’}
    }
    },
    {
    filter: {
    fieldName: ‘customEvent:userId’,
    stringFilter: {value: ‘VD5894237597126308723423’}
    }
    },
    ]
    }
    },
    metrics: [
    {name: ‘eventCount’}
    ],
    });
    `
    This way, you’re checking for two filters: if the event name is ‘butt’ and if the userId matches ‘VD5894237597126308723423’. Now you should get the results filtered for specific userId.

Log in to reply.