Sub topic 17
Managing GA4 Audiences Programmatically with the Admin API
Prerequisites and API model
This chapter assumes that you already understand GA4 events, parameters, user properties, and audience conditions. You also need Editor or Administrator access to the property and an OAuth 2.0 access token with permission to edit Analytics configuration.
The Google Analytics Admin API manages audience definitions and their settings. It does not return the current list of users in an audience. Use the Google Analytics Data API or the GA4 interface to analyze audience results.
An audience is addressed below a property resource:
properties/PROPERTY_ID/audiences/AUDIENCE_ID
The audience resource stores values such as:
displayNameanddescriptionmembershipDurationDaysadsPersonalizationEnabled- event-trigger settings
- filter clauses
- sequence conditions
- inclusion and exclusion rules
A filter clause maps to a condition group in the audience builder. Its filter expression can test an event, event parameter, user property, or other supported dimension. The API also records scope, such as one event, one session, or multiple sessions.
List and inspect audiences
The list method helps you discover audience resource names before changing them. The get method returns one complete definition.
export PROPERTY_ID="123456789"
export ACCESS_TOKEN="YOUR_OAUTH_ACCESS_TOKEN"
curl --request GET \
--url "https://analyticsadmin.googleapis.com/v1beta/properties/${PROPERTY_ID}/audiences?pageSize=50" \
--header "Authorization: Bearer ${ACCESS_TOKEN}"A list response contains audience resources and possibly a nextPageToken. Follow that token when more results exist. Store the resource name, not only the display name, because display names do not uniquely identify an audience.
export AUDIENCE_NAME="properties/123456789/audiences/987654321"
curl --request GET \
--url "https://analyticsadmin.googleapis.com/v1beta/${AUDIENCE_NAME}" \
--header "Authorization: Bearer ${ACCESS_TOKEN}"Update and archive safely
Use PATCH with an updateMask so that the request changes only the fields you intend to change.
curl --request PATCH \
--url "https://analyticsadmin.googleapis.com/v1beta/${AUDIENCE_NAME}?updateMask=displayName,description,membershipDurationDays" \
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"displayName": "Cart abandoners - 30 days",
"description": "Users who added an item to cart but did not purchase.",
"membershipDurationDays": 30
}'Audience definitions can be complex. For a new audience, create the definition in the GA4 interface first, inspect its returned JSON, and use that structure as a tested template for API requests. This avoids silently changing a scope or sequence step when translating the interface into nested filterClauses.
The API archives audiences rather than deleting them permanently. Treat archive requests as production changes. Confirm the resource name and keep a copy of the current definition before archiving.
Deployment practice
Keep audience definitions in source control as JSON templates. Use separate property IDs for testing and production. Before applying a change:
- Fetch the current audience definition.
- Compare the intended fields and update mask.
- Apply the change.
- Fetch the resource again and verify the stored values.
- Check membership in GA4 after processing time.
Exercise
Why should an application use an updateMask when patching an audience?
Show answer
It limits the request to named fields, reducing the chance that an omitted or stale field overwrites another part of the audience definition.