Skip to content

Commit

Permalink
feat: created analytics sheets notebook for hca browser (#4317)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaten committed Jan 27, 2025
1 parent e0ce2c7 commit 70482a8
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
20 changes: 20 additions & 0 deletions analytics/hca-explorer-sheets/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# CHANGE THESE VALUES TO GENERATE NEW REPORTS
# The start and end dates of the current month (yyyy-mm-dd)
START_DATE_CURRENT = "2024-12-01"
END_DATE_CURRENT = "2024-12-31"
# The start and end dates of the prior months
START_DATE_PRIOR = "2024-11-01"
END_DATE_PRIOR = "2024-11-30"
# The name of the folder in which to save the report
PARENT_FOLDER_NAME = "December 2024 (demos)"

# The name of the spreadsheet with the report
SHEET_NAME = "HCA Explorer"

HCA_PORTAL_ID = "361323030"
# Filter to exclude the Data Explorer
HCA_BROWSER_EXCLUDE_FILTER = {"filter": {"fieldName": "hostName", "stringFilter": {"matchType": "EXACT", "value": "explore.data.humancellatlas.org"}}}
SECRET_NAME = "HCA_ANALYTICS_REPORTING_CLIENT_SECRET_PATH"
ANALYTICS_START = "2021-01-01"

OAUTH_PORT = 8082
116 changes: 116 additions & 0 deletions analytics/hca-explorer-sheets/generate_sheets_report.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from analytics import sheets_api as sheets\n",
"from analytics import sheets_elements as elements\n",
"from analytics import api as ga\n",
"import pandas as pd\n",
"from constants import *"
]
},
{
"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%2Fanalytics.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&state=gQXcCGWIVhfxgIYwz935kc6SSgk4Wp&access_type=offline\n"
]
}
],
"source": [
"ga_authentication, drive_authentication = ga.authenticate(\n",
" SECRET_NAME,\n",
" ga.ga4_service_params,\n",
" ga.drive_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",
"hca_portal_params = {\n",
" **default_params,\n",
" \"base_dimension_filter\": HCA_BROWSER_EXCLUDE_FILTER,\n",
" \"property\": HCA_PORTAL_ID,\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"df_pageviews = elements.get_page_views_change(hca_portal_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dict_spreadsheet = {\n",
" \"Page Views\": df_pageviews,\n",
"}\n",
"sheets.fill_spreadsheet_with_df_dict(\n",
" 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",
" dict_spreadsheet,\n",
" sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE,\n",
" column_formatting_options={\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",
" \"Page Views\": {\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",
")"
]
}
],
"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
}
16 changes: 16 additions & 0 deletions analytics/hca-explorer-sheets/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Installing the environment
- Use Python 3.12.4
- Run `python -m venv ./venv` to create a new environment under `./venv`.
- Run `source ./venv/bin/activate` to activate the environment.
- Run `pip install -r ./requirements.txt` to install requirements.

## Deactivating/reactivating
- To deactivate the environment, run `deactivate`.
- To activate the environment again, run `source ./venv/bin/activate`.

## 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.
- Add a path to your Google Cloud credentials in the first cell as instructed by the comments.
- 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.
2 changes: 1 addition & 1 deletion analytics/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
accessible-pygments==0.0.5
alabaster==0.7.16
-e git+https://github.com/DataBiosphere/data-browser.git@60dc83eb36468c1f8091136f8e0534b3cba81b44#egg=analytics&subdirectory=analytics/analytics_package
-e git+https://github.com/DataBiosphere/data-browser.git@6d65fae358f74ca0c6f7fb817c4b0bf89336c5f8#egg=analytics&subdirectory=analytics/analytics_package
appdirs==1.4.4
appnope==0.1.4
asttokens==2.4.1
Expand Down

0 comments on commit 70482a8

Please sign in to comment.