Skip to content

Commit 08f88ce

Browse files
authored
docs: add gtm (langchain-ai#4887)
Add google tag manager
1 parent 5cbf31e commit 08f88ce

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

docs/_scripts/notebook_hooks.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
"""mkdocs hooks for adding custom logic to documentation pipeline.
2+
3+
Lifecycle events: https://www.mkdocs.org/dev-guide/plugins/#events
4+
"""
5+
16
import logging
27
import os
38
import posixpath
49
import re
510
from typing import Any, Dict
611

12+
from bs4 import BeautifulSoup
13+
from mkdocs.config.defaults import MkDocsConfig
714
from mkdocs.structure.files import Files, File
815
from mkdocs.structure.pages import Page
916

@@ -101,8 +108,7 @@
101108
"how-tos/deploy-self-hosted.md": "cloud/deployment/self_hosted_data_plane.md",
102109
"concepts/self_hosted.md": "concepts/langgraph_self_hosted_data_plane.md",
103110
# assistant redirects
104-
"cloud/how-tos/assistant_versioning.md": "cloud/how-tos/configuration_cloud.md"
105-
111+
"cloud/how-tos/assistant_versioning.md": "cloud/how-tos/configuration_cloud.md",
106112
}
107113

108114

@@ -292,7 +298,7 @@ def on_page_markdown(markdown: str, page: Page, **kwargs: Dict[str, Any]):
292298
"""
293299

294300

295-
def write_html(site_dir, old_path, new_path):
301+
def _write_html(site_dir, old_path, new_path):
296302
"""Write an HTML file in the site_dir with a meta redirect to the new page"""
297303
# Determine all relevant paths
298304
old_path_abs = os.path.join(site_dir, old_path)
@@ -308,6 +314,52 @@ def write_html(site_dir, old_path, new_path):
308314
f.write(content)
309315

310316

317+
def _inject_gtm(html: str) -> str:
318+
"""Inject Google Tag Manager code into the HTML.
319+
320+
Code to inject Google Tag Manager noscript tag immediately after <body>.
321+
322+
This is done via hooks rather than via a template because the MkDocs material
323+
theme does not seem to allow placing the code immediately after the <body> tag
324+
without modifying the template files directly.
325+
326+
Args:
327+
html: The HTML content to modify.
328+
329+
Returns:
330+
The modified HTML content with GTM code injected.
331+
"""
332+
# Code was copied from Google Tag Manager setup instructions.
333+
gtm_code = """
334+
<!-- Google Tag Manager (noscript) -->
335+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T35S4S46"
336+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
337+
<!-- End Google Tag Manager (noscript) -->
338+
"""
339+
soup = BeautifulSoup(html, "html.parser")
340+
body = soup.body
341+
if body:
342+
# Insert the GTM code as raw HTML at the top of <body>
343+
body.insert(0, BeautifulSoup(gtm_code, "html.parser"))
344+
return str(soup)
345+
else:
346+
return html # fallback if no <body> found
347+
348+
349+
def on_post_page(output: str, page: Page, config: MkDocsConfig) -> str:
350+
"""Inject Google Tag Manager noscript tag immediately after <body>.
351+
352+
Args:
353+
output: The HTML output of the page.
354+
page: The page instance.
355+
config: The MkDocs configuration object.
356+
357+
Returns:
358+
modified HTML output with GTM code injected.
359+
"""
360+
return _inject_gtm(output)
361+
362+
311363
# Create HTML files for redirects after site dir has been built
312364
def on_post_build(config):
313365
use_directory_urls = config.get("use_directory_urls")
@@ -324,4 +376,4 @@ def on_post_build(config):
324376
+ hash
325377
+ suffix
326378
)
327-
write_html(config["site_dir"], old_html_path, new_html_path)
379+
_write_html(config["site_dir"], old_html_path, new_html_path)

docs/overrides/main.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{% extends "base.html" %}
22

3+
{% block analytics %}
4+
<!-- Google Tag Manager -->
5+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
6+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
7+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
8+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
9+
})(window,document,'script','dataLayer','GTM-T35S4S46');</script>
10+
<!-- End Google Tag Manager -->
11+
{% endblock %}
12+
13+
314
{% block extrahead %}
415
<meta name="algolia-site-verification" content="165B7E7C89E49946" />
516
<style>
@@ -185,7 +196,6 @@
185196
</style>
186197
{% endblock %}
187198

188-
189199
{% block content %}
190200
<div class="notebook-links">
191201
{% if page.nb_url %}
@@ -209,7 +219,6 @@
209219
{% endif %}
210220
{% endblock %}
211221

212-
213222
{% block announce %}
214223
<strong>We are growing and hiring for multiple roles for LangChain, LangGraph and LangSmith. <a href="https://www.langchain.com/careers" target="_blank" rel="noopener noreferrer"> Join our team!</a></strong>
215224
{% endblock %}

0 commit comments

Comments
 (0)