Sub topic 13
Reading and Updating GA4 Retention with the Admin API
This chapter assumes that you know the GA4 Admin area, properties, events, and users. You should also understand that retention settings affect user-level event data, not every number shown in standard reports.
The Google Analytics Admin API lets you read and update a property's event data retention setting programmatically. This is useful when several properties must follow the same policy or when you want to audit settings regularly.
What the retention resource controls
A property's dataRetentionSettings resource controls how long GA4 keeps user-level and event-level data for features such as Explorations, segments, and audiences. It does not change the default storage period for aggregated data in standard reports.
The resource has two important fields:
| Field | Purpose |
|---|---|
| `eventDataRetention` | The retention period for event and user-level data |
| `resetUserDataOnNewActivity` | Whether new activity starts the retention period again for that user |
Available periods depend on the property and API version. Common enum values include TWO_MONTHS, FOURTEEN_MONTHS, TWENTY_SIX_MONTHS, THIRTY_EIGHT_MONTHS, and FIFTY_MONTHS.
A shorter period reduces the time that detailed data remains available. A longer period supports year-over-year analysis and longer audience membership, but it may require stronger privacy justification.
Read the current setting
The API request uses the property ID in the URL. The access token must belong to a principal with permission to edit or administer the property, and the request normally uses the https://www.googleapis.com/auth/analytics.edit OAuth scope.
# Read the current retention settings for property 123456789
curl -s \
-H "Authorization: Bearer $ACCESS_TOKEN" \
"https://analyticsadmin.googleapis.com/v1beta/properties/123456789/dataRetentionSettings"The response contains the current retention value and the reset behavior. Save this response with the audit date. A later comparison can show whether someone changed the setting outside the approved process.
Update selected fields
Use PATCH to change the resource. The updateMask tells the API which fields should change. This prevents an update from unintentionally replacing unrelated values.
# Set event retention to 14 months and reset retention after new activity
curl -s -X PATCH \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"eventDataRetention": "FOURTEEN_MONTHS",
"resetUserDataOnNewActivity": true
}' \
"https://analyticsadmin.googleapis.com/v1beta/properties/123456789/dataRetentionSettings?updateMask=eventDataRetention,resetUserDataOnNewActivity"After a successful update, issue another GET request. Treat the returned resource as the source of truth rather than assuming that the request applied exactly as intended.
Common errors and safeguards
- Do not confuse event data retention with the lifetime of aggregated reports.
- Do not use a retention period that conflicts with your privacy policy or an approved deletion schedule.
- Do not omit
updateMask; an incomplete mask can leave the intended field unchanged. - Confirm that the property ID belongs to the intended account before sending a
PATCH. - Keep the token and property ID outside source code and logs.
- Test the request against a non-production property before automating changes.
Exercise
Your policy requires 14 months of event data, with the retention period restarting when a user has new activity. Which API fields should your update include, and how should you verify the change?
Show answer
Include eventDataRetention set to FOURTEEN_MONTHS and resetUserDataOnNewActivity set to true. Include both fields in updateMask, then send a GET request and compare the returned values with the policy.