Search code examples
pythonpandasgoogle-analyticsgoogle-analytics-apigoogle-api-python-client

How to Covert Google analtics API response to Pandas data frame


I have some code which exports data from Google analytics using the Google Analtics data api.

def sample_run_report(credentials=None, property_id="YOUR-GA4-PROPERTY-ID"):
"""Runs a simple report on a Google Analytics 4 property."""

client = BetaAnalyticsDataClient(credentials=credentials)
dates = [DateRange(start_date="7daysAgo", end_date='today')]
metrics = [Metric(name='activeUsers')]
dimensions = [], #[Dimension(name='city')]
request = RunReportRequest(
    property=f'properties/{property_id}',
    metrics=metrics,
    date_ranges=dates,
)

response = client.run_report(request)
print(response)

This code works file it exports the data in the standard response for the api.

{
  "dimensionHeaders": [
    {
      object (DimensionHeader)
    }
  ],
  "metricHeaders": [
    {
      object (MetricHeader)
    }
  ],
  "rows": [
    {
      object (Row)
    }
  ]
 .....
}

I would like to format this as a pandas dataFrame I have not been able to find any method to output it as a dataframe.


Solution

  • The Google Analytics data api only exports data in a single format. I was able to create a method which will covert the response into a Pandas DataFrame

    def covert_to_pandas(data):
    
        # Grab all the columns in the result
        columns = []
        for col in data.dimension_headers:
            columns.append(col.name)
        for col in data.metric_headers:
            columns.append(col.name)
    
        # Grab all the rows in the result.
        rows = []
        for row_data in data.rows:
            row = []
            for val in row_data.dimension_values:
                row.append(val.value)
            for val in row_data.metric_values:
                row.append(val.value)
            rows.append(row)
    
        # convert to data frame
        return pd.DataFrame(rows, columns=columns)
    

    Note

    If you want to use this with any of the graphs make sure to add the following.

    df = df.astype(float)
    

    or you will get an error

    raise TypeError("no numeric data to plot") TypeError: no numeric data to plot

    This is due to the type being object.

    Now I can use it

    enter image description here

    Examining Google Analytics GA4 Data with Python and Pandas.