GA-RepGen is a tool designed to automate the process of generating reports by integrating data from Google Analytics and dynamically update Google Sheets. This tool utilizes Google APIs to fetch analytics data and update a specified Google Sheet in real-time, streamlining data analysis and reporting workflows.
With GA-RepGen, you can:
- Access and retrieve data from Google Analytics using the Google Analytics API.
- Update and manipulate data within a Google Sheet using the Google Sheets API.
- Automate the generation of reports to save time and improve accuracy.
This README provides instructions for setting up the necessary API credentials and integrating them with GA-RepGen.
- A Google Cloud Platform (GCP) account.
- The necessary APIs (Google Analytics API and Google Sheets API) enabled in your project.
- Access to the Google Sheet being updated.
- Go to the Google Cloud Console.
- Log in with your Google account.
- In the top navigation bar, click the project dropdown menu.
- Click
New Project
. - Name your project and click
Create
.
- In the left-hand menu, navigate to APIs & Services > Library.
- Search for
Google Analytics API
. - Click
Enable
.
- Return to the Library.
- Search for
Google Sheets API
. - Click
Enable
.
- In the left-hand menu, navigate to APIs & Services > Credentials.
- Click
Create Credentials
and selectService Account
. - Fill in the details for the service account (e.g., name, role).
- Role: Assign
Editor
orOwner
role as needed for your use case.
- Role: Assign
- Click
Done
.
- Go to the Service Accounts page.
- Find the service account you just created.
- Click the Actions menu (three dots) next to it and select
Manage keys
. - Click
Add Key > Create New Key
. - Select
JSON
and clickCreate
. - Save the downloaded JSON file securely.
- Open the Google Sheet.
- Click the Share button in the top-right corner.
- Add the email address of the service account (found in the JSON file under
client_email
). - Assign
Editor
permissions and clickSend
.
Use the downloaded JSON file in your application to authenticate requests to the Google Analytics API and Google Sheets API.
For example:
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
# Load the JSON key file
credentials = Credentials.from_service_account_file(
'path_to_your_key.json',
scopes=[
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/spreadsheets'
]
)
# Access Google Sheets
sheet_service = build('sheets', 'v4', credentials=credentials)
spreadsheet_id = '1faXFb6yEYzHssBFU-LH5b4YIRhBxttfBuyaHnl09fuA'
result = sheet_service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range='Sheet1!A1:A10'
).execute()
print(result)
- Keep your JSON file secure; do not expose it publicly.
- Ensure the service account has sufficient permissions to access the APIs and Google Sheet.
- Refer to the Google Cloud Documentation for additional details.