I'm trying to get a Google Analytics Admin API response into a Pandas dataframe, and I'm having trouble parsing the results. I'm working with the list customDimensions method within the admin_v1beta library. I can see the results, but I haven't yet figured out how to get them into a format that's usable within Pandas.
My function:
def get_custom_dimensions(property_filter):
client = admin_v1beta.AnalyticsAdminServiceClient()
request = admin_v1beta.ListCustomDimensionsRequest(
parent=property_filter
)
return client.list_custom_dimensions(request=request)
And the output:
ga4_custom_dimensions = get_custom_dimensions("properties/xxx")
print(type(ga4_custom_dimensions))
<class 'google.analytics.admin_v1beta.services.analytics_admin_service.pagers.ListCustomDimensionsPager'>
print(ga4_custom_dimensions)
ListCustomDimensionsPager<custom_dimensions {
name: "properties/xxx/customDimensions/yy1"
parameter_name: "custom_dimension_zz1"
display_name: "Custom Dimension ZZ1"
description: "The dimension ZZ1 useful for filtering."
scope: EVENT
}
custom_dimensions {
name: "properties/xxx/customDimensions/yy2"
parameter_name: "custom_dim...
The response has its own class, so before I can do anything with it I need to convert it and I haven't yet figured out how. My first thought was to convert to JSON:
custom_dimensions_json_1 = json.dumps(ga4_custom_dimensions.__dict__)
custom_dimensions_json_2 = json.dumps(vars(ga4_custom_dimensions))
These produce the same error,
TypeError: Object of type _GapicCallable is not JSON serializable.
My next attempt was via the Pandas json_normalize method:
custom_dimension_df = pd.json_normalize(ga4_custom_dimensions)
The result is a dataframe with only an index.
Lastly, I tried parsing the results directly:
temp_df = pd.DataFrame(ga4_custom_dimensions['custom_dimensions'])[['name',
'parameter_name',
'display_name',
'description',
'scope']]
This produces "TypeError:
ListCustomDimensionsPager' object is not subscriptable.
Can anyone guide me on how to parse the API response within Pandas?