Created
February 20, 2024 09:18
-
-
Save keNzi/1841d6fd4259924adaf4511670cb08fb to your computer and use it in GitHub Desktop.
retrieve GA4 data using API with UTF-8 metrics name. Save to JSON file using Pandas DataFrame
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
import os | |
import pandas as pd | |
from google.analytics.data_v1beta import BetaAnalyticsDataClient | |
from google.analytics.data_v1beta.types import ( | |
DateRange, | |
Dimension, | |
Metric, | |
MetricType, | |
RunReportRequest, | |
OrderBy, | |
FilterExpression, | |
Filter, | |
FilterExpressionList, | |
) | |
property_list = [ | |
"YOUR-PROPERTY-ID", | |
"YOUR-ANOTHER-PROPERTY-ID", | |
"AND-THE-NEXT-ONE", | |
] | |
properties = { | |
"YOUR-PROPERTY-ID": "domain-url.com", | |
"YOUR-ANOTHER-PROPERTY-ID": "second-domain.com", | |
"AND-THE-NEXT-ONE": "anither-domain.com", | |
} | |
report_type_list = [ | |
'basic', | |
'conversions', | |
] | |
start_date = '2024-02-01' | |
end_date = '2024-02-18' | |
dimensions_dict = { | |
'basic': ['date', 'sessionSourceMedium', 'sessionCampaignName'], | |
'conversions': ['date', 'sessionSourceMedium', 'sessionCampaignName', 'eventName'], | |
} | |
metrics_dict = { | |
'basic': [ | |
'totalUsers', | |
'newUsers', | |
'sessions', | |
'bounceRate', | |
'averageSessionDuration', | |
'screenPageViewsPerSession', | |
], | |
'conversions': [ | |
'conversions', | |
], | |
} | |
def create_dimensions(report_type): | |
dimensions = [] | |
for dimension in dimensions_dict[report_type]: | |
dimensions.append(Dimension(name=dimension)) | |
return dimensions | |
def create_metrics(report_type): | |
metrics = [] | |
for metric in metrics_dict[report_type]: | |
metrics.append(Metric(name=metric)) | |
return metrics | |
def run_sample(): | |
for property_key in property_list: | |
property_id = property_key | |
for report_type in report_type_list: | |
run_report(property_id, report_type) | |
def run_report(property_id, report_type): | |
client = BetaAnalyticsDataClient() | |
request = RunReportRequest( | |
property=f"properties/{property_id}", | |
dimensions=create_dimensions(report_type), | |
metrics=create_metrics(report_type), | |
date_ranges=[DateRange(start_date=start_date, end_date=end_date)], | |
order_bys=[ | |
OrderBy(dimension=OrderBy.DimensionOrderBy(dimension_name="date")) | |
], | |
) | |
response = client.run_report(request) | |
print_run_report_response(response) | |
property_value = properties.get(property_id) | |
print_run_report_response_rows_table(response, property_value, report_type) | |
def print_run_report_response(response): | |
print(f"{response.row_count} rows received") | |
for dimensionHeader in response.dimension_headers: | |
print(f"Dimension header name: {dimensionHeader.name}") | |
for metricHeader in response.metric_headers: | |
metric_type = MetricType(metricHeader.type_).name | |
print(f"Metric header name: {metricHeader.name} ({metric_type})") | |
def print_run_report_response_rows_table(response, property_value, report_type): | |
data = [] | |
for row in response.rows: | |
dimension_values = [value.value for value in row.dimension_values] | |
metric_values = [value.value for value in row.metric_values] | |
data.append(dimension_values + metric_values) | |
columns = [dimension.name for dimension in response.dimension_headers] + [metric.name for metric in response.metric_headers] | |
df = pd.DataFrame(data, columns=columns) | |
# print(df) | |
response_to_json(df, property_value, report_type) | |
return df | |
def response_to_json(data, property_value, report_type): | |
response_files_path = f"ga4_reports/{property_value}/original/{start_date}___{end_date}" | |
report_file = f'{response_files_path}/{report_type}.json' | |
os.makedirs(f'{response_files_path}', exist_ok=True) | |
return data.to_json(report_file, orient='table') | |
if __name__ == "__main__": | |
run_sample() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment