Sub topic 16
Running a Funnel Report with the GA4 Data API
What you need before calling the API
This chapter assumes that you understand GA4 events and parameters, have used a GA4 property, and know how to authenticate an API request. You also need the numeric property ID, such as 123456789, and permission to read that property.
The Data API lets your application request funnel results instead of relying on the Explore interface. You can use it for scheduled reports, dashboards, or repeated analysis. The runFunnelReport method is available through the Data API alpha channel, so check the current API documentation before putting it into a long-term production integration.
A funnel request has three important parts:
dateRangeslimits the period being analyzed.funneldefines the journey.- Each step contains a name and a filter condition.
Define the funnel in JSON
The following request describes a closed funnel. A user must complete view_item, then add_to_cart, then purchase in that order.
{
"dateRanges": [
{
"startDate": "28daysAgo",
"endDate": "yesterday"
}
],
"funnel": {
"isOpenFunnel": false,
"steps": [
{
"name": "View item",
"filterExpression": {
"funnelEventFilter": {
"eventName": "view_item"
}
}
},
{
"name": "Add to cart",
"filterExpression": {
"funnelEventFilter": {
"eventName": "add_to_cart"
}
}
},
{
"name": "Purchase",
"filterExpression": {
"funnelEventFilter": {
"eventName": "purchase"
}
}
}
]
}
}Send this body to the runFunnelReport method for the property. The request URL identifies the property, typically in the form properties/PROPERTY_ID:runFunnelReport. Your client must also provide valid Google API authentication and the required HTTP headers.
The response contains headers that describe the returned dimensions and metrics, followed by rows containing the funnel results. Read the headers rather than assuming that a value is always in a particular column position. This makes your code safer if you later add a breakdown or another metric.
Add conditions and compare results
An event filter selects users who triggered a named event. You can make a step more specific by filtering an event parameter, such as requiring item_id to equal a particular product. Use the same event names and parameter values that your website or app actually sends.
Set isOpenFunnel to false when users must enter at the first step. Set it to true when users may enter at any step and you want to measure later-stage behavior independently. Keep this choice consistent when comparing API results with an exploration.
The API does not repair missing instrumentation. If purchase is not sent, or if its event name differs from the name in the request, the final step will be undercounted. Validate the events in DebugView or other GA4 reports before diagnosing the request itself.
Exercise
A report has many users at “View item” but zero users at “Purchase.” What should you check first?
Show answer
Check that the purchase event is being sent with the exact event name, that the selected date range contains purchases, and that the API request targets the correct GA4 property.