diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index ba8a15a93..e62379b01 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -72,11 +72,12 @@ def update_config(app, env): # Ref: https://plausible.io/docs/plausible-script if plausible_domain and plausible_url: - plausible_script = f""" - data-domain={plausible_domain} src={plausible_url} - """ - # Link the JS file - app.add_js_file(None, body=plausible_script, loading_method="defer") + kwargs = { + "loading_method": "defer", + "data-domain": plausible_domain, + "filename": plausible_url, + } + app.add_js_file(**kwargs) # Two types of Google Analytics id. gid = analytics.get("google_analytics_id") diff --git a/tests/test_build.py b/tests/test_build.py index 5e3eaf352..b420c78d1 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -556,16 +556,6 @@ def test_edit_page_url(sphinx_build_factory, html_context, edit_url): {"html_theme_options.analytics": {"google_analytics_id": "G-XXXXX"}}, ["gtag", "G-XXXXX"], ), - # plausible - ( - { - "html_theme_options.analytics": { - "plausible_analytics_domain": "toto", - "plausible_analytics_url": "http://.../script.js", - } - }, - ["data-domain", "toto"], - ), # google and plausible ( { @@ -591,6 +581,7 @@ def test_edit_page_url(sphinx_build_factory, html_context, edit_url): ], ) def test_analytics(sphinx_build_factory, provider, tags): + confoverrides = provider sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) sphinx_build.build() @@ -604,6 +595,26 @@ def test_analytics(sphinx_build_factory, provider, tags): assert tags_found is True +def test_plausible(sphinx_build_factory): + provider = { + "html_theme_options.analytics": { + "plausible_analytics_domain": "toto", + "plausible_analytics_url": "http://.../script.js", + } + } + confoverrides = provider + sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) + sphinx_build.build() + index_html = sphinx_build.html_tree("index.html") + + # Search all the scripts and make sure one of them has the Google tag in there + attr_found = False + for script in index_html.select("script"): + if script.attrs.get("data-domain") == "toto": + attr_found = True + assert attr_found is True + + def test_show_nav_level(sphinx_build_factory): """The navbar items align with the proper part of the page.""" confoverrides = {"html_theme_options.show_nav_level": 2}