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? Reply To: How to track custom event occurrences with specific parameters in GA4 using the API?

  • 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.