Skip to content

Commit

Permalink
Add support for Google Analytics to theme. (#411)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ericholscher authored and Blendify committed Dec 4, 2018
1 parent 5b7d202 commit 7e4592f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ file of this repository, and can be defined in your project's ``conf.py`` via
html_theme_options = {
'canonical_url': '',
'analytics_id': '',
'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
'logo_only': False,
'display_version': True,
'prev_next_buttons_location': 'bottom',
Expand Down
17 changes: 17 additions & 0 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@
});
</script>

{# Do not conflict with RTD insertion of analytics script #}
{% if not READTHEDOCS %}
{% if theme_analytics_id %}
<!-- Theme Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', '{{ theme_analytics_id }}', 'auto');
ga('send', 'pageview');
</script>

{% endif %}
{% endif %}

{%- block footer %} {% endblock %}

</body>
Expand Down

0 comments on commit 7e4592f

Please sign in to comment.