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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plausible analytics #828

Merged
merged 13 commits into from Jul 24, 2022
17 changes: 17 additions & 0 deletions docs/user_guide/analytics.rst
Expand Up @@ -12,3 +12,20 @@ Google Analytics' javascript is included in the html pages.
html_theme_options = {
"google_analytics_id": "G-XXXXXXXXXX",
}

Plausible Analytics
===================

Alternatively https://plausible.io can be used to gather simple
and privacy-friendly analytics for the site. The dashboard can be accessed
at ``url/domain``.
tupui marked this conversation as resolved.
Show resolved Hide resolved
The Scientific-Python community can offer a self-hosted server.
tupui marked this conversation as resolved.
Show resolved Hide resolved

Plausible' javascript is included in the html pages.
tupui marked this conversation as resolved.
Show resolved Hide resolved

.. code:: python

html_theme_options = {
"plausible_analytics_domain": "my-domain",
"plausible_analytics_url": "https://.../script.js",
tupui marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 11 additions & 0 deletions src/pydata_sphinx_theme/__init__.py
Expand Up @@ -77,6 +77,17 @@ def update_config(app, env):
app.add_js_file(gid_js_path, loading_method="async")
app.add_js_file(None, body=gid_script)

# or Plausible analytics
plausible_domain = theme_options.get("plausible_domain")
plausible_url = theme_options.get("plausible_url")

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")


def prepare_html_config(app, pagename, templatename, context, doctree):
"""Prepare some configuration values for the HTML build.
Expand Down
2 changes: 2 additions & 0 deletions src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf
Expand Up @@ -18,6 +18,8 @@ twitter_url =
icon_links_label = Icon Links
icon_links =
google_analytics_id =
tupui marked this conversation as resolved.
Show resolved Hide resolved
plausible_analytics_domain=
tupui marked this conversation as resolved.
Show resolved Hide resolved
plausible_analytics_url=
favicons =
show_prev_next = True
search_bar_text = Search the docs ...
Expand Down
17 changes: 17 additions & 0 deletions tests/test_build.py
Expand Up @@ -568,6 +568,23 @@ def test_old_google_analytics_id(sphinx_build_factory):
assert tags_found is True


def test_plausible_analytics_id(sphinx_build_factory):
confoverrides = {
"html_theme_options.plausible_domain": "toto",
"html_theme_options.plausible_url": "http://.../script.js",
}
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 plausible domain
tags_found = False
for script in index_html.select("script"):
if script.string and "data-domain" in script.string and "toto" in script.string:
tags_found = True
assert tags_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