This program allows you to automatically fetch data from Google Analytics 4 (GA4) and upload it to Google Sheets.
This program uses the Google Analytics Data API and gspread to interact with Google Sheets.
- Ensure you have a Google service account credential and that this service has been granted access to your Google Analytics 4 account and your Google Sheet.
- Make sure you have the required packages installed:
pip install google-analytics-data gspread oauth2client requests
- Replace 'PATH to your Service acc file' with the path to your Google service account credential file.
- Replace 'Your google sheet doc id' with the ID of your Google Sheets document.
- Replace 'Your sheet name' with the name of the sheet you want to upload the data to.
- Replace 'your property id' with your GA4 property ID.
To choose the right dimensions and metrics for your GA4 report, use the GA4 Dimensions & Metrics Explorer. This tool helps you find the available dimensions and metrics that you can use to fetch data from GA4.
To get a Google service account file:
- Go to the Google Cloud Console.
- Create a new project or select an existing project.
- Navigate to IAM & Admin > Service accounts.
- Click on Create Service Account.
- Fill in the details and click Create.
- On the next page, grant necessary permissions to the service account (e.g., Google Analytics Data API access).
- On the last step, click on Create Key and choose JSON format.
- Your browser will download the JSON service account key.
- Remember to share your Google Sheet with the email address associated with your service account (found in the JSON file under "client_email").
- After setting up and configuring:
python3 path_to_script.py
- The data will automatically be fetched from your GA4 account and uploaded to your Google Sheet.
This project is distributed under the MIT license. You can use, modify, and distribute it under the terms of this license.