Skip to content

Commit

Permalink
feat: analytics sheets for anvil explorer (#4373) (#4380)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaten authored Feb 19, 2025
1 parent 9828592 commit e92c50d
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
18 changes: 18 additions & 0 deletions analytics/anvil-explorer-sheets/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# CHANGE THESE VALUES TO GENERATE NEW REPORTS
# The start and end dates of the current month (yyyy-mm-dd)
START_DATE_CURRENT = "2025-01-01"
END_DATE_CURRENT = "2025-01-31"
# The start and end dates of the prior months
START_DATE_PRIOR = "2024-12-01"
END_DATE_PRIOR = "2024-12-31"
# The name of the folder in which to save the report
PARENT_FOLDER_NAME = "January 2025"

# The name of the spreadsheet with the report
SHEET_NAME = "AnVIL Explorer"
ANVIL_EXPLORER_ID = "383267328"
SECRET_NAME = 'ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH'
GA_PROPERTY_PORTAL = "368678391" # AnVIL Explorer - GA4
ANALYTICS_START = "2024-01-01"

OAUTH_PORT = 8082
226 changes: 226 additions & 0 deletions analytics/anvil-explorer-sheets/generate_sheets_report.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import analytics.api as ga\n",
"import analytics.sheets_api as sheets\n",
"import analytics.sheets_elements as elements\n",
"import pandas as pd\n",
"import gspread\n",
"from constants import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../do_not_commit_ga4_credentials.json\n"
]
}
],
"source": [
"%env ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../do_not_commit_ga4_credentials.json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=425030666072-vun85q7nt3038skng8gs0f03juh97e17.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8082%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&state=o8oUzUca8ESjd1GImvQPjxAcKhIkdb&access_type=offline\n"
]
}
],
"source": [
"ga_authentication, drive_authentication, sheets_authentication = ga.authenticate(\n",
" SECRET_NAME,\n",
" ga.ga4_service_params,\n",
" ga.drive_service_params,\n",
" ga.sheets_service_params,\n",
" port=OAUTH_PORT\n",
")\n",
"\n",
"date_string = f\"{START_DATE_CURRENT} - {END_DATE_CURRENT}\"\n",
"\n",
"default_params = {\n",
" \"service_system\": ga_authentication,\n",
" \"start_date\": START_DATE_CURRENT,\n",
" \"end_date\": END_DATE_CURRENT,\n",
"}\n",
"\n",
"anvil_catalog_params = {\n",
" **default_params,\n",
" \"property\": ANVIL_EXPLORER_ID,\n",
"}\n",
"\n",
"anvil_catalog_params_all_time = {\n",
" **anvil_catalog_params,\n",
" \"start_date\": ANALYTICS_START,\n",
" \"end_date\": END_DATE_CURRENT,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"df_monthly_pageviews = elements.get_page_views_over_time_df(anvil_catalog_params_all_time)\n",
"df_pageviews = elements.get_page_views_change(anvil_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)\n",
"df_outbound = elements.get_outbound_links_change(anvil_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'spreadsheetId': '1Wm8jbfdBKu7Gd0ld40r8AwT4ffPg6xP4nV-jdY1FsE4',\n",
" 'replies': [{'addChart': {'chart': {'chartId': 918233154,\n",
" 'spec': {'title': 'Pageviews and Users Over Time',\n",
" 'basicChart': {'chartType': 'LINE',\n",
" 'axis': [{'position': 'BOTTOM_AXIS', 'viewWindowOptions': {}},\n",
" {'position': 'LEFT_AXIS', 'viewWindowOptions': {}}],\n",
" 'domains': [{'domain': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
" 'startRowIndex': 0,\n",
" 'endRowIndex': 12,\n",
" 'startColumnIndex': 0,\n",
" 'endColumnIndex': 1}]}}}],\n",
" 'series': [{'series': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
" 'startRowIndex': 0,\n",
" 'endRowIndex': 12,\n",
" 'startColumnIndex': 1,\n",
" 'endColumnIndex': 2}]}},\n",
" 'targetAxis': 'LEFT_AXIS'},\n",
" {'series': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
" 'startRowIndex': 0,\n",
" 'endRowIndex': 12,\n",
" 'startColumnIndex': 2,\n",
" 'endColumnIndex': 3}]}},\n",
" 'targetAxis': 'LEFT_AXIS'}],\n",
" 'headerCount': 1},\n",
" 'hiddenDimensionStrategy': 'SKIP_HIDDEN_ROWS_AND_COLUMNS',\n",
" 'titleTextFormat': {'fontFamily': 'Roboto'},\n",
" 'fontName': 'Roboto'},\n",
" 'position': {'overlayPosition': {'anchorCell': {'sheetId': 1792760679,\n",
" 'columnIndex': 5},\n",
" 'offsetXPixels': 75,\n",
" 'offsetYPixels': 25,\n",
" 'widthPixels': 600,\n",
" 'heightPixels': 371}}}}}]}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict_spreadsheet = {\n",
" \"Explorer Summary\": df_monthly_pageviews,\n",
" \"Pageviews\": df_pageviews,\n",
" \"Outbound Links\": df_outbound,\n",
"}\n",
"sheet = sheets.create_sheet_in_folder(\n",
" drive_authentication,\n",
" SHEET_NAME,\n",
" PARENT_FOLDER_NAME,\n",
" override_behavior=sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE\n",
" )\n",
"sheets.fill_spreadsheet_with_df_dict(\n",
" sheet,\n",
" dict_spreadsheet,\n",
" sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE,\n",
" column_formatting_options={\n",
" \"Explorer Summary\": {\n",
" \"Month\": sheets.COLUMN_FORMAT_OPTIONS.YEAR_MONTH_DATE,\n",
" \"Users Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" \"Total Pageviews Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" },\n",
" \"Outbound Links\": {\n",
" \"Total Clicks Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" },\n",
" \"Pageviews\": {\n",
" \"Total Views Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
" },\n",
"\n",
" },\n",
" sheet_formatting_options={\n",
" \"Explorer Summary\": {\n",
" \"extra_columns\": 1,\n",
" \"extra_columns_width\": 2000\n",
" }\n",
" }\n",
")\n",
"monthly_traffic_worksheet = sheet.worksheet(\"Explorer Summary\")\n",
"date_range = sheets.WorksheetRange(\n",
" monthly_traffic_worksheet, \n",
" gspread.cell.Cell(1, 1), \n",
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 2)\n",
")\n",
"users_range = sheets.WorksheetRange(\n",
" monthly_traffic_worksheet, \n",
" gspread.cell.Cell(1, 2), \n",
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 3)\n",
")\n",
"pageviews_range = sheets.WorksheetRange(\n",
" monthly_traffic_worksheet, \n",
" gspread.cell.Cell(1, 3), \n",
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 4)\n",
")\n",
"sheets.add_chart_to_sheet(\n",
" sheets_authentication,\n",
" sheet,\n",
" sheet.worksheet(\"Explorer Summary\"),\n",
" sheets.CHART_TYPES.LINE,\n",
" date_range,\n",
" [users_range, pageviews_range],\n",
" chart_position=gspread.cell.Cell(1, 6),\n",
" chart_position_offset_x=75,\n",
" chart_position_offset_y=25,\n",
" title=\"Pageviews and Users Over Time\"\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
5 changes: 5 additions & 0 deletions analytics/anvil-explorer-sheets/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Generating Reports
- Update `constants.py` to reflect the date ranges and file name you would like for the report
- Open `./generate_sheets_report.ipynb` using your favorite IDE or by running `jupyter notebook` and selecting it from the browser window that appears
- Run all cells in the Jupyter notebook by pressing the button with two arrows at the top. You will be prompted to log in to your Google Account, which must have access to the relevant analytics property
- Check your Google Drive to ensure that the desired spreadsheet is present

0 comments on commit e92c50d

Please sign in to comment.