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