Skip to content

Commit 41f414d

Browse files
author
jpaten
committed
feat: added pageviews analytics functions (#4336)
1 parent eef1cee commit 41f414d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
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-2
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]):
@@ -131,3 +132,56 @@ def get_outbound_links_change(analytics_params, start_current, end_current, star
131132
df_current_reindexed["Total Users Percent Change"] = (df_current_reindexed["Total Users"] / df_previous_reindexed["Total Users"]) - 1
132133
return df_current_reindexed.sort_values(["Total Clicks", "Total Users"], ascending=False, kind="stable").reset_index()
133134

135+
136+
137+
def get_page_views_df(analytics_params):
138+
"""
139+
Get a DF with page views from the Analytics API.
140+
141+
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
142+
:return: a DataFrame with the page views from the Analytics API
143+
"""
144+
df_response = get_flat_data_df(
145+
[METRIC_EVENT_COUNT, METRIC_TOTAL_USERS, METRIC_PAGE_VIEW],
146+
[DIMENSION_PAGE_PATH, DIMENSION_EVENT_NAME],
147+
dimension_filter="eventName==page_view",
148+
**analytics_params,
149+
).rename(
150+
columns={
151+
DIMENSION_PAGE_PATH["alias"]: "Page Path",
152+
METRIC_PAGE_VIEW: "Total Views",
153+
METRIC_TOTAL_USERS: "Total Users",
154+
}
155+
)[["Page Path", "Total Views", "Total Users"]].copy()
156+
return df_response
157+
158+
def get_page_views_change_df(analytics_params, start_current, end_current, start_previous, end_previous):
159+
"""
160+
Get a DF with page views from the Analytics API and a comparison for the prior month
161+
:param analytics_params: the parameters for the Analytics API, including authentication and property ids
162+
:param start_current: the start date for the current month in the format "YYYY-MM-DD"
163+
:param end_current: the end date for the current month
164+
:param start_previous: the start date for the previous month
165+
:param end_previous: the end date for the previous month
166+
"""
167+
analytics_params_current = {
168+
**analytics_params,
169+
"start_date": start_current,
170+
"end_date": end_current,
171+
}
172+
analytics_params_previous = {
173+
**analytics_params,
174+
"start_date": start_previous,
175+
"end_date": end_previous,
176+
}
177+
current_length = float((dt.datetime.fromisoformat(end_current) - dt.datetime.fromisoformat(start_current)).days + 1)
178+
previous_length = float((dt.datetime.fromisoformat(end_previous) - dt.datetime.fromisoformat(start_previous)).days + 1)
179+
df_current = get_page_views_df(analytics_params_current).set_index("Page Path")
180+
df_previous = get_page_views_df(analytics_params_previous).set_index("Page Path") * current_length / previous_length
181+
combined_index = df_current.index.union(df_previous.index)
182+
df_current_reindexed = df_current.reindex(combined_index).fillna(0)
183+
df_previous_reindexed = df_previous.reindex(combined_index)
184+
df_current_reindexed["Total Views Percent Change"] = (df_current_reindexed["Total Views"] / df_previous_reindexed["Total Views"]) - 1
185+
df_current_reindexed["Total Users Percent Change"] = (df_current_reindexed["Total Users"] / df_previous_reindexed["Total Users"]) - 1
186+
return df_current_reindexed.sort_values(["Total Views", "Total Users"], ascending=False, kind="stable").reset_index()
187+

0 commit comments

Comments
 (0)