Skip to content

Commit

Permalink
Update the script to include more reports
Browse files Browse the repository at this point in the history
  • Loading branch information
vimaljoseph committed Dec 12, 2023
1 parent 7d1b901 commit a9e7213
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Also check
- Python 3
- `googleapiclient`
- `oauth2client`
- `pyyaml`
- `argparse`

## Setup and Installation

Expand All @@ -40,7 +42,7 @@ It's recommended to use a virtual environment:
```sh
python -m venv venv
source venv/bin/activate
pip install google-api-python-client oauth2client
pip install google-api-python-client oauth2client pyyaml argparse
```

3. **Service Account and API Key:**
Expand All @@ -49,17 +51,19 @@ pip install google-api-python-client oauth2client
- Rename the file to `ua-archive.json` and place it in the project directory.

4. **Configure the Script:**
- Open the script in a text editor.
- Modify the parameters at the bottom of the script (`api_key`, `view_id`, `dimensions`, `metrics`, `start_date`, `end_date`, `output_file`) as needed.
- Copy 'settings.yml.defualt' to 'settings.yml' and update 'api_key' with the name of json file generated and 'view_id' with your UA property's view ID.
- Edit the `reports_config.yml` to add the reports you want to generate.

## Usage

Run the script with Python:

```sh
python ua_archive.py
python3 analytics_reporter.py --report_id 1 --start 2023-01-01 --end 2023-01-31
```
After successful execution, a CSV file named `UA_report.csv` will be generated in the project directory.
report_id should be the id in the `reports_config.yml`

After successful execution, a CSV file named `<report_id>_<report_name>_report.csv` will be generated in the project directory.

## Contributing

Expand Down
62 changes: 62 additions & 0 deletions analytics_reporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Analytics Reporter
Author: Vimal Joseph
More Information: https://www.zyxware.com/article/6662/backup-universal-analytics-data-python
This script generates reports based on Google Analytics data.
"""
import argparse
import yaml
from ga_data_fetcher import get_data
from utils import format_date, write_to_csv

def load_yaml_config(file_path):
with open(file_path, 'r') as file:
return yaml.safe_load(file)

def generate_report(report_config, start_date, end_date, api_key, view_id, output_file):
if not report_config:
print("Report configuration not found.")
return

dimensions = report_config['dimensions']
metrics = report_config['metrics']

data = get_data(api_key, view_id, dimensions, metrics, start_date, end_date, format_date)

if data:
write_to_csv(data, output_file)
print(f"Successfully saved report to CSV file: {output_file}")
else:
print("No data received for the report.")

def main():
parser = argparse.ArgumentParser(description='Generate Google Analytics reports.')
parser.add_argument('-id', '--report_id', type=int, required=True, help='ID of the report to generate')
parser.add_argument('-s', '--start', type=str, required=True, help='Start date (YYYY-MM-DD)')
parser.add_argument('-e', '--end', type=str, required=True, help='End date (YYYY-MM-DD)')
args = parser.parse_args()

settings = load_yaml_config("settings.yml")
report_configs = load_yaml_config(settings['analytics_settings']['reports_config'])

report_id = args.report_id
start_date = args.start
end_date = args.end
api_key = settings['analytics_settings']['api_key']
view_id = settings['analytics_settings']['view_id']

report_config = next((r for r in report_configs['reports'] if r['id'] == report_id), None)
if not report_config:
print(f"Report configuration for ID {report_id} not found.")
return
# Construct the file name
report_name = report_config['name'].replace(' ', '-').lower()
output_file = f"{report_id}_{report_name}_report.csv"
generate_report(report_config, start_date, end_date, api_key, view_id, output_file)


if __name__ == "__main__":
main()

58 changes: 58 additions & 0 deletions ga_data_fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Data Fetcher
Author: Vimal Joseph
More Information: https://www.zyxware.com/article/6662/backup-universal-analytics-data-python
This script fetch the required data using Google Analytics API.
"""
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.errors import HttpError

def get_data(api_key, view_id, dimensions, metrics, start_date, end_date, date_formatter):
# Initialize service
credentials = ServiceAccountCredentials.from_json_keyfile_name(api_key)
service = build('analyticsreporting', 'v4', credentials=credentials)

# Build request
request = service.reports().batchGet(
body={
'reportRequests': [
{
'viewId': view_id,
'dateRanges': [{'startDate': start_date, 'endDate': end_date}],
'dimensions': [{'name': d} for d in dimensions],
'metrics': [{'expression': m} for m in metrics]
}
]
}
)

try:
response = request.execute()
report = response.get('reports', [])[0] # Assuming one report request

column_header_entries = report['columnHeader']['dimensions'] + \
[entry['name'] for entry in report['columnHeader']['metricHeader']['metricHeaderEntries']]

rows = report.get('data', {}).get('rows', [])

formatted_data = []
for row in rows:
formatted_row = {}
dimensions_data = row['dimensions']
metrics_data = row['metrics'][0]['values']
all_data = dimensions_data + metrics_data
for i, header in enumerate(column_header_entries):
if header == 'ga:date':
formatted_row[header] = date_formatter(all_data[i])
else:
formatted_row[header] = all_data[i]
formatted_data.append(formatted_row)

return formatted_data
except HttpError as error:
print(f"Error fetching data: {error}")
return []

46 changes: 46 additions & 0 deletions reports_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
reports:
- id: 1
name: "User Engagement Over Time"
dimensions: ["ga:clientId", "ga:date"]
metrics: ["ga:sessions", "ga:sessionDuration", "ga:pageviews"]

- id: 2
name: "User Journey Analysis"
dimensions: ["ga:landingPagePath", "ga:exitPagePath", "ga:pagePath", "ga:previousPagePath"]
metrics: ["ga:sessions", "ga:pageviews", "ga:entrances", "ga:exits"]

- id: 3
name: "Acquisition Channels and Campaign Performance"
dimensions: ["ga:source", "ga:medium", "ga:campaign"]
metrics: ["ga:sessions", "ga:users"]

- id: 4
name: "User Segmentation"
dimensions: ["ga:userAgeBracket", "ga:userGender", "ga:userType"]
metrics: ["ga:users", "ga:sessions", "ga:sessionDuration", "ga:pageviews"]

- id: 5
name: "Conversion Analysis"
dimensions: ["ga:goalCompletionLocation", "ga:goalPreviousStep1", "ga:goalPreviousStep2", "ga:goalPreviousStep3"]
metrics: ["ga:goalCompletions", "ga:goalConversionRate"]

- id: 6
name: "User Interaction with Content"
dimensions: ["ga:pagePath"]
metrics: ["ga:pageviews", "ga:uniquePageviews", "ga:avgTimeOnPage"]

- id: 7
name: "Keyword and SEO Analysis"
dimensions: ["ga:keyword", "ga:searchQuery"]
metrics: ["ga:sessions", "ga:users", "ga:organicSearches"]

- id: 8
name: "Device and Technology Analysis"
dimensions: ["ga:deviceCategory", "ga:browser", "ga:operatingSystem"]
metrics: ["ga:sessions", "ga:users", "ga:pageviews"]

- id: 9
name: "Custom User Events Tracking"
dimensions: ["ga:eventCategory", "ga:eventAction", "ga:eventLabel"]
metrics: ["ga:totalEvents", "ga:uniqueEvents"]

5 changes: 5 additions & 0 deletions settings.yml.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
analytics_settings:
reports_config: "reports_config.yml"
api_key: "REPLACE with JSON file name from Google"
view_id: "REPLACE with UA View ID"

8 changes: 8 additions & 0 deletions ua_archive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""
Analytics Reporter
Author: Vimal Joseph
More Information: https://www.zyxware.com/article/6662/backup-universal-analytics-data-python
This script generates reports based on Google Analytics data.
"""
import csv
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
Expand Down
18 changes: 18 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import csv
def format_date(date_str):
"""Convert date from 'YYYYMMDD' to 'YYYY-MM-DD' format."""
try:
from datetime import datetime
return datetime.strptime(date_str, '%Y%m%d').strftime('%Y-%m-%d')
except ValueError:
return date_str # Return the original string if it can't be converted

def write_to_csv(data, output_file):
if not data:
print("No data to write to CSV.")
return

with open(output_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)

0 comments on commit a9e7213

Please sign in to comment.