This Python script allows you to fetch data from Google Analytics 4 using the Google Analytics Data API. It is designed to be a template that can be easily customized for your specific requirements.
Before using this script, make sure you have the following:
-
Google Analytics 4 Property ID: Replace YOUR_PROPERTY_ID in the script with the actual Property ID for your Google Analytics 4 property.
-
Service Account Credentials: You need a service account and its corresponding JSON key file. Replace 'path/to/your/credentials.json' with the path to your JSON key file.
-
Python Libraries: Ensure you have the required Python libraries installed. You can install them using:
pip install pandas google-auth google-analytics-data
-
Set Request Parameters: Update the dimension_arr and measure_arr arrays in the script with the dimensions and measures you want to retrieve.
-
Set Date Range: Adjust the start_date_str and end_date_str variables to specify the date range for the data extraction.
-
Run the Script: Execute the script in your Python environment.
python your_script_name.py
-
Review Output: The script will fetch data from Google Analytics 4 for the specified date range, dimensions, and measures. The results will be saved in separate CSV files for each year.
- The script includes a delay of 1 second between API requests to avoid rate limits.
- Any errors encountered during the API requests will be printed to the console.