From 08f88ce8da30f8d4349f86b6165774dd516c2c5e Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Fri, 30 May 2025 21:49:22 -0400 Subject: [PATCH] docs: add gtm (#4887) Add google tag manager --- docs/_scripts/notebook_hooks.py | 60 ++++++++++++++++++++++++++++++--- docs/overrides/main.html | 13 +++++-- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/docs/_scripts/notebook_hooks.py b/docs/_scripts/notebook_hooks.py index 0cebf7b75..30545408e 100644 --- a/docs/_scripts/notebook_hooks.py +++ b/docs/_scripts/notebook_hooks.py @@ -1,9 +1,16 @@ +"""mkdocs hooks for adding custom logic to documentation pipeline. + +Lifecycle events: https://www.mkdocs.org/dev-guide/plugins/#events +""" + import logging import os import posixpath import re from typing import Any, Dict +from bs4 import BeautifulSoup +from mkdocs.config.defaults import MkDocsConfig from mkdocs.structure.files import Files, File from mkdocs.structure.pages import Page @@ -101,8 +108,7 @@ REDIRECT_MAP = { "how-tos/deploy-self-hosted.md": "cloud/deployment/self_hosted_data_plane.md", "concepts/self_hosted.md": "concepts/langgraph_self_hosted_data_plane.md", # assistant redirects - "cloud/how-tos/assistant_versioning.md": "cloud/how-tos/configuration_cloud.md" - + "cloud/how-tos/assistant_versioning.md": "cloud/how-tos/configuration_cloud.md", } @@ -292,7 +298,7 @@ Redirecting... """ -def write_html(site_dir, old_path, new_path): +def _write_html(site_dir, old_path, new_path): """Write an HTML file in the site_dir with a meta redirect to the new page""" # Determine all relevant paths old_path_abs = os.path.join(site_dir, old_path) @@ -308,6 +314,52 @@ def write_html(site_dir, old_path, new_path): f.write(content) +def _inject_gtm(html: str) -> str: + """Inject Google Tag Manager code into the HTML. + + Code to inject Google Tag Manager noscript tag immediately after . + + This is done via hooks rather than via a template because the MkDocs material + theme does not seem to allow placing the code immediately after the tag + without modifying the template files directly. + + Args: + html: The HTML content to modify. + + Returns: + The modified HTML content with GTM code injected. + """ + # Code was copied from Google Tag Manager setup instructions. + gtm_code = """ + + + +""" + soup = BeautifulSoup(html, "html.parser") + body = soup.body + if body: + # Insert the GTM code as raw HTML at the top of + body.insert(0, BeautifulSoup(gtm_code, "html.parser")) + return str(soup) + else: + return html # fallback if no found + + +def on_post_page(output: str, page: Page, config: MkDocsConfig) -> str: + """Inject Google Tag Manager noscript tag immediately after . + + Args: + output: The HTML output of the page. + page: The page instance. + config: The MkDocs configuration object. + + Returns: + modified HTML output with GTM code injected. + """ + return _inject_gtm(output) + + # Create HTML files for redirects after site dir has been built def on_post_build(config): use_directory_urls = config.get("use_directory_urls") @@ -324,4 +376,4 @@ def on_post_build(config): + hash + suffix ) - write_html(config["site_dir"], old_html_path, new_html_path) + _write_html(config["site_dir"], old_html_path, new_html_path) diff --git a/docs/overrides/main.html b/docs/overrides/main.html index 4b59238dc..12a20dfbb 100644 --- a/docs/overrides/main.html +++ b/docs/overrides/main.html @@ -1,5 +1,16 @@ {% extends "base.html" %} +{% block analytics %} + + + +{% endblock %} + + {% block extrahead %} {% endblock %} - {% block content %}