Google Analytics

https://img.shields.io/pypi/v/kpireport-googleanalytics
pip install kpireport-googleanalytics

API

class kpireport_googleanalytics.datasource.GoogleAnalyticsDatasource(report, **kwargs)

Bases: kpireport.datasource.Datasource

A Datasource that provides data from the Google Analytics APIs.

This Datasource supports a whitelist of query types:

report: Get a Report from the V4 Reporting API.

See query_report() for additional options/arguments.

To use in a View, the type of query is specified as the first argument. Any additional keyword arguments are interpreted as options specific to that query type.

# From within a View member function...
df = self.datasources.query("ga", "report", account_like="MyAccount")
key_file

The Google service account key file (must be in JSON format.) Refer to the Google Cloud documentation for more information on how to set up this authentication credential. Default /etc/kpireporter/google_oauth2_key.json.

Type

str

query(input: str, **kwargs)pandas.core.frame.DataFrame

Query the Google Analytics API.

Parameters

input (str) – The name of the query command to invoke. Currently supports only “report”.

Returns

A DataFrame with the query results.

query_report(account_like=None, property_like=None, view_like=None, dimensions=None, metrics=None, filters_expression=None, order_bys=None)pandas.core.frame.DataFrame

Request a report from the GA v4 Analytics API.

Parameters
  • account_like (str) – the GA account name or ID to search for the view. If not defined, the first account found is used.

  • property_like (str) – the GA property name or ID to search for the view. If not defined, the first property found is used.

  • view_like (str) –

    the GA view name or ID. If not defined, the first view found is used.

    Note

    If you have multiple accounts or properties available from your credentials, ensure you set account_like and property_like if you are using this field, as the default functionality for both of those options is to naively take the first account/property found, which may not have the view you’re looking for.

  • dimensions (List[str]) – a list of dimensions. These can be just dimension names, or the full object syntax. If one of these dimensions is a “date-like” dimension (e.g., “ga:date.*”), the output DataFrame will have this dimension treated as a DateTimeIndex, making it effectively return something that looks like a time series.

  • metrics (List[str]) – a list of metrics. These can be just metric expressions, or the full object syntax.

  • filters_expression (str) – an optional filter expression.

  • order_bys (List[dict]) – a list of orderings.

Returns

a pd.DataFrame with dimensions and metrics added.

The dimensions will be the first columns in the resulting table, and each metric returned will be in a subsequent column.

Return type

pd.DataFrame

Changelog

0.0.1

Prelude

Initial commit.

New Features

  • Initial commit.