-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the script to include more reports
- Loading branch information
1 parent
7d1b901
commit a9e7213
Showing
7 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |