diff --git a/analytics/lungmap-analytics/sheets/constants.py b/analytics/lungmap-analytics/sheets/constants.py new file mode 100644 index 000000000..e30cd79d4 --- /dev/null +++ b/analytics/lungmap-analytics/sheets/constants.py @@ -0,0 +1,19 @@ +# 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 = "Lungmap Data Browser" +HISTORIC_UA_DATA_PATH = "../users_over_time_history.json" +LUNGMAP_ID = "362871218" +SECRET_NAME = 'ANALYTICS_REPORTING_CLIENT_SECRET_PATH' +GA_PROPERTY_PORTAL = "368678391" # Lungmap DX - GA4 +ANALYTICS_START = "2023-07-01" + +OAUTH_PORT = 8082 \ No newline at end of file diff --git a/analytics/lungmap-analytics/sheets/generate_sheets_report.ipynb b/analytics/lungmap-analytics/sheets/generate_sheets_report.ipynb new file mode 100644 index 000000000..afc36c008 --- /dev/null +++ b/analytics/lungmap-analytics/sheets/generate_sheets_report.ipynb @@ -0,0 +1,174 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "%env ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../../do_not_commit_ga4_credentials.json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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", + "lungmap_catalog_params = {\n", + " **default_params,\n", + " \"property\": LUNGMAP_ID,\n", + "}\n", + "\n", + "lungmap_catalog_params_all_time = {\n", + " **lungmap_catalog_params,\n", + " \"start_date\": ANALYTICS_START,\n", + " \"end_date\": END_DATE_CURRENT,\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df_monthly_pageviews = elements.get_page_views_over_time_df(lungmap_catalog_params_all_time, additional_data_path=HISTORIC_UA_DATA_PATH, additional_data_behavior=elements.ADDITIONAL_DATA_BEHAVIOR.ADD)\n", + "df_pageviews = elements.get_page_views_change(lungmap_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)\n", + "df_outbound = elements.get_outbound_links_change(lungmap_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict_spreadsheet = {\n", + " \"Monthly Traffic 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", + " \"Monthly Traffic 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", + " \"Monthly Traffic Summary\": {\n", + " \"extra_columns\": 1,\n", + " \"extra_columns_width\": 2000\n", + " }\n", + " }\n", + ")\n", + "monthly_traffic_worksheet = sheet.worksheet(\"Monthly Traffic 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(\"Monthly Traffic 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=75,\n", + " title=\"Pageviews and Users Over Time\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +} diff --git a/analytics/lungmap-analytics/sheets/readme.md b/analytics/lungmap-analytics/sheets/readme.md new file mode 100644 index 000000000..ef6f3409e --- /dev/null +++ b/analytics/lungmap-analytics/sheets/readme.md @@ -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 diff --git a/analytics/requirements.txt b/analytics/requirements.txt index 5d3f308a2..a66fabd96 100644 --- a/analytics/requirements.txt +++ b/analytics/requirements.txt @@ -1,6 +1,6 @@ accessible-pygments==0.0.5 alabaster==0.7.16 --e git+https://github.com/DataBiosphere/data-browser.git@e0ce2c7464107bbbc166f7e21fcc3c4426b6e553#egg=analytics&subdirectory=analytics/analytics_package +-e git+https://github.com/DataBiosphere/data-browser.git@e2653f5605cc3220d28299bfc2cc48205c23067d#egg=analytics&subdirectory=analytics/analytics_package anyio==4.7.0 appdirs==1.4.4 appnope==0.1.4