This repo has examples of getting started with Data API for GA4 and Reporting API for Universal Analytics.
The code is taken from the following sources. Please refer to these sources while writing your own code.
UA: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py
https://developers.google.com/analytics/devguides/reporting/core/v4/samples#multiple-segments
GA4: https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries
https://github.com/googleapis/python-analytics-data/tree/main/samples/snippets
Set up steps are described here:
UA: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py
GA4: https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries
Make sure you put a JSON service account credential file in the ./sandbox subdirectory. Reference that file in your code.
The service account should have Viewer access to your UA view and GA4 property.
pip install -r ua/requirements.txt
pip install -r ga4/requirements.txt