Skip to content

Commit 3a98b06

Browse files
author
Jonah Paten
authored
feat: added lungmap analytics reports (#4374) (#4381)
* feat: added lungmap analytics reports (#4374) * fix: corrected date typo (#4374)
1 parent 3c7e64d commit 3a98b06

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# CHANGE THESE VALUES TO GENERATE NEW REPORTS
2+
# The start and end dates of the current month (yyyy-mm-dd)
3+
START_DATE_CURRENT = "2025-01-01"
4+
END_DATE_CURRENT = "2025-01-31"
5+
# The start and end dates of the prior months
6+
START_DATE_PRIOR = "2024-12-01"
7+
END_DATE_PRIOR = "2024-12-31"
8+
# The name of the folder in which to save the report
9+
PARENT_FOLDER_NAME = "January 2025"
10+
11+
# The name of the spreadsheet with the report
12+
SHEET_NAME = "Lungmap Data Browser"
13+
HISTORIC_UA_DATA_PATH = "../users_over_time_history.json"
14+
LUNGMAP_ID = "362871218"
15+
SECRET_NAME = 'ANALYTICS_REPORTING_CLIENT_SECRET_PATH'
16+
GA_PROPERTY_PORTAL = "368678391" # Lungmap DX - GA4
17+
ANALYTICS_START = "2023-07-01"
18+
19+
OAUTH_PORT = 8082
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import analytics.api as ga\n",
10+
"import analytics.sheets_api as sheets\n",
11+
"import analytics.sheets_elements as elements\n",
12+
"import pandas as pd\n",
13+
"import gspread\n",
14+
"from constants import *"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"%env ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../../do_not_commit_ga4_credentials.json"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"ga_authentication, drive_authentication, sheets_authentication = ga.authenticate(\n",
33+
" SECRET_NAME,\n",
34+
" ga.ga4_service_params,\n",
35+
" ga.drive_service_params,\n",
36+
" ga.sheets_service_params,\n",
37+
" port=OAUTH_PORT\n",
38+
")\n",
39+
"\n",
40+
"date_string = f\"{START_DATE_CURRENT} - {END_DATE_CURRENT}\"\n",
41+
"\n",
42+
"default_params = {\n",
43+
" \"service_system\": ga_authentication,\n",
44+
" \"start_date\": START_DATE_CURRENT,\n",
45+
" \"end_date\": END_DATE_CURRENT,\n",
46+
"}\n",
47+
"\n",
48+
"lungmap_catalog_params = {\n",
49+
" **default_params,\n",
50+
" \"property\": LUNGMAP_ID,\n",
51+
"}\n",
52+
"\n",
53+
"lungmap_catalog_params_all_time = {\n",
54+
" **lungmap_catalog_params,\n",
55+
" \"start_date\": ANALYTICS_START,\n",
56+
" \"end_date\": END_DATE_CURRENT,\n",
57+
"}"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"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",
67+
"df_pageviews = elements.get_page_views_change(lungmap_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)\n",
68+
"df_outbound = elements.get_outbound_links_change(lungmap_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"dict_spreadsheet = {\n",
78+
" \"Monthly Traffic Summary\": df_monthly_pageviews,\n",
79+
" \"Pageviews\": df_pageviews,\n",
80+
" \"Outbound Links\": df_outbound,\n",
81+
"}\n",
82+
"sheet = sheets.create_sheet_in_folder(\n",
83+
" drive_authentication,\n",
84+
" SHEET_NAME,\n",
85+
" PARENT_FOLDER_NAME,\n",
86+
" override_behavior=sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE\n",
87+
" )\n",
88+
"sheets.fill_spreadsheet_with_df_dict(\n",
89+
" sheet,\n",
90+
" dict_spreadsheet,\n",
91+
" sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE,\n",
92+
" column_formatting_options={\n",
93+
" \"Monthly Traffic Summary\": {\n",
94+
" \"Month\": sheets.COLUMN_FORMAT_OPTIONS.YEAR_MONTH_DATE,\n",
95+
" \"Users Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
96+
" \"Total Pageviews Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
97+
" },\n",
98+
" \"Outbound Links\": {\n",
99+
" \"Total Clicks Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
100+
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
101+
" },\n",
102+
" \"Pageviews\": {\n",
103+
" \"Total Views Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
104+
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
105+
" },\n",
106+
"\n",
107+
" },\n",
108+
" sheet_formatting_options={\n",
109+
" \"Monthly Traffic Summary\": {\n",
110+
" \"extra_columns\": 1,\n",
111+
" \"extra_columns_width\": 2000\n",
112+
" }\n",
113+
" }\n",
114+
")\n",
115+
"monthly_traffic_worksheet = sheet.worksheet(\"Monthly Traffic Summary\")\n",
116+
"date_range = sheets.WorksheetRange(\n",
117+
" monthly_traffic_worksheet, \n",
118+
" gspread.cell.Cell(1, 1), \n",
119+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 2)\n",
120+
")\n",
121+
"users_range = sheets.WorksheetRange(\n",
122+
" monthly_traffic_worksheet, \n",
123+
" gspread.cell.Cell(1, 2), \n",
124+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 3)\n",
125+
")\n",
126+
"pageviews_range = sheets.WorksheetRange(\n",
127+
" monthly_traffic_worksheet, \n",
128+
" gspread.cell.Cell(1, 3), \n",
129+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 4)\n",
130+
")\n",
131+
"sheets.add_chart_to_sheet(\n",
132+
" sheets_authentication,\n",
133+
" sheet,\n",
134+
" sheet.worksheet(\"Monthly Traffic Summary\"),\n",
135+
" sheets.CHART_TYPES.LINE,\n",
136+
" date_range,\n",
137+
" [users_range, pageviews_range],\n",
138+
" chart_position=gspread.cell.Cell(1, 6),\n",
139+
" chart_position_offset_x=75,\n",
140+
" chart_position_offset_y=75,\n",
141+
" title=\"Pageviews and Users Over Time\"\n",
142+
")"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": []
151+
}
152+
],
153+
"metadata": {
154+
"kernelspec": {
155+
"display_name": "venv",
156+
"language": "python",
157+
"name": "python3"
158+
},
159+
"language_info": {
160+
"codemirror_mode": {
161+
"name": "ipython",
162+
"version": 3
163+
},
164+
"file_extension": ".py",
165+
"mimetype": "text/x-python",
166+
"name": "python",
167+
"nbconvert_exporter": "python",
168+
"pygments_lexer": "ipython3",
169+
"version": "3.12.8"
170+
}
171+
},
172+
"nbformat": 4,
173+
"nbformat_minor": 4
174+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Generating Reports
2+
- Update `constants.py` to reflect the date ranges and file name you would like for the report
3+
- Open `./generate_sheets_report.ipynb` using your favorite IDE or by running `jupyter notebook` and selecting it from the browser window that appears
4+
- 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
5+
- Check your Google Drive to ensure that the desired spreadsheet is present

0 commit comments

Comments
 (0)