diff --git a/docs/conf.py b/docs/conf.py index 87afe702..44233ba3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -77,6 +77,7 @@ "external_links": [ {"name": "Python", "url": "https://www.python.org/"}, ], + "goatcounter_non_domain_endpoint": "https://sunpy-testing.goatcounter.com/count", } graphviz_output_format = "svg" graphviz_dot_args = [ diff --git a/docs/index.rst b/docs/index.rst index 1e5f11fc..132101bb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -15,6 +15,7 @@ The core changes between this theme and the `pydata-sphinx-theme` are: * Add links to the header bar which are specified as theme config variables, with defaults for sunpy.org. * Add a center element to the footer bar. * Restyled the theme for SunPy colors. +* Added optional support for analytics though `goatcounter.com `__ which defaults to on for sunpy.org domains. .. grid:: 1 2 2 2 :gutter: 3 diff --git a/src/sunpy_sphinx_theme/__init__.py b/src/sunpy_sphinx_theme/__init__.py index 93da819d..4fd2c098 100644 --- a/src/sunpy_sphinx_theme/__init__.py +++ b/src/sunpy_sphinx_theme/__init__.py @@ -140,6 +140,39 @@ def setup(app: Sphinx): app.connect("builder-inited", update_config) app.connect("html-page-context", update_html_context) + # Conditionally include goat counter js + # We can't do this in update_config as that causes the scripts to be duplicated. + # Also in here none of the theme defaults have be applied by `update_config` + # TODO: Improve this mess + theme_options = utils.get_theme_options_dict(app) + # We want to default to the sunpy goat counter only if the sst_site_root is sunpy.org + root_domain = theme_options.get("sst_site_root", "https://sunpy.org") + sunpy_goat_url = "https://sunpy.goatcounter.com/count" + default_goat_url = sunpy_goat_url if root_domain == "https://sunpy.org" else None + if primary_goat_url := theme_options.get("goatcounter_analytics_url", default_goat_url): + root_domain = root_domain.removeprefix("https://").removeprefix("http://") + default_endpoint = theme_options.get("goatcounter_non_domain_endpoint", False) + if default_endpoint is False: + default_endpoint = "" + app.add_js_file( + None, + body=f""" + var endpoint = '{default_endpoint}'; + if (location.hostname.endsWith('{root_domain}')) {{ + endpoint = '{primary_goat_url}' + }} + + window.goatcounter = {{ + endpoint: endpoint, + path: function(p) {{ return location.host + p }} + }} + """, + ) + app.add_js_file( + "https://gc.zgo.at/count.js", + loading_method="async", + ) + return { "parallel_read_safe": True, "parallel_write_safe": True, diff --git a/src/sunpy_sphinx_theme/theme/sunpy/theme.conf b/src/sunpy_sphinx_theme/theme/sunpy/theme.conf index d195101a..7fe0ec38 100644 --- a/src/sunpy_sphinx_theme/theme/sunpy/theme.conf +++ b/src/sunpy_sphinx_theme/theme/sunpy/theme.conf @@ -17,6 +17,12 @@ sst_site_root = https://sunpy.org sst_is_root = False # This is the name that appears in the top left of the navbar sst_project_name = SunPy +# Enable sending stats to goat counter +# Set this URL to something like https://MYCODE.goatcounter.com/count +goatcounter_analytics_url = +# If you only want to count visits to sst_site_root or subdomains there of then set this to False +# otherwise set it to another (or the same) goat counter endpoint +goatcounter_non_domain_endpoint = False # The logo to go in the top navbar in place of the project name sst_logo = navbar_links =