|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +import cartopy |
| 6 | +import matplotlib |
| 7 | +import matplotlib.cm as cm |
| 8 | +import matplotlib.colors as colors |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import numpy as np |
| 11 | +from google.analytics.data_v1beta import BetaAnalyticsDataClient |
| 12 | +from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest |
| 13 | + |
| 14 | +PORTAL_ID = '266784902' |
| 15 | +FOUNDATIONS_ID = '281776420' |
| 16 | +COOKBOOKS_ID = '324070631' |
| 17 | + |
| 18 | +PRIVATE_KEY_ID = os.environ.get('PRIVATE_KEY_ID') |
| 19 | +PRIVATE_KEY = os.environ.get('PRIVATE_KEY') |
| 20 | + |
| 21 | +credentials_dict = { |
| 22 | + 'type': 'service_account', |
| 23 | + 'project_id': 'cisl-vast-pythia', |
| 24 | + 'private_key_id': PRIVATE_KEY_ID, |
| 25 | + 'private_key': PRIVATE_KEY, |
| 26 | + 'client_email': '[email protected]', |
| 27 | + 'client_id': '113402578114110723940', |
| 28 | + 'auth_uri': 'https://accounts.google.com/o/oauth2/auth', |
| 29 | + 'token_uri': 'https://oauth2.googleapis.com/token', |
| 30 | + 'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs', |
| 31 | + 'client_x509_cert_url': 'https://www.googleapis.com/robot/v1/metadata/x509/pythia-metrics-api%40cisl-vast-pythia.iam.gserviceaccount.com', |
| 32 | + 'universe_domain': 'googleapis.com', |
| 33 | +} |
| 34 | + |
| 35 | +client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict) |
| 36 | + |
| 37 | +pre_project_date = '2020-03-31' # random date before project start |
| 38 | + |
| 39 | + |
| 40 | +def _format_rounding(value): |
| 41 | + return f'{round(value / 1000, 1):.1f}K' |
| 42 | + |
| 43 | + |
| 44 | +def _run_total_users_report(property_id): |
| 45 | + request = RunReportRequest( |
| 46 | + property=f'properties/{property_id}', |
| 47 | + dimensions=[], |
| 48 | + metrics=[Metric(name='activeUsers')], |
| 49 | + date_ranges=[DateRange(start_date=pre_project_date, end_date='today')], |
| 50 | + ) |
| 51 | + response = client.run_report(request) |
| 52 | + |
| 53 | + total_users = 0 |
| 54 | + for row in response.rows: |
| 55 | + total_users += int(row.metric_values[0].value) |
| 56 | + |
| 57 | + return _format_rounding(total_users) |
| 58 | + |
| 59 | + |
| 60 | +def get_total_users(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID): |
| 61 | + metrics_dict = {} |
| 62 | + metrics_dict['Now'] = str(datetime.datetime.now()) |
| 63 | + metrics_dict['Portal'] = _run_total_users_report(PORTAL_ID) |
| 64 | + metrics_dict['Foundations'] = _run_total_users_report(FOUNDATIONS_ID) |
| 65 | + metrics_dict['Cookbooks'] = _run_total_users_report(COOKBOOKS_ID) |
| 66 | + with open('portal/metrics/user_metrics.json', 'w') as outfile: |
| 67 | + json.dump(metrics_dict, outfile) |
| 68 | + |
| 69 | + |
| 70 | +def _run_active_users_this_year(property_id): |
| 71 | + current_year = datetime.datetime.now().year |
| 72 | + start_date = f'{current_year}-01-01' |
| 73 | + |
| 74 | + request = RunReportRequest( |
| 75 | + property=f'properties/{property_id}', |
| 76 | + dimensions=[Dimension(name='date')], |
| 77 | + metrics=[Metric(name='activeUsers')], |
| 78 | + date_ranges=[DateRange(start_date=start_date, end_date='today')], |
| 79 | + ) |
| 80 | + response = client.run_report(request) |
| 81 | + |
| 82 | + dates = [] |
| 83 | + user_counts = [] |
| 84 | + for row in response.rows: |
| 85 | + date_str = row.dimension_values[0].value |
| 86 | + date = datetime.datetime.strptime(date_str, '%Y%m%d') |
| 87 | + dates.append(date) |
| 88 | + user_counts.append(int(row.metric_values[0].value)) |
| 89 | + |
| 90 | + return zip(*sorted(zip(dates, user_counts), key=lambda x: x[0])) |
| 91 | + |
| 92 | + |
| 93 | +def plot_projects_this_year(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID): |
| 94 | + portal_dates, portal_users = _run_active_users_this_year(PORTAL_ID) |
| 95 | + foundations_dates, foundations_users = _run_active_users_this_year(FOUNDATIONS_ID) |
| 96 | + cookbooks_dates, cookbooks_users = _run_active_users_this_year(COOKBOOKS_ID) |
| 97 | + |
| 98 | + plt.figure(figsize=(10, 5.5)) |
| 99 | + plt.title('Year-to-Date Pythia Active Users', fontsize=15) |
| 100 | + |
| 101 | + plt.plot(portal_dates, portal_users, color='purple', label='Portal') |
| 102 | + plt.plot(foundations_dates, foundations_users, color='royalblue', label='Foundations') |
| 103 | + plt.plot(cookbooks_dates, cookbooks_users, color='indianred', label='Cookbooks') |
| 104 | + |
| 105 | + plt.legend(fontsize=12, loc='upper right') |
| 106 | + |
| 107 | + plt.xlabel('Date', fontsize=12) |
| 108 | + plt.savefig('portal/metrics/thisyear.png', bbox_inches='tight') |
| 109 | + |
| 110 | + |
| 111 | +def _run_top_pages_report(property_id): |
| 112 | + request = RunReportRequest( |
| 113 | + property=f'properties/{property_id}', |
| 114 | + dimensions=[Dimension(name='pageTitle')], |
| 115 | + date_ranges=[DateRange(start_date=pre_project_date, end_date='today')], |
| 116 | + metrics=[Metric(name='screenPageViews')], |
| 117 | + ) |
| 118 | + response = client.run_report(request) |
| 119 | + |
| 120 | + views_dict = {} |
| 121 | + for row in response.rows: |
| 122 | + page = row.dimension_values[0].value |
| 123 | + views = int(row.metric_values[0].value) |
| 124 | + views_dict[page] = views |
| 125 | + |
| 126 | + top_pages = sorted(views_dict.items(), key=lambda item: item[1], reverse=True)[:5] |
| 127 | + pages = [page.split('—')[0] for page, _ in top_pages] |
| 128 | + views = [views for _, views in top_pages] |
| 129 | + |
| 130 | + return pages[::-1], views[::-1] |
| 131 | + |
| 132 | + |
| 133 | +def plot_top_pages(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID): |
| 134 | + portal_pages, portal_views = _run_top_pages_report(PORTAL_ID) |
| 135 | + foundations_pages, foundations_views = _run_top_pages_report(FOUNDATIONS_ID) |
| 136 | + cookbooks_pages, cookbooks_views = _run_top_pages_report(COOKBOOKS_ID) |
| 137 | + |
| 138 | + pages = cookbooks_pages + foundations_pages + portal_pages |
| 139 | + |
| 140 | + fig, ax = plt.subplots(figsize=(10, 5.5)) |
| 141 | + plt.title('All-Time Top Pages', fontsize=15) |
| 142 | + |
| 143 | + y = np.arange(5) |
| 144 | + y2 = np.arange(6, 11) |
| 145 | + y3 = np.arange(12, 17) |
| 146 | + y4 = np.append(y, y2) |
| 147 | + y4 = np.append(y4, y3) |
| 148 | + |
| 149 | + bar1 = ax.barh(y3, portal_views, align='center', label='Portal', color='purple') |
| 150 | + bar2 = ax.barh(y2, foundations_views, align='center', label='Foundations', color='royalblue') |
| 151 | + bar3 = ax.barh(y, cookbooks_views, align='center', label='Cookbooks', color='indianred') |
| 152 | + |
| 153 | + ax.set_yticks(y4, labels=pages, fontsize=12) |
| 154 | + |
| 155 | + ax.bar_label(bar1, fmt=_format_rounding, padding=5, fontsize=10) |
| 156 | + ax.bar_label(bar2, fmt=_format_rounding, padding=5, fontsize=10) |
| 157 | + ax.bar_label(bar3, fmt=_format_rounding, padding=5, fontsize=10) |
| 158 | + |
| 159 | + ax.set_xscale('log') |
| 160 | + ax.set_xlim([10, 10**5]) |
| 161 | + ax.set_xlabel('Page Views', fontsize=12) |
| 162 | + |
| 163 | + plt.legend(fontsize=12, loc='lower right') |
| 164 | + plt.savefig('portal/metrics/toppages.png', bbox_inches='tight') |
| 165 | + |
| 166 | + |
| 167 | +def _run_usersXcountry_report(property_id): |
| 168 | + request = RunReportRequest( |
| 169 | + property=f'properties/{property_id}', |
| 170 | + dimensions=[Dimension(name='country')], |
| 171 | + metrics=[Metric(name='activeUsers')], |
| 172 | + date_ranges=[DateRange(start_date=pre_project_date, end_date='today')], |
| 173 | + ) |
| 174 | + response = client.run_report(request) |
| 175 | + |
| 176 | + user_by_country = {} |
| 177 | + for row in response.rows: |
| 178 | + country = row.dimension_values[0].value |
| 179 | + users = int(row.metric_values[0].value) |
| 180 | + user_by_country[country] = user_by_country.get(country, 0) + users |
| 181 | + |
| 182 | + return user_by_country |
| 183 | + |
| 184 | + |
| 185 | +def plot_usersXcountry(FOUNDATIONS_ID): |
| 186 | + users_by_country = _run_usersXcountry_report(FOUNDATIONS_ID) |
| 187 | + |
| 188 | + # Google API Country names do not match Cartopy Country Shapefile names |
| 189 | + dict_api2cartopy = { |
| 190 | + 'Tanzania': 'United Republic of Tanzania', |
| 191 | + 'United States': 'United States of America', |
| 192 | + 'Congo - Kinshasa': 'Democratic Republic of the Congo', |
| 193 | + 'Bahamas': 'The Bahamas', |
| 194 | + 'Timor-Leste': 'East Timor', |
| 195 | + 'C\u00f4te d\u2019Ivoire': 'Ivory Coast', |
| 196 | + 'Bosnia & Herzegovina': 'Bosnia and Herzegovina', |
| 197 | + 'Serbia': 'Republic of Serbia', |
| 198 | + 'Trinidad & Tobago': 'Trinidad and Tobago', |
| 199 | + } |
| 200 | + |
| 201 | + for key in dict_api2cartopy: |
| 202 | + users_by_country[dict_api2cartopy[key]] = users_by_country.pop(key) |
| 203 | + |
| 204 | + top_10_countries = sorted(users_by_country.items(), key=lambda item: item[1], reverse=True)[:10] |
| 205 | + top_10_text = '\n'.join( |
| 206 | + f'{country}: {_format_rounding(value)}' for i, (country, value) in enumerate(top_10_countries) |
| 207 | + ) |
| 208 | + |
| 209 | + fig = plt.figure(figsize=(10, 4)) |
| 210 | + ax = plt.axes(projection=cartopy.crs.PlateCarree(), frameon=False) |
| 211 | + ax.set_title('Pythia Foundations Users by Country', fontsize=15) |
| 212 | + |
| 213 | + shapefile = cartopy.io.shapereader.natural_earth(category='cultural', resolution='110m', name='admin_0_countries') |
| 214 | + reader = cartopy.io.shapereader.Reader(shapefile) |
| 215 | + countries = reader.records() |
| 216 | + |
| 217 | + colormap = plt.get_cmap('Blues') |
| 218 | + newcmp = colors.ListedColormap(colormap(np.linspace(0.2, 1, 128))) |
| 219 | + newcmp.set_extremes(under='grey') |
| 220 | + |
| 221 | + norm = colors.LogNorm(vmin=1, vmax=max(users_by_country.values())) |
| 222 | + mappable = cm.ScalarMappable(norm=norm, cmap=newcmp) |
| 223 | + |
| 224 | + for country in countries: |
| 225 | + country_name = country.attributes['SOVEREIGNT'] |
| 226 | + if country_name in users_by_country.keys(): |
| 227 | + facecolor = newcmp(norm(users_by_country[country_name])) |
| 228 | + ax.add_geometries( |
| 229 | + [country.geometry], |
| 230 | + cartopy.crs.PlateCarree(), |
| 231 | + facecolor=facecolor, |
| 232 | + edgecolor='white', |
| 233 | + linewidth=0.7, |
| 234 | + norm=matplotlib.colors.LogNorm(), |
| 235 | + ) |
| 236 | + else: |
| 237 | + ax.add_geometries( |
| 238 | + [country.geometry], cartopy.crs.PlateCarree(), facecolor='grey', edgecolor='white', linewidth=0.7 |
| 239 | + ) |
| 240 | + |
| 241 | + cax = fig.add_axes([0.1, -0.015, 0.67, 0.03]) |
| 242 | + cbar = fig.colorbar(mappable=mappable, cax=cax, spacing='uniform', orientation='horizontal', extend='min') |
| 243 | + cbar.set_label('Unique Users') |
| 244 | + |
| 245 | + props = dict(boxstyle='round', facecolor='white', edgecolor='white') |
| 246 | + ax.text(1.01, 0.5, top_10_text, transform=ax.transAxes, fontsize=12, verticalalignment='center', bbox=props) |
| 247 | + |
| 248 | + plt.tight_layout() |
| 249 | + plt.savefig('portal/metrics/bycountry.png', bbox_inches='tight') |
| 250 | + |
| 251 | + |
| 252 | +if __name__ == '__main__': |
| 253 | + get_total_users(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) |
| 254 | + plot_projects_this_year(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) |
| 255 | + plot_top_pages(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) |
| 256 | + plot_usersXcountry(FOUNDATIONS_ID) |
0 commit comments