Skip to content

Commit

Permalink
feat: added pageviews analytics functions (#4336)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaten committed Jan 15, 2025
1 parent 2f205ed commit c5a7133
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions analytics/analytics_package/analytics/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Metric names
METRIC_EVENT_COUNT = 'eventCount'
METRIC_TOTAL_USERS = 'totalUsers'
METRIC_PAGE_VIEW = 'screenPageViews'

# DIMENSIONS
DIMENSION_PAGE_PATH = {
Expand Down
59 changes: 56 additions & 3 deletions analytics/analytics_package/analytics/sheets_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from .charts import get_data_df
from .fields import *
from urllib.parse import urlparse
import datetime as dt

def get_flat_data_df(analytics_params, metrics, dimensions, remove_matches=None):
def get_flat_data_df(metrics, dimensions, remove_matches=None, **other_params):
"""
Get a df from the Analytics API with a flat structure (no multiindex).
Expand All @@ -22,7 +23,7 @@ def get_flat_data_df(analytics_params, metrics, dimensions, remove_matches=None)
df = get_data_df(
metrics,
[dimension["id"] for dimension in dimensions],
**analytics_params,
**other_params,
)
if remove_matches is not None:
for i, match in enumerate([dimension["remove_matches"] for dimension in dimensions]):
Expand Down Expand Up @@ -95,4 +96,56 @@ def get_outbound_sheets_df(analytics_params):
"hostname": "Hostname",
}
)[["Page Path", "Hostname", "Outbound Link", "Total Clicks", "Total Users"]]
return df_all_links.copy().reset_index(drop=True)
return df_all_links.copy().reset_index(drop=True)

def get_page_views_df(analytics_params):
"""
Get a DF with page views from the Analytics API.
: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
"""
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,
).rename(
columns={
DIMENSION_PAGE_PATH["alias"]: "Page Path",
METRIC_PAGE_VIEW: "Total Views",
METRIC_TOTAL_USERS: "Total Users",
}
)[["Page Path", "Total Views", "Total Users"]].copy()
return df_response

def get_page_views_change_df(analytics_params, start_current, end_current, start_previous, end_previous):
"""
Get a DF with page views from the Analytics API and a comparison for the prior month
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
:param start_current: the start date for the current month in the format "YYYY-MM-DD"
:param end_current: the end date for the current month
:param start_previous: the start date for the previous month
:param end_previous: the end date for the previous month
"""
analytics_params_current = {
**analytics_params,
"start_date": start_current,
"end_date": end_current,
}
analytics_params_previous = {
**analytics_params,
"start_date": start_previous,
"end_date": end_previous,
}
current_length = float((dt.datetime.fromisoformat(end_current) - dt.datetime.fromisoformat(start_current)).days + 1)
previous_length = float((dt.datetime.fromisoformat(end_previous) - dt.datetime.fromisoformat(start_previous)).days + 1)
df_current = get_page_views_df(analytics_params_current).set_index("Page Path")
df_previous = get_page_views_df(analytics_params_previous).set_index("Page Path") * current_length / previous_length
combined_index = df_current.index.union(df_previous.index)
df_current_reindexed = df_current.reindex(combined_index).fillna(0)
df_previous_reindexed = df_previous.reindex(combined_index)
df_current_reindexed["Total Views Percent Change"] = (df_current_reindexed["Total Views"] / df_previous_reindexed["Total Views"]) - 1
df_current_reindexed["Total Users Percent Change"] = (df_current_reindexed["Total Users"] / df_previous_reindexed["Total Users"]) - 1
return df_current_reindexed.sort_values(["Total Views", "Total Users"], ascending=False, kind="stable").reset_index()

0 comments on commit c5a7133

Please sign in to comment.