Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added analytics script to get a simple csv of outlinks (#3202) #3217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
119 changes: 119 additions & 0 deletions analytics/anvil-analytics-combined/portal-outlinks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Add credentials environment variable here\n",
"# Example:\n",
"#%env GA4_CREDENTIALS=your/path/to/your/credentials.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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"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)"
]
}
],
"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