Skip to content

Commit

Permalink
Properly configure Google Analytics (#57)
Browse files Browse the repository at this point in the history
* Properly configure Google Analytics configuration

Fixes #48

* Use `black` auto-formatting

* Add html theme option "analytics"
  • Loading branch information
mhostetter authored Apr 3, 2022
1 parent 3b1a1a0 commit 084c498
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 11 additions & 2 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,18 @@ Configuration Options
You can specify any of the available `Google Fonts <https://fonts.google.com/>`_.

.. confval:: google_analytics_account
.. confval:: analytics

Set to enable google analytics.
Set to enable site analytics.

.. code-block:: python
html_theme_options = {
"analytics": {
"provider": "google",
"property": "G-XXXXXXXXXX" # Or "UA-XXXXXXXX-X"
}
}
.. confval:: globaltoc_collapse

Expand Down
14 changes: 12 additions & 2 deletions sphinx_immaterial/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Sphinx Material theme."""
"""Sphinx-Immaterial theme."""

import os
from typing import List, Type, Dict, Mapping
Expand Down Expand Up @@ -225,6 +225,16 @@ def html_page_context(
"versionPath": theme_options.get("version_json"),
}

analytics = None
if theme_options.get("google_analytics"):
# Parse old-style analytics config for backwards compatibility
analytics = {
"provider": "google", # Google is the only provider currently supported
"property": theme_options.get("google_analytics")[0],
}
if theme_options.get("analytics"):
analytics = theme_options.get("analytics")

context.update(
config=dict_merge(
context.get("config", {}),
Expand All @@ -239,9 +249,9 @@ def html_page_context(
"social": theme_options.get("social"),
"disqus": theme_options.get("disqus"),
"manifest": theme_options.get("pwa_manifest"),
"analytics": analytics,
},
"plugins": theme_options.get("plugins"),
"google_analytics": theme_options.get("google_analytics"),
},
),
base_url=base_url,
Expand Down

0 comments on commit 084c498

Please sign in to comment.