Skip to content

Commit

Permalink
added all three configurations to outlinks ipynb (#3202)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaten committed Sep 28, 2024
1 parent 7a10f70 commit 45da0b3
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 104 deletions.
19 changes: 19 additions & 0 deletions analytics/anvil-analytics-combined/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ANVIL_PORTAL_NAME = "anvil-portal"
ANVIL_CATALOG_NAME = "anvil-catalog"
ANVIL_EXPLORER_NAME = "anvil-explorer"

PROPERTY_ID_MAP = {
ANVIL_CATALOG_NAME: "368661710",
ANVIL_EXPLORER_NAME: "383267328",
ANVIL_PORTAL_NAME: "368678391",
}

SECRET_NAME = 'GA4_CREDENTIALS'
GA_PROPERTY_PORTAL = "368678391" # AnVIL Portal - GA4
PRE_AUDIENCE_EXCLUDE_PAGES_FILTER = "landingPagePlusQueryString!=/guides/content/creating-links"
EXCLUDE_PAGES_FILTER = {"filter": {"fieldName": "audienceId", "numericFilter": {"operation": "EQUAL", "value": {"doubleValue": 5559548544}}}}
ANALYTICS_START = "2021-01-01"
TODAY = 'today'
THIRTY_DAYS_AGO = '30daysAgo'

OAUTH_PORT = 8082
287 changes: 287 additions & 0 deletions analytics/anvil-analytics-combined/portal-outlinks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: GA4_CREDENTIALS=../../../analytics_keys/ga4_key_test.json\n"
]
}
],
"source": [
"# Add credentials environment variable here\n",
"# Example:\n",
"#%env GA4_CREDENTIALS=your/path/to/your/credentials.json\n",
"%env GA4_CREDENTIALS=../../../analytics_keys/ga4_key_test.json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import analytics.charts as ac\n",
"import analytics.api as ga\n",
"import pandas as pd\n",
"from constants import *\n"
]
},
{
"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%2Fanalytics.readonly&state=9yQGXQsk0VZyaaqUuCVtkfw6Yjsi9T&access_type=offline\n"
]
}
],
"source": [
"default_params = {\n",
" \"service_system\": ac.authenticate_ga(SECRET_NAME, ga.ga4_service_params, port=OAUTH_PORT),\n",
"}\n",
"outlink_search_params = {\n",
" \"start_date\": '30daysAgo',\n",
" \"end_date\": TODAY,\n",
" \"dimensionFilter\": {\n",
" \"filter\": {\n",
" \"stringFilter\": {\n",
" \"matchType\": \"EXACT\",\n",
" \"value\": \"true\"\n",
" },\n",
" \"fieldName\": \"outbound\"\n",
" }\n",
" }\n",
"}\n",
"catalog_params = {\n",
" **default_params,\n",
" **outlink_search_params,\n",
" \"property\": PROPERTY_ID_MAP[ANVIL_CATALOG_NAME],\n",
"}\n",
"explorer_params = {\n",
" **default_params,\n",
" **outlink_search_params,\n",
" \"property\": PROPERTY_ID_MAP[ANVIL_EXPLORER_NAME],\n",
"}\n",
"portal_params = {\n",
" **default_params,\n",
" **outlink_search_params,\n",
" \"base_dimension_filter\": EXCLUDE_PAGES_FILTER,\n",
" \"property\": PROPERTY_ID_MAP[ANVIL_PORTAL_NAME],\n",
"}\n",
"\n",
"outlink_params_for_each_property = {\n",
" ANVIL_PORTAL_NAME: portal_params,\n",
" ANVIL_CATALOG_NAME: catalog_params,\n",
" ANVIL_EXPLORER_NAME: explorer_params,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"anvil-portal\n",
"anvil-catalog\n",
"anvil-explorer\n"
]
}
],
"source": [
"results_dict = {}\n",
"\n",
"for key in outlink_params_for_each_property:\n",
" print(key)\n",
" df_outlinks = (ac.get_data_df(\n",
" [\"eventCount\"],\n",
" [\"pagePath\", \"linkDomain\", \"linkUrl\"], \n",
" **outlink_params_for_each_property[key], \n",
" df_processor=lambda df: df.loc[df.index.get_level_values(1) != \"\"]\n",
" )).rename_axis(\n",
" index={\"pagePath\": \"Current Page\", \"linkDomain\": \"Outlink Domain\", \"linkUrl\": \"Outlink URL\"}\n",
" )\n",
" results_dict[key] = df_outlinks\n",
"\n",
"with pd.ExcelWriter(\"outlinks_summary.xlsx\") as writer:\n",
" for key in results_dict:\n",
" results_dict[key].to_excel(writer, sheet_name=key)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>eventCount</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Current Page</th>\n",
" <th>Outlink Domain</th>\n",
" <th>Outlink URL</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>/data/consortia/GTEx/workspaces</th>\n",
" <th>anvil.terra.bio</th>\n",
" <th>https://anvil.terra.bio/</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/consortia/HPRC/workspaces</th>\n",
" <th>anvil.terra.bio</th>\n",
" <th>https://anvil.terra.bio/</th>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/consortia/GREGoR</th>\n",
" <th>gregorconsortium.org</th>\n",
" <th>https://gregorconsortium.org/data</th>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/studies/phs001642/workspaces</th>\n",
" <th>anvil.terra.bio</th>\n",
" <th>https://anvil.terra.bio/</th>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/studies/phs000424/workspaces</th>\n",
" <th>anvil.terra.bio</th>\n",
" <th>https://anvil.terra.bio/</th>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <th>...</th>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">/data/studies/phs002378</th>\n",
" <th rowspan=\"2\" valign=\"top\">ncbi.nlm.nih.gov</th>\n",
" <th>https://www.ncbi.nlm.nih.gov/books/NBK5295/</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>https://www.ncbi.nlm.nih.gov/projects/gap/cgi-bin/study.cgi?study_id=phs001026</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/studies/phs003047</th>\n",
" <th>dbgap.ncbi.nlm.nih.gov</th>\n",
" <th>https://dbgap.ncbi.nlm.nih.gov/aa/wga.cgi?adddataset=phs003047</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/studies/phs003181/workspaces</th>\n",
" <th>anvil.terra.bio</th>\n",
" <th>https://anvil.terra.bio/</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>/data/studies/phs003184</th>\n",
" <th>ncbi.nlm.nih.gov</th>\n",
" <th>https://www.ncbi.nlm.nih.gov/projects/gap/cgi-bin/study.cgi?study_id=phs003184.v1.p1</th>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>61 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" eventCount\n",
"Current Page Outlink Domain Outlink URL \n",
"/data/consortia/GTEx/workspaces anvil.terra.bio https://anvil.terra.bio/ 26\n",
"/data/consortia/HPRC/workspaces anvil.terra.bio https://anvil.terra.bio/ 18\n",
"/data/consortia/GREGoR gregorconsortium.org https://gregorconsortium.org/data 15\n",
"/data/studies/phs001642/workspaces anvil.terra.bio https://anvil.terra.bio/ 14\n",
"/data/studies/phs000424/workspaces anvil.terra.bio https://anvil.terra.bio/ 13\n",
"... ...\n",
"/data/studies/phs002378 ncbi.nlm.nih.gov https://www.ncbi.nlm.nih.gov/books/NBK5295/ 1\n",
" https://www.ncbi.nlm.nih.gov/projects/gap/cgi-b... 1\n",
"/data/studies/phs003047 dbgap.ncbi.nlm.nih.gov https://dbgap.ncbi.nlm.nih.gov/aa/wga.cgi?addda... 1\n",
"/data/studies/phs003181/workspaces anvil.terra.bio https://anvil.terra.bio/ 1\n",
"/data/studies/phs003184 ncbi.nlm.nih.gov https://www.ncbi.nlm.nih.gov/projects/gap/cgi-b... 1\n",
"\n",
"[61 rows x 1 columns]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_dict[ANVIL_CATALOG_NAME]"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 45da0b3

Please sign in to comment.