Skip to content

Commit c5a7133

Browse files
author
jpaten
committed
feat: added pageviews analytics functions (#4336)
1 parent 2f205ed commit c5a7133

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

analytics/analytics_package/analytics/fields.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Metric names
22
METRIC_EVENT_COUNT = 'eventCount'
33
METRIC_TOTAL_USERS = 'totalUsers'
4+
METRIC_PAGE_VIEW = 'screenPageViews'
45

56
# DIMENSIONS
67
DIMENSION_PAGE_PATH = {

analytics/analytics_package/analytics/sheets_elements.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from .charts import get_data_df
33
from .fields import *
44
from urllib.parse import urlparse
5+
import datetime as dt
56

6-
def get_flat_data_df(analytics_params, metrics, dimensions, remove_matches=None):
7+
def get_flat_data_df(metrics, dimensions, remove_matches=None, **other_params):
78
"""
89
Get a df from the Analytics API with a flat structure (no multiindex).
910
@@ -22,7 +23,7 @@ def get_flat_data_df(analytics_params, metrics, dimensions, remove_matches=None)
2223
df = get_data_df(
2324
metrics,
2425
[dimension["id"] for dimension in dimensions],
25-
**analytics_params,
26+
**other_params,
2627
)
2728
if remove_matches is not None:
2829
for i, match in enumerate([dimension["remove_matches"] for dimension in dimensions]):
@@ -95,4 +96,56 @@ def get_outbound_sheets_df(analytics_params):
9596
"hostname": "Hostname",
9697
}
9798
)[["Page Path", "Hostname", "Outbound Link", "Total Clicks", "Total Users"]]
98-
return df_all_links.copy().reset_index(drop=True)
99+
return df_all_links.copy().reset_index(drop=True)
100+
101+
def get_page_views_df(analytics_params):
102+
"""
103+
Get a DF with page views from the Analytics API.
104+
105+
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
106+
:return: a DataFrame with the page views from the Analytics API
107+
"""
108+
df_response = get_flat_data_df(
109+
[METRIC_EVENT_COUNT, METRIC_TOTAL_USERS, METRIC_PAGE_VIEW],
110+
[DIMENSION_PAGE_PATH, DIMENSION_EVENT_NAME],
111+
dimension_filter="eventName==page_view",
112+
**analytics_params,
113+
).rename(
114+
columns={
115+
DIMENSION_PAGE_PATH["alias"]: "Page Path",
116+
METRIC_PAGE_VIEW: "Total Views",
117+
METRIC_TOTAL_USERS: "Total Users",
118+
}
119+
)[["Page Path", "Total Views", "Total Users"]].copy()
120+
return df_response
121+
122+
def get_page_views_change_df(analytics_params, start_current, end_current, start_previous, end_previous):
123+
"""
124+
Get a DF with page views from the Analytics API and a comparison for the prior month
125+
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
126+
:param start_current: the start date for the current month in the format "YYYY-MM-DD"
127+
:param end_current: the end date for the current month
128+
:param start_previous: the start date for the previous month
129+
:param end_previous: the end date for the previous month
130+
"""
131+
analytics_params_current = {
132+
**analytics_params,
133+
"start_date": start_current,
134+
"end_date": end_current,
135+
}
136+
analytics_params_previous = {
137+
**analytics_params,
138+
"start_date": start_previous,
139+
"end_date": end_previous,
140+
}
141+
current_length = float((dt.datetime.fromisoformat(end_current) - dt.datetime.fromisoformat(start_current)).days + 1)
142+
previous_length = float((dt.datetime.fromisoformat(end_previous) - dt.datetime.fromisoformat(start_previous)).days + 1)
143+
df_current = get_page_views_df(analytics_params_current).set_index("Page Path")
144+
df_previous = get_page_views_df(analytics_params_previous).set_index("Page Path") * current_length / previous_length
145+
combined_index = df_current.index.union(df_previous.index)
146+
df_current_reindexed = df_current.reindex(combined_index).fillna(0)
147+
df_previous_reindexed = df_previous.reindex(combined_index)
148+
df_current_reindexed["Total Views Percent Change"] = (df_current_reindexed["Total Views"] / df_previous_reindexed["Total Views"]) - 1
149+
df_current_reindexed["Total Users Percent Change"] = (df_current_reindexed["Total Users"] / df_previous_reindexed["Total Users"]) - 1
150+
return df_current_reindexed.sort_values(["Total Views", "Total Users"], ascending=False, kind="stable").reset_index()
151+

0 commit comments

Comments
 (0)