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

Fix plausible loading #909

Merged
merged 6 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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