Skip to content

Commit

Permalink
Goat counter 2 馃悙馃悙 (#247)
Browse files Browse the repository at this point in the history
* Add goat counter

* Allow different goat URL for not-root domain

* more defaults

* Enable the goat by default

* Improve default logic

* docs and fix it
  • Loading branch information
Cadair committed Apr 6, 2024
1 parent 31ccd66 commit 9263f4b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.goatcounter.com/>`__ which defaults to on for sunpy.org domains.

.. grid:: 1 2 2 2
:gutter: 3
Expand Down
33 changes: 33 additions & 0 deletions src/sunpy_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/sunpy_sphinx_theme/theme/sunpy/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 9263f4b

Please sign in to comment.