Skip to content

Commit

Permalink
fix: analytics package issues (#4352)
Browse files Browse the repository at this point in the history
* fix: renamed rowsCount to rowCount (#4351)

* fix: switched ga result filtering from results to dimension filters (#4351)

* chore: bumped analytics package version (#4351)

* chore: updated analytics package docstrings (#4351)

* chore: remove test csv outputs (#4351)

---------

Co-authored-by: hunterckx <[email protected]>
  • Loading branch information
jpaten and hunterckx authored Jan 25, 2025
1 parent 6d65fae commit e0ce2c7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
4 changes: 2 additions & 2 deletions analytics/analytics_package/analytics/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_metrics_by_dimensions_v4_style(service, metrics, dimensions, property, s
while has_more:
result = service.properties().runReport(property=property, body=params).execute()
if rows_left is None:
rows_left = result.get("rowsCount", 0)
rows_left = result.get("rowCount", 0)
page_row_count = len(result["rows"]) if "rows" in result else 0
has_more = page_row_count > 0
if has_more:
Expand All @@ -172,7 +172,7 @@ def get_metrics_by_dimensions_v4_style(service, metrics, dimensions, property, s
offset += max_results
params["offset"] = offset

df = v4_results_to_df(results, dimensions, metrics)
df = v4_results_to_df(results, dimensions, metrics)

return df

Expand Down
9 changes: 5 additions & 4 deletions analytics/analytics_package/analytics/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
METRIC_TOTAL_USERS = 'totalUsers'
METRIC_PAGE_VIEW = 'screenPageViews'

# Event Names
EVENT_BUILTIN_CLICK = "click"
EVENT_CUSTOM_CLICK = "outbound_link_clicked"
EVENT_PAGE_VIEW = "page_view"

# DIMENSIONS
DIMENSION_PAGE_PATH = {
'id': 'pagePath',
'alias': 'page_path',
'remove_matches': None,
}
DIMENSION_BUILTIN_URL = {
'id': 'linkUrl',
'alias': 'builtin_url',
'remove_matches': r"\s*",
}
DIMENSION_EVENT_NAME = {
'id': 'eventName',
'alias': 'event_name',
'remove_matches': None,
}
DIMENSION_CUSTOM_URL = {
'id': 'customEvent:click_url',
'alias': 'outbound_url',
'remove_matches': r"\(not set\)",
}
23 changes: 7 additions & 16 deletions analytics/analytics_package/analytics/sheets_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,20 @@
from urllib.parse import urlparse
import datetime as dt

def get_flat_data_df(metrics, dimensions, remove_matches=None, **other_params):
def get_flat_data_df(metrics, dimensions, **other_params):
"""
Get a df from the Analytics API with a flat structure (no multiindex).
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
:param metrics: the metrics to get
:param dimensions: the dimensions to get
:param remove_matches: a list of regex patterns or None elements to remove from each dimension.
Each regex or None element should correspond with an element of dimensions and remove_matches must be the same length as dimensions.
If the value is None, no patterns are removed, defaults to None.
:return: a DataFrame with the data from the Analytics API
"""
if remove_matches is not None:
assert len(remove_matches) == len(dimensions)

df = get_data_df(
metrics,
[dimension["id"] for dimension in dimensions],
**other_params,
)
if remove_matches is not None:
for i, match in enumerate([dimension["remove_matches"] for dimension in dimensions]):
if match is not None:
df = df.loc[~df.index.get_level_values(i).str.fullmatch(match)]
return df.reset_index().rename(columns=get_rename_dict(dimensions)).copy()

def get_rename_dict(dimensions):
Expand All @@ -41,26 +30,27 @@ def get_rename_dict(dimensions):
def get_outbound_links_df(analytics_params):
"""
Get a DF with outbound links from the Analytics API. Merges the builtin and custom events for outbound links.
analytics_params cannot currently include a dimension_filter
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
:return: a DataFrame with the outbound links from the Analytics API
"""
pd.set_option('future.no_silent_downcasting', True)
assert "dimension_filter" not in analytics_params
# Get the builtin "Click" event
df_builtin_links = get_flat_data_df(
[METRIC_EVENT_COUNT, METRIC_TOTAL_USERS],
[DIMENSION_PAGE_PATH, DIMENSION_BUILTIN_URL, DIMENSION_EVENT_NAME],
remove_matches=[None, r"\s*", None],
dimension_filter=f"eventName=={EVENT_BUILTIN_CLICK}",
**analytics_params,
).groupby(
[DIMENSION_PAGE_PATH["alias"], DIMENSION_BUILTIN_URL["alias"]]
).sum().reset_index()

# Get the custom "outbound_link_click" event
df_custom_links = get_flat_data_df(
[METRIC_EVENT_COUNT, METRIC_TOTAL_USERS],
[DIMENSION_EVENT_NAME, DIMENSION_CUSTOM_URL, DIMENSION_PAGE_PATH],
remove_matches=[DIMENSION_EVENT_NAME["remove_matches"], r"\(not set\)", None],
dimension_filter=f"eventName=={EVENT_CUSTOM_CLICK}",
**analytics_params,
).groupby(
[DIMENSION_PAGE_PATH["alias"], DIMENSION_CUSTOM_URL["alias"]]
Expand Down Expand Up @@ -153,11 +143,12 @@ def get_page_views_df(analytics_params):
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
:return: a DataFrame with the page views from the Analytics API
"""
assert "dimension_filter" not in analytics_params
df_response = get_flat_data_df(
[METRIC_EVENT_COUNT, METRIC_TOTAL_USERS, METRIC_PAGE_VIEW],
[DIMENSION_PAGE_PATH, DIMENSION_EVENT_NAME],
dimension_filter="eventName==page_view",
**analytics_params,
dimension_filter=f"eventName=={EVENT_PAGE_VIEW}",
).rename(
columns={
DIMENSION_PAGE_PATH["alias"]: "Page Path",
Expand Down
2 changes: 1 addition & 1 deletion analytics/analytics_package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="analytics",
version="3.3.0",
version="3.3.1",
packages=["analytics"],
install_requires=["matplotlib", "pandas", "numpy", "google-auth-oauthlib", "google-api-python-client", "gspread", "gspread-formatting"],
)

0 comments on commit e0ce2c7

Please sign in to comment.