This Python script extracts data from Google Analytics 4 (GA4) using the Google Analytics Data API. It allows for batch report generation with customizable dimensions and metrics. The extracted data is then transformed and output as Pandas DataFrames.
- You can follow the instructions on this page to link your Google service account to Google Analytics 4.
-
Service Account: Place your service account JSON file in the project directory.
-
Update the service account path in the script:
auth_data = AuthorizationData("YOUR_SERVICE_ACCOUNT.json")
-
Set your GA4 property ID:
data = ga4.generate_batch_report(auth_data.client, YOUR_PROPERTY_ID, config)
Define the configuration dictionary to specify the dimensions and metrics for the reports. Here is an example configuration:
config = {
"signal_data": [
{"dimension": ['date', 'userGender', 'userAgeBracket']},
{"metric": ['activeUsers']}
],
"active_usr": [
{"dimension": ['date']},
{"metric": ['active1DayUsers', 'active7DayUsers', 'active28DayUsers']}
]
}