Skip to content

Commit e92c50d

Browse files
author
Jonah Paten
authored
feat: analytics sheets for anvil explorer (#4373) (#4380)
1 parent 9828592 commit e92c50d

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 = "AnVIL Explorer"
13+
ANVIL_EXPLORER_ID = "383267328"
14+
SECRET_NAME = 'ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH'
15+
GA_PROPERTY_PORTAL = "368678391" # AnVIL Explorer - GA4
16+
ANALYTICS_START = "2024-01-01"
17+
18+
OAUTH_PORT = 8082
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
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": 2,
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"env: ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../do_not_commit_ga4_credentials.json\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"%env ANVIL_ANALYTICS_REPORTING_CLIENT_SECRET_PATH=../../../do_not_commit_ga4_credentials.json"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 3,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"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"
44+
]
45+
}
46+
],
47+
"source": [
48+
"ga_authentication, drive_authentication, sheets_authentication = ga.authenticate(\n",
49+
" SECRET_NAME,\n",
50+
" ga.ga4_service_params,\n",
51+
" ga.drive_service_params,\n",
52+
" ga.sheets_service_params,\n",
53+
" port=OAUTH_PORT\n",
54+
")\n",
55+
"\n",
56+
"date_string = f\"{START_DATE_CURRENT} - {END_DATE_CURRENT}\"\n",
57+
"\n",
58+
"default_params = {\n",
59+
" \"service_system\": ga_authentication,\n",
60+
" \"start_date\": START_DATE_CURRENT,\n",
61+
" \"end_date\": END_DATE_CURRENT,\n",
62+
"}\n",
63+
"\n",
64+
"anvil_catalog_params = {\n",
65+
" **default_params,\n",
66+
" \"property\": ANVIL_EXPLORER_ID,\n",
67+
"}\n",
68+
"\n",
69+
"anvil_catalog_params_all_time = {\n",
70+
" **anvil_catalog_params,\n",
71+
" \"start_date\": ANALYTICS_START,\n",
72+
" \"end_date\": END_DATE_CURRENT,\n",
73+
"}"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 4,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"df_monthly_pageviews = elements.get_page_views_over_time_df(anvil_catalog_params_all_time)\n",
83+
"df_pageviews = elements.get_page_views_change(anvil_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)\n",
84+
"df_outbound = elements.get_outbound_links_change(anvil_catalog_params, START_DATE_CURRENT, END_DATE_CURRENT, START_DATE_PRIOR, END_DATE_PRIOR)"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 5,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"{'spreadsheetId': '1Wm8jbfdBKu7Gd0ld40r8AwT4ffPg6xP4nV-jdY1FsE4',\n",
96+
" 'replies': [{'addChart': {'chart': {'chartId': 918233154,\n",
97+
" 'spec': {'title': 'Pageviews and Users Over Time',\n",
98+
" 'basicChart': {'chartType': 'LINE',\n",
99+
" 'axis': [{'position': 'BOTTOM_AXIS', 'viewWindowOptions': {}},\n",
100+
" {'position': 'LEFT_AXIS', 'viewWindowOptions': {}}],\n",
101+
" 'domains': [{'domain': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
102+
" 'startRowIndex': 0,\n",
103+
" 'endRowIndex': 12,\n",
104+
" 'startColumnIndex': 0,\n",
105+
" 'endColumnIndex': 1}]}}}],\n",
106+
" 'series': [{'series': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
107+
" 'startRowIndex': 0,\n",
108+
" 'endRowIndex': 12,\n",
109+
" 'startColumnIndex': 1,\n",
110+
" 'endColumnIndex': 2}]}},\n",
111+
" 'targetAxis': 'LEFT_AXIS'},\n",
112+
" {'series': {'sourceRange': {'sources': [{'sheetId': 1792760679,\n",
113+
" 'startRowIndex': 0,\n",
114+
" 'endRowIndex': 12,\n",
115+
" 'startColumnIndex': 2,\n",
116+
" 'endColumnIndex': 3}]}},\n",
117+
" 'targetAxis': 'LEFT_AXIS'}],\n",
118+
" 'headerCount': 1},\n",
119+
" 'hiddenDimensionStrategy': 'SKIP_HIDDEN_ROWS_AND_COLUMNS',\n",
120+
" 'titleTextFormat': {'fontFamily': 'Roboto'},\n",
121+
" 'fontName': 'Roboto'},\n",
122+
" 'position': {'overlayPosition': {'anchorCell': {'sheetId': 1792760679,\n",
123+
" 'columnIndex': 5},\n",
124+
" 'offsetXPixels': 75,\n",
125+
" 'offsetYPixels': 25,\n",
126+
" 'widthPixels': 600,\n",
127+
" 'heightPixels': 371}}}}}]}"
128+
]
129+
},
130+
"execution_count": 5,
131+
"metadata": {},
132+
"output_type": "execute_result"
133+
}
134+
],
135+
"source": [
136+
"dict_spreadsheet = {\n",
137+
" \"Explorer Summary\": df_monthly_pageviews,\n",
138+
" \"Pageviews\": df_pageviews,\n",
139+
" \"Outbound Links\": df_outbound,\n",
140+
"}\n",
141+
"sheet = sheets.create_sheet_in_folder(\n",
142+
" drive_authentication,\n",
143+
" SHEET_NAME,\n",
144+
" PARENT_FOLDER_NAME,\n",
145+
" override_behavior=sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE\n",
146+
" )\n",
147+
"sheets.fill_spreadsheet_with_df_dict(\n",
148+
" sheet,\n",
149+
" dict_spreadsheet,\n",
150+
" sheets.FILE_OVERRIDE_BEHAVIORS.OVERRIDE_IF_IN_SAME_PLACE,\n",
151+
" column_formatting_options={\n",
152+
" \"Explorer Summary\": {\n",
153+
" \"Month\": sheets.COLUMN_FORMAT_OPTIONS.YEAR_MONTH_DATE,\n",
154+
" \"Users Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
155+
" \"Total Pageviews Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
156+
" },\n",
157+
" \"Outbound Links\": {\n",
158+
" \"Total Clicks Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
159+
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
160+
" },\n",
161+
" \"Pageviews\": {\n",
162+
" \"Total Views Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
163+
" \"Total Users Percent Change\": sheets.COLUMN_FORMAT_OPTIONS.PERCENT_COLORED,\n",
164+
" },\n",
165+
"\n",
166+
" },\n",
167+
" sheet_formatting_options={\n",
168+
" \"Explorer Summary\": {\n",
169+
" \"extra_columns\": 1,\n",
170+
" \"extra_columns_width\": 2000\n",
171+
" }\n",
172+
" }\n",
173+
")\n",
174+
"monthly_traffic_worksheet = sheet.worksheet(\"Explorer Summary\")\n",
175+
"date_range = sheets.WorksheetRange(\n",
176+
" monthly_traffic_worksheet, \n",
177+
" gspread.cell.Cell(1, 1), \n",
178+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 2)\n",
179+
")\n",
180+
"users_range = sheets.WorksheetRange(\n",
181+
" monthly_traffic_worksheet, \n",
182+
" gspread.cell.Cell(1, 2), \n",
183+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 3)\n",
184+
")\n",
185+
"pageviews_range = sheets.WorksheetRange(\n",
186+
" monthly_traffic_worksheet, \n",
187+
" gspread.cell.Cell(1, 3), \n",
188+
" gspread.cell.Cell(df_monthly_pageviews.index.size + 1, 4)\n",
189+
")\n",
190+
"sheets.add_chart_to_sheet(\n",
191+
" sheets_authentication,\n",
192+
" sheet,\n",
193+
" sheet.worksheet(\"Explorer Summary\"),\n",
194+
" sheets.CHART_TYPES.LINE,\n",
195+
" date_range,\n",
196+
" [users_range, pageviews_range],\n",
197+
" chart_position=gspread.cell.Cell(1, 6),\n",
198+
" chart_position_offset_x=75,\n",
199+
" chart_position_offset_y=25,\n",
200+
" title=\"Pageviews and Users Over Time\"\n",
201+
")"
202+
]
203+
}
204+
],
205+
"metadata": {
206+
"kernelspec": {
207+
"display_name": "venv",
208+
"language": "python",
209+
"name": "python3"
210+
},
211+
"language_info": {
212+
"codemirror_mode": {
213+
"name": "ipython",
214+
"version": 3
215+
},
216+
"file_extension": ".py",
217+
"mimetype": "text/x-python",
218+
"name": "python",
219+
"nbconvert_exporter": "python",
220+
"pygments_lexer": "ipython3",
221+
"version": "3.12.8"
222+
}
223+
},
224+
"nbformat": 4,
225+
"nbformat_minor": 4
226+
}
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)