Skip to content

Commit 7e4592f

Browse files
ericholscherBlendify
authored andcommitted
Add support for Google Analytics to theme. (#411)
* Add support for Google Analytics to theme. This setting has actually existed in the default config since the original version, but doesn't currently work. This fixes that and supports GA if it's set. * Add docs around analytics_id * Do not conflict with RTD insertion of analytics script * Remove dupliate analytics_id * Add analytics_id to the configuration in its new place
1 parent 5b7d202 commit 7e4592f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

docs/configuring.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ file of this repository, and can be defined in your project's ``conf.py`` via
1919
2020
html_theme_options = {
2121
'canonical_url': '',
22-
'analytics_id': '',
22+
'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
2323
'logo_only': False,
2424
'display_version': True,
2525
'prev_next_buttons_location': 'bottom',

sphinx_rtd_theme/layout.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@
221221
});
222222
</script>
223223

224+
{# Do not conflict with RTD insertion of analytics script #}
225+
{% if not READTHEDOCS %}
226+
{% if theme_analytics_id %}
227+
<!-- Theme Analytics -->
228+
<script>
229+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
230+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
231+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
232+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
233+
234+
ga('create', '{{ theme_analytics_id }}', 'auto');
235+
ga('send', 'pageview');
236+
</script>
237+
238+
{% endif %}
239+
{% endif %}
240+
224241
{%- block footer %} {% endblock %}
225242

226243
</body>

0 commit comments

Comments
 (0)