Skip to content

Commit

Permalink
Fix plausible loading (#909)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Holdgraf <choldgraf@gmail.com>
Co-authored-by: Chris Holdgraf <choldgraf@berkeley.edu>
Co-authored-by: Stefan Ulbrich <stefan.ulbrich@lgmail.com>
  • Loading branch information
4 people committed Sep 18, 2022
1 parent 02926a8 commit 892620e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/pydata_sphinx_theme/__init__.py
Expand Up @@ -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")
Expand Down
31 changes: 21 additions & 10 deletions tests/test_build.py
Expand Up @@ -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
(
{
Expand All @@ -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()
Expand All @@ -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}
Expand Down

0 comments on commit 892620e

Please sign in to comment.