@@ -232,17 +232,47 @@ class ADDITIONAL_DATA_BEHAVIOR(Enum):
232
232
ADD = "add"
233
233
REPLACE = "replace"
234
234
235
+ def get_page_views_over_time_df (analytics_params , additional_data_path = None , additional_data_behavior = None ):
236
+ """
237
+ Get a DataFrame with pageviews and total active users over time from the Analytics API.
238
+ :param analytics_params: the parameters for the Analytics API, including service params, start dates, and end dates
239
+ :param additional_data_path: the path to a JSON file with additional data to be added to the DataFrame, defaults to None
240
+ :param additional_data_behavior: the behavior to use when adding the additional data, defaults to None
241
+ """
242
+ return get_change_over_time_df (
243
+ ["Users" , "Total Pageviews" ],
244
+ ["activeUsers" , "screenPageViews" ],
245
+ ["Month" ],
246
+ "yearMonth" ,
247
+ additional_data_path = additional_data_path ,
248
+ additional_data_behavior = additional_data_behavior ,
249
+ ** analytics_params
250
+ )
251
+
235
252
def get_change_over_time_df (
236
- analytics_params , include_changes = True , additional_data_path = None , additional_data_behavior = None
253
+ metric_titles , metrics , time_title , time_dimension , include_changes = True , change_title_suffix = " Change" , additional_data_path = None , additional_data_behavior = None , strftime_format = "%Y-%m" , ** other_params
237
254
):
255
+ """
256
+ Get a DataFrame with the change over time for the given metrics, renamed to match metric_titles
257
+ :param metric_titles: the titles of the metrics to be displayed
258
+ :param metrics: the metrics to be displayed
259
+ :param time_title: the title to be displayed for the time dimension
260
+ :param time_dimension: the time dimension to be displayed
261
+ :param include_changes: whether to include the percent change columns, defaults to True
262
+ :param change_title_suffix: the suffix to be added to the change columns, defaults to " Change"
263
+ :param additional_data_path: the path to a JSON file with additional data to be added to the DataFrame, defaults to None
264
+ :param additional_data_behavior: the behavior to use when adding the additional data, defaults to None
265
+ :param strftime_format: the format to use for the time dimension, defaults to "%Y-%m". None means a datetime will be returned
266
+ :param other_params: any other parameters to be passed to the get_df_over_time function, including service params
267
+ """
238
268
df_api = get_df_over_time (
239
- [ "Users" , "Total Pageviews" ] ,
240
- [ "activeUsers" , "screenPageViews" ] ,
241
- "yearMonth" ,
242
- sort_results = ["yearMonth" ],
269
+ metric_titles ,
270
+ metrics ,
271
+ time_dimension ,
272
+ sort_results = [time_dimension ],
243
273
df_processor = (lambda df : df .set_index (df .index + "01" )[- 2 ::- 1 ]),
244
274
format_table = False ,
245
- ** analytics_params
275
+ ** other_params
246
276
)
247
277
248
278
df_combined = pd .DataFrame ()
@@ -259,7 +289,13 @@ def get_change_over_time_df(
259
289
df_combined = df_api
260
290
261
291
if include_changes :
262
- df_combined ["Users Change" ] = df_combined ["Users" ].pct_change ()
263
- df_combined ["Total Pageviews Change" ] = df_combined ["Total Pageviews" ].pct_change ()
264
-
265
- return df_combined
292
+ assert change_title_suffix is not None
293
+ df_combined [
294
+ [f"{ title } { change_title_suffix } " for title in metric_titles ]
295
+ ] = df_combined [metric_titles ].pct_change (periods = - 1 ).replace ({np .inf : np .nan })
296
+
297
+ if strftime_format is not None :
298
+ df_combined .index = pd .to_datetime (df_combined .index ).strftime (strftime_format )
299
+
300
+ return df_combined .reset_index (names = time_title )
301
+
0 commit comments