InternalServerError: 500 Exception deserializing response!
Manimozhi01 opened this issue · 0 comments
Manimozhi01 commented
CODE:
from urllib import request
import pandas as pd
from google2pandas import GoogleAnalyticsQueryV4
import os
import json
import time
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
DateRange,
Dimension,
Filter,
FilterExpression,
Metric,
RunReportRequest,
OrderBy,
)
# Set environment variables
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "PATH/TO/YOUR/CREDENTIALS.json"
GA_CALL_DELAY = 20
USER = 'user'
BOM = 'bom'
ALL = "ALL"
property_id = 'YOUR PROPERTY ID'
def ga4_response_to_df(response):
dim_len = len(response.dimension_headers)
metric_len = len(response.metric_headers)
all_data = []
for row in response.rows:
row_data = {}
for i in range(0, dim_len):
row_data.update({response.dimension_headers[i].name: row.dimension_values[i].value})
for i in range(0, metric_len):
row_data.update({response.metric_headers[i].name: row.metric_values[i].value})
all_data.append(row_data)
df = pd.DataFrame(all_data, index=None)
return df
client = BetaAnalyticsDataClient()
request = RunReportRequest(
property=f"properties/{property_id}",
dimensions=[
Dimension(name="date"),
Dimension(name="eventName"),
Dimension(name="customEvent:action"),
Dimension(name="customEvent:label"),
Dimension(name="customEvent:extralabel"),
Dimension(name="customEvent:userCompany"),
Dimension(name="customEvent:userCity"),
Dimension(name="customEvent:releaseType"),
Dimension(name="customEvent:userPersona")
],
metrics=[Metric(name="eventCount")],
date_ranges=[DateRange(start_date="2023-09-01", end_date="2023-09-30")],
dimension_filter=FilterExpression(
filter=Filter(
field_name="customEvent:action",
in_list_filter=Filter.InListFilter(values=["TripTest"]),
)
),
#order_bys=[OrderBy(dimension=OrderBy.DimensionOrderBy(dimension_name='date'), desc=False)]
)
client = BetaAnalyticsDataClient()
response = client.run_report(request)
# Convert the GA4 API response to a Pandas DataFrame
df = ga4_response_to_df(response)
# Convert the 'date' column to datetime format
df['date'] = pd.to_datetime(df['date'])
# Extract month and year from the 'date' column
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
# Replace 'output_file.csv' with your desired output CSV file path
output_csv_file = 'YOUR\PATH\EXCEL.csv'
# Write the DataFrame to a CSV file
df.to_csv(output_csv_file, index=False)
print("Data successfully stored in CSV.")
I am trying to extract data for the custom dimension: event action= TripTest for specific date range. I am retrieving dimensions like event name, label(custom), extralabel(custom) etc. This throws an error : InternalServerError: 500 Exception deserializing response!
If I remove the dimensions label and extralabel in request, it is not throwing any error.
Is there any solution for this issue ?