[ADD] doc: support for analytics keys

This commit is contained in:
Xavier Morel 2014-10-29 15:53:41 +01:00
parent 27fa742a70
commit 28a69fa250
2 changed files with 20 additions and 0 deletions

View File

@ -113,4 +113,15 @@
</p>
</div>
</div>
{%- if google_analytics_key -%}
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{ google_analytics_key }}', 'auto');
ga('send','pageview');
</script>
{%- endif -%}
{%- endblock %}

View File

@ -187,6 +187,9 @@ def setup(app):
app.connect('html-page-context', versionize)
app.add_config_value('versions', '', 'env')
app.connect('html-page-context', analytics)
app.add_config_value('google_analytics_key', False, 'env')
def canonicalize(app, pagename, templatename, context, doctree):
""" Adds a 'canonical' URL for the current document in the rendering
context. Requires the ``canonical_root`` setting being set. The canonical
@ -212,6 +215,12 @@ def versionize(app, pagename, templatename, context, doctree):
if vs != app.config.version
]
def analytics(app, pagename, templatename, context, doctree):
if not app.config.google_analytics_key:
return
context['google_analytics_key'] = app.config.google_analytics_key
def _build_url(root, branch, pagename):
return "{canonical_url}{canonical_branch}/{canonical_page}".format(
canonical_url=root,