@@ -100,7 +100,7 @@ def get_outbound_links_df(analytics_params):
100100
101101 return df_all_links .copy ().reset_index (drop = True )
102102
103- def get_outbound_links_change_df (analytics_params , start_current , end_current , start_previous , end_previous ):
103+ def get_outbound_links_change (analytics_params , start_current , end_current , start_previous , end_previous ):
104104 """
105105 Get a DF with outbound links from the Analytics API and a comparison for the prior month
106106 :param analytics_params: the parameters for the Analytics API, including authentication and property ids
@@ -125,28 +125,26 @@ def get_outbound_links_change_df(analytics_params, start_current, end_current, s
125125 df_previous = get_outbound_links_df (analytics_params_month_2 ).set_index (
126126 ["Page Path" , "Outbound Link" , "Hostname" ]
127127 )
128- combined_index = df_current .index .union (df_previous .index )
129- df_current_reindexed = df_current .reindex (combined_index ).fillna (0 )
130- df_previous_reindexed = df_previous .reindex (combined_index )
131- df_current_reindexed ["Total Clicks Percent Change" ] = get_change (
132- df_current_reindexed ["Total Clicks" ],
133- df_previous_reindexed ["Total Clicks" ],
128+ total_clicks_percent_change = get_change (
129+ df_current ["Total Clicks" ],
130+ df_previous ["Total Clicks" ],
134131 start_current ,
135132 end_current ,
136133 start_previous ,
137134 end_previous
138135 )
139- df_current_reindexed [ "Total Users Percent Change" ] = get_change (
140- df_current_reindexed ["Total Users" ],
141- df_previous_reindexed ["Total Users" ],
136+ total_users_percent_change = get_change (
137+ df_current ["Total Users" ],
138+ df_previous ["Total Users" ],
142139 start_current ,
143140 end_current ,
144141 start_previous ,
145142 end_previous
146143 )
147- return df_current_reindexed .sort_values (["Total Clicks" , "Total Users" ], ascending = False , kind = "stable" ).reset_index ()
148-
149-
144+ df_reindexed = df_current .reindex (total_clicks_percent_change .index ).fillna (0 )
145+ df_reindexed ["Total Clicks Percent Change" ] = total_clicks_percent_change
146+ df_reindexed ["Total Users Percent Change" ] = total_users_percent_change
147+ return df_reindexed .sort_values (["Total Clicks" , "Total Users" ], ascending = False , kind = "stable" ).reset_index ()
150148
151149def get_page_views_df (analytics_params ):
152150 """
@@ -169,7 +167,7 @@ def get_page_views_df(analytics_params):
169167 )[["Page Path" , "Total Views" , "Total Users" ]].copy ()
170168 return df_response
171169
172- def get_page_views_change_df (analytics_params , start_current , end_current , start_previous , end_previous ):
170+ def get_page_views_change (analytics_params , start_current , end_current , start_previous , end_previous ):
173171 """
174172 Get a DF with page views from the Analytics API and a comparison for the prior month
175173 :param analytics_params: the parameters for the Analytics API, including authentication and property ids
@@ -188,32 +186,33 @@ def get_page_views_change_df(analytics_params, start_current, end_current, start
188186 "start_date" : start_previous ,
189187 "end_date" : end_previous ,
190188 }
191- current_length = float ((dt .datetime .fromisoformat (end_current ) - dt .datetime .fromisoformat (start_current )).days + 1 )
192- previous_length = float ((dt .datetime .fromisoformat (end_previous ) - dt .datetime .fromisoformat (start_previous )).days + 1 )
193189 df_current = get_page_views_df (analytics_params_current ).set_index ("Page Path" )
194- df_previous = get_page_views_df (analytics_params_previous ).set_index ("Page Path" ) * current_length / previous_length
190+ df_previous = get_page_views_df (analytics_params_previous ).set_index ("Page Path" )
195191 combined_index = df_current .index .union (df_previous .index )
196192 df_current_reindexed = df_current .reindex (combined_index ).fillna (0 )
197193 df_previous_reindexed = df_previous .reindex (combined_index )
198- df_current_reindexed [ "Total Views Percent Change" ] = get_change (
194+ views_percent_change = get_change (
199195 df_current_reindexed ["Total Views" ],
200196 df_previous_reindexed ["Total Views" ],
201197 start_current ,
202198 end_current ,
203199 start_previous ,
204200 end_previous ,
205201 )
206- df_current_reindexed [ "Total Users Percent Change" ] = get_change (
202+ users_percent_change = get_change (
207203 df_current_reindexed ["Total Users" ],
208204 df_previous_reindexed ["Total Users" ],
209205 start_current ,
210206 end_current ,
211207 start_previous ,
212208 end_previous ,
213209 )
214- return df_current_reindexed .sort_values (["Total Views" , "Total Users" ], ascending = False , kind = "stable" ).reset_index ()
210+ df_reindexed = df_current .reindex (views_percent_change .index ).fillna (0 )
211+ df_reindexed ["Total Views Percent Change" ] = views_percent_change
212+ df_reindexed ["Total Users Percent Change" ] = users_percent_change
213+ return df_reindexed .sort_values (["Total Views" , "Total Users" ], ascending = False , kind = "stable" ).reset_index ()
215214
216- def get_change (series_current , series_previous , start_current , end_current , start_previous , end_previous ):
215+ def get_change (series_current , series_previous , start_current , end_current , start_previous , end_previous , combined_index = None ):
217216 """
218217 Get the percent change between two serieses, accounting for different numbers of days in the month.
219218 :param series_current: the series representing the current month
@@ -230,8 +229,7 @@ def get_change(series_current, series_previous, start_current, end_current, star
230229 combined_index = series_current .index .union (series_previous .index )
231230 current_length = float ((dt .datetime .fromisoformat (end_current ) - dt .datetime .fromisoformat (start_current )).days + 1 )
232231 previous_length = float ((dt .datetime .fromisoformat (end_previous ) - dt .datetime .fromisoformat (start_previous )).days + 1 )
233- assert current_length != 0
234- assert previous_length != 0
232+ assert current_length != 0 and previous_length != 0
235233 series_current_reindexed = series_current .reindex (combined_index ).fillna (0 )
236234 # Adjust the values from the prior series to account for the different number of days in the month
237235 series_previous_reindexed = (series_previous .reindex (combined_index ) * current_length / previous_length )
0 commit comments