2
2
from .charts import get_data_df
3
3
from .fields import *
4
4
from urllib .parse import urlparse
5
+ import datetime as dt
5
6
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 ):
7
8
"""
8
9
Get a df from the Analytics API with a flat structure (no multiindex).
9
10
@@ -22,7 +23,7 @@ def get_flat_data_df(analytics_params, metrics, dimensions, remove_matches=None)
22
23
df = get_data_df (
23
24
metrics ,
24
25
[dimension ["id" ] for dimension in dimensions ],
25
- ** analytics_params ,
26
+ ** other_params ,
26
27
)
27
28
if remove_matches is not None :
28
29
for i , match in enumerate ([dimension ["remove_matches" ] for dimension in dimensions ]):
@@ -95,4 +96,56 @@ def get_outbound_sheets_df(analytics_params):
95
96
"hostname" : "Hostname" ,
96
97
}
97
98
)[["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