Learn business growth with Google Analytics 4 Forums Google Analytics 4 Implementing data retention settings in ga4 using python

  • Implementing data retention settings in ga4 using python

    Posted by Sebastian on 27 November 2022 at 2:47 am

    Hey mates, I’ve just started messing around with ga4. I’ve been trying out the samples right here and I’ve managed to handle dimension and metric queries perfectly fine. But now, I’m trying to crack how to make this request here (for properties.getDataRetentionSettings) using Python. Anyone with an idea on how I can make this happen?

    I’ve also stumbled upon the source code and yet, I couldn’t find a template for a ga4 request outside of dimension and metrics. Any pointers will be super helpful!

    Nam replied 1 year, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • Avery

    Member
    22 January 2023 at 7:34 am

    Hey there! So with GA4, the data you’ve been accessing so far, like dimensions and metrics, typically comes from the Data API. That’s great for getting report-like data.

    However, if you’re trying to access settings from a GA4 property or account, you need to switch gears a bit and use the Admin API instead.

    Good news though: there’s a trove of Python samples for the GA4 admin API available you can check out and start from [here](https://github.com/googleapis/python-analytics-admin/tree/main/samples).

    And guess what? Here’s another [sample code](https://github.com/googleapis/python-analytics-admin/blob/380b4264003b17b802b7099b98837694cd0b87d1/samples/generated_samples/analyticsadmin_v1alpha_generated_analytics_admin_service_get_data_retention_settings_sync.py) I’ve found that might be just what you’re looking for. It’s supposed to retrieve data retention settings, which sounds like what you’re after.

    Just a heads-up though, when you’re using the Admin API, take extra care! You could unintentionally change settings or even delete properties – definitely want to avoid that. Happy coding!

  • Nam

    Member
    14 June 2023 at 4:54 am

    To access data retention settings in Google Analytics 4 (GA4) using Python, the Google Analytics Admin API is needed. It appears, though, that you are presently using the Google Analytics Data API which mainly serves to run reports on the data in GA4 based on specific dimensions and metrics. For the operation you intend to perform – getting data retention settings – you would need to use the Google Analytics Admin API. Unfortunately, as of the time of this response, the Admin API for Python is not available in a stable release yet. Therefore, you might need to directly make HTTP requests as per the method outlined in the link you provided, or wait for the Python version to be released in a stable version.

Log in to reply.