Google Analytics4 Data Extract using Python
The SERVICE_ACCOUNT_FILE in the script refers to the JSON
key file of a service account that has permissions to access the Google
Analytics Data API. This file is essential for authentication and should be
downloaded from the Google Cloud Console.
Steps to Obtain the SERVICE_ACCOUNT_FILE
- Create
a Service Account:
- Go
to the Google Cloud Console.
- Navigate
to IAM & Admin > Service Accounts.
- Click
Create Service Account.
- Assign
Permissions:
- Assign
the role "Analytics Data Viewer" to the service account.
- Create
a Key:
- After
creating the service account, click on it to open the details.
- Go
to the Keys tab and click Add Key > Create New Key.
- Select
JSON format and click Create. This downloads the key file
to your computer.
- Grant
Access to the GA4 Property:
- Go
to your GA4 account.
- Navigate
to Admin > Account Access Management.
- Add
the service account's email (found in the JSON file) and assign it Viewer
or higher permissions.
- Save
the JSON File:
- Place
the downloaded JSON file in a secure directory within your project.
- In
the script, set SERVICE_ACCOUNT_FILE to the path of this file.
Example:
python
Copy code
SERVICE_ACCOUNT_FILE = 'path/to/service_account_key.json'
Security Best Practices
- Do
not hardcode sensitive keys in your script: Store them in environment
variables or a secure location.
- Restrict
permissions: Assign only the necessary roles to the service account.
- Keep
the key file secure: Avoid sharing or exposing it in public
repositories.
Step-by-Step Guide: Granting Access to a GA4 Property for
a Service Account
To ensure your service account can access your GA4 property
data, follow these steps:
1. Access GA4 Admin Settings
- Log
in to Google Analytics.
- In
your GA4 account, select the property where you want to grant access.
- Navigate
to Admin in the left-hand menu.
2. Open Account Access Management
- Under
the Account or Property section, find and click on Account
Access Management or Property Access Management (depending on
the scope you want to assign access to).
- Account
Access Management: Grants access at the account level (applies to all
properties in the account).
- Property
Access Management: Grants access only to a specific property.
3. Add the Service Account Email
- Click
the + button in the upper-right corner and select Add Users.
- In
the Add users window:
- Enter
the email address of the service account. You can find this email
in your service account JSON key file under the client_email field. It
looks something like this:
css
Copy code
your-service-account@your-project-id.iam.gserviceaccount.com
4. Assign Permissions
- In
the Permissions section, select the appropriate role for the
service account. Typically:
- Choose
Viewer if you only need the service account to read data.
- Choose
Editor if you need to configure reports or settings (less common
for data export).
- Ensure
the Notify new users by email box is unchecked (since service
accounts do not have an inbox).
5. Confirm Access
- Click
Add to save the changes.
- The
service account now has access to the GA4 property or account.
Verification
Once access is granted:
- Run
your Python script to fetch data.
- If
you encounter any errors, double-check the email address, permissions,
and ensure the service account key is valid.
PS C:\pandas> streamlit run GA4.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8506
Comments
Post a Comment