-
How to track custom event occurrences with specific parameters in GA4 using the API?
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?
Log in to reply.